3using System.Collections;
4using System.Collections.Generic;
5using System.Runtime.ExceptionServices;
7using System.Threading.Tasks;
25 private readonly Dictionary<string, MQQueue> inputQueues =
new Dictionary<string, MQQueue>();
26 private readonly Dictionary<string, MQQueue> outputQueues =
new Dictionary<string, MQQueue>();
27 private readonly
string queueManager;
28 private readonly
string channel;
29 private readonly
string cipher;
30 private readonly
string cipherSuite;
31 private readonly
string certificateStore;
32 private readonly
string host;
33 private readonly
int port;
34 private MQQueueManager manager;
45 : this(QueueManager, Channel, string.Empty, string.Empty, string.Empty, Host, Port,
Sniffers)
60 public MqClient(
string QueueManager,
string Channel,
string Cipher,
string CipherSuite,
string CertificateStore,
64 this.queueManager = QueueManager;
65 this.channel = Channel;
67 this.cipherSuite = CipherSuite;
68 this.certificateStore = CertificateStore;
80 lock (this.inputQueues)
82 foreach (MQQueue Queue
in this.inputQueues.Values)
86 lock (this.outputQueues)
88 foreach (MQQueue Queue
in this.outputQueues.Values)
92 if (!(this.manager is
null))
96 this.manager?.Close();
113 ConnectionTask Item =
new ConnectionTask(
this, UserName, Password);
114 MqTasks.ExecuteTask(Item);
115 return Item.Completed;
123 public void Connect(
string UserName,
string Password)
128 Hashtable ConnectionParameters =
new Hashtable()
130 { MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED },
131 { MQC.HOST_NAME_PROPERTY, this.host },
132 { MQC.PORT_PROPERTY, this.port },
133 { MQC.CHANNEL_PROPERTY, this.channel },
134 { MQC.USER_ID_PROPERTY, UserName },
135 { MQC.PASSWORD_PROPERTY, Password }
138 if (!
string.IsNullOrEmpty(this.cipher))
139 ConnectionParameters[MQC.SSL_CIPHER_SPEC_PROPERTY] = this.cipher;
141 if (!
string.IsNullOrEmpty(this.cipher))
142 ConnectionParameters[MQC.SSL_CIPHER_SUITE_PROPERTY] = this.cipherSuite;
144 if (!
string.IsNullOrEmpty(this.cipher))
145 ConnectionParameters[MQC.SSL_CERT_STORE_PROPERTY] = this.certificateStore;
147 this.manager =
new MQQueueManager(this.queueManager, ConnectionParameters);
152 ExceptionDispatchInfo.Capture(ex).Throw();
161 public Task
PutAsync(
string QueueName,
string Message)
163 PutTask Item =
new PutTask(
this, QueueName, Message);
164 MqTasks.ExecuteTask(Item);
165 return Item.Completed;
173 public void Put(
string QueueName,
string Message)
181 lock (this.outputQueues)
183 if (!this.outputQueues.TryGetValue(QueueName, out Queue))
185 Queue = this.manager.AccessQueue(QueueName, MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING);
186 this.outputQueues[QueueName] = Queue;
190 MQMessage MqMessage =
new MQMessage()
193 Format = MQC.MQFMT_STRING
198 MqMessage.WriteString(Message);
200 Queue.Put(MqMessage);
215 return this.
GetOne(QueueName, MQC.MQWI_UNLIMITED);
224 public string GetOne(
string QueueName,
int TimeoutMilliseconds)
226 string Result =
null;
232 lock (this.inputQueues)
234 if (!this.inputQueues.TryGetValue(QueueName, out Queue))
236 Queue = this.manager.AccessQueue(QueueName, MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING);
237 this.inputQueues[QueueName] = Queue;
241 MQMessage Message =
new MQMessage();
242 MQGetMessageOptions Options =
new MQGetMessageOptions()
244 Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT,
245 WaitInterval = TimeoutMilliseconds
248 Queue.Get(Message, Options);
250 Result = Message.ReadString(Message.MessageLength);
253 catch (MQException ex)
255 if (ex.Reason == 2033)
259 ExceptionDispatchInfo.Capture(ex).Throw();
264 ExceptionDispatchInfo.Capture(ex).Throw();
277 return this.
GetOneAsync(QueueName, MQC.MQWI_UNLIMITED);
286 public Task<string>
GetOneAsync(
string QueueName,
int TimeoutMilliseconds)
288 GetTask Item =
new GetTask(
this, QueueName, TimeoutMilliseconds);
289 MqTasks.ExecuteTask(Item);
290 return Item.Completed;
324 public void SubscribeIncoming(
string QueueName, ManualResetEvent Cancel, TaskCompletionSource<bool> Stopped,
327 this.
Information(
"Subscribing to messages from " + QueueName);
328 SubscriptionTask Item =
new SubscriptionTask(
this, QueueName, Cancel, Stopped, Callback, State);
329 MqTasks.ExecuteTask(Item);
string GetOne(string QueueName, int TimeoutMilliseconds)
Gets one message from a queue.
void Connect(string UserName, string Password)
Connects to the Queue Manager
Task< string > GetOneAsync(string QueueName)
Gets one message from a queue.
MqClient(string QueueManager, string Channel, string Cipher, string CipherSuite, string CertificateStore, string Host, int Port, params ISniffer[] Sniffers)
IBM MQ client
MqClient(string QueueManager, string Channel, string Host, int Port, params ISniffer[] Sniffers)
IBM MQ client
Task< string > GetOneAsync(string QueueName, int TimeoutMilliseconds)
Gets one message from a queue.
void Dispose()
IDisposable.Dispose
const int DefaultPort
Default port for IBM MQ is 1414.
void SubscribeIncoming(string QueueName, ManualResetEvent Cancel, TaskCompletionSource< bool > Stopped, MqMessageEventHandler Callback, object State)
Subscribes to incoming messages.
void SubscribeIncoming(string QueueName, ManualResetEvent Cancel, MqMessageEventHandler Callback, object State)
Subscribes to incoming messages.
string GetOne(string QueueName)
Gets one message from a queue.
Task ConnectAsync(string UserName, string Password)
Connects to the Queue Manager asynchronously.
void SubscribeIncoming(string QueueName, MqMessageEventHandler Callback, object State)
Subscribes to incoming messages.
void Put(string QueueName, string Message)
Puts a message onto a queue.
Task PutAsync(string QueueName, string Message)
Puts a message onto a queue.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Simple base class for classes implementing communication protocols.
Task Exception(Exception Exception)
Called to inform the viewer of an exception state.
ISniffer[] Sniffers
Registered sniffers.
Task Information(string Comment)
Called to inform the viewer of something.
Task TransmitText(string Text)
Called when text has been transmitted.
Task ReceiveText(string Text)
Called when text has been received.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
delegate Task MqMessageEventHandler(object Sender, MqMessageEventArgs e)
Delegate for MQ Message event handlers or callback methods.