Reputation: 756
Below is the code responsible for creating the connection:
IConnection connectionMQ;
factoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
IConnectionFactory cf = factoryFactory.CreateConnectionFactory();
cf.SetStringProperty(XMSC.WMQ_HOST_NAME, "host");
cf.SetIntProperty(XMSC.WMQ_PORT, port);
cf.SetStringProperty(XMSC.WMQ_CHANNEL, "channel");
cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "queueManager");
cf.SetStringProperty(XMSC.WMQ_SSL_PEER_NAME, "sslPeerName");
cf.SetStringProperty(XMSC.WMQ_SSL_CIPHER_SPEC, "TLS_RSA_WITH_AES_256_CBC_SHA256");
cf.SetStringProperty(XMSC.WMQ_CCSID, "ccSid");
cf.SetStringProperty(XMSC.WMQ_SSL_KEY_REPOSITORY, "*USER");
cf.SetStringProperty(XMSC.WMQ_SSL_CLIENT_CERT_LABEL, "clientCertLabel");
connectionMQ = cf.CreateConnection();
Consumer :
using (sessionWMQ = connectionWMQ.CreateSession(true, AcknowledgeMode.AutoAcknowledge))
{
var destination = sessionWMQ.CreateQueue("queueName");
consumerAsync = sessionWMQ.CreateConsumer(destination);
var messageListener = new MessageListener(OnMessageCallback);
consumerAsync.MessageListener = messageListener;
connectionWMQ.Start();
}
OnMessageCallback:
public void OnMessageCallback(IMessage message)
{
if (message != null)
{
numMessage++;
sessionWMQ.Commit();
}
}
This implementation causes me to download exactly one message and not the next ones. What should I change to download all messages and even if the queue is empty it will wait for the message?
I will add that there is no error and this message is correctly taken from the queue and the next time I connect this message is gone.
Upvotes: 0
Views: 184
Reputation: 6226
The objects that you're creating in your consumer section are instantly going out of scope and therefore they are simply not staying 'alive' long enough to be able to process subsequent messages.
The listener component is disposed of at the end of the using statement, and therefore the reference to the OnMessageCallback
delegate is also closed off.
Are you able to create variables at a higher level, such that will remain in-scope? Also, if this code is within a class which is created on start-up of your function/console app, this needs to be held in scope, too, as this will be cleaned-up otherwise and your listener closed early.
Upvotes: 0