We are using IBM MQ XMS C# client ver 9.0 (on .NET 4.6.2 framework) to work with IBM MQ. I just need to know of all the messages on a given queue without removing them off the queue.
We also got the Consumers set up for the queue. Need both consumers & browsers working in tandem. Browser should not remove messages but still need to get all the messages.
So I have set up a QueueBrowser like below, but queueBrowser.GetEnumerator() doesn't get messages at all.
With the same code if Create a MessageConsumer and attach a listener, it will get the messages posted the queue. So issue with QueueBrowser only.
Can anyone point out why its happening like this. Why queueEnumerator.MoveNext() always return false, indicating no messages on the queue.
XMSFactoryFactory xMSFactoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
// Create WMQ Connection Factory.
IConnectionFactory connectionFactory = xMSFactoryFactory.CreateConnectionFactory();
connectionFactory.SetStringProperty(XMSC.WMQ_HOST_NAME, "hostname");
connectionFactory.SetIntProperty(XMSC.WMQ_PORT, portNumber);
connectionFactory.SetStringProperty(XMSC.WMQ_CHANNEL, "channelName");
connectionFactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
connectionFactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "QueueManagerName");
// Create connection.
connectionWMQ = connectionFactory.CreateConnection();
connectionWMQ.ExceptionListener = new ExceptionListener(OnXMSException);
// Create session
ISession sessionWMQ = connectionWMQ.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
IDestination destination = sessionWMQ.CreateQueue("QueueName");
IQueueBrowser queueBrowser = sessionWMQ.CreateBrowser(destination);
connectionWMQ.Start();
Thread thread = new Thread(KeepBrowsingMessaegs);
thread.Start();
--end of the method
private void KeepBrowsingMessaegs()
{
IEnumerator queueEnumerator = queueBrowser.GetEnumerator();
while (!cancellationTokenSource.IsCancellationRequested)
{
if (queueEnumerator.MoveNext())
{
ITextMessage textMessage = queueEnumerator.Current as ITextMessage;
if (textMessage != null)
{
System.Diagnostics.Trace.Write(textMessage);
}
}
}
}