Should each IMessageConsumer or IMessageProducer have its own ISession?

青春壹個敷衍的年華 提交于 2019-12-13 18:13:07

问题


To make a connection, following objects are needed:

IConnection
ISession
IDestination
IMessageConsumer // receiving messages
IMessageProducer // sending messages

My case is that I have to connect to 5 queues as Consumer and to 5 queues as Producer. After reading a lot, I came to the conclusion that IConnection should be a single-instance object. But where I start asking questions is; Do I have to create new ISession objects for each queue connection? Or can this be a single-instance too?

Quote from IBM Documentation

A session is a single threaded context for sending and receiving messages.

If an application must process messages concurrently on more than one thread, the application must create a session on each thread, and then use that session for any send or receive operation within that thread.

So can we conclude that there must be a session for each queue connection? In other words, this means that I have to create 10 ISession, 10 IDestination, 5 IMessageConsumer and 5 IMessageProducer objects. Am I right? What's the best practice here?

I've also read somewhere that a new ISession creation will make a new TCP connection depending on SHARECNV value (if 1, every session is a new TCP connection). So what's the point of using a single-instance IConnection then?

PSEUDO CODE

Create connection

var factory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
var cf = factory.CreateConnectionFactory();
cf.SetStringProperty(XMSC.WMQ_HOST_NAME, host);
cf.SetIntProperty(XMSC.WMQ_PORT, port);
cf.SetStringProperty(XMSC.WMQ_CHANNEL, channel);
cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, qm);
cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
connection = cf.CreateConnection();

Do this 5 times for all incoming messages

ISession session = connection.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
IDestination destination = session.CreateQueue(queuename);
IMessageConsumer consumer = session.CreateConsumer(destination);
consumer.MessageListener = listener;

Do this 5 times for all outgoing messages

ISession session = connection.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
IDestination destination = session.CreateQueue(queuename);
IMessageProducer producer = session.CreateProducer(destination);

Start connection

connection.Start();

Dispose connection

producer?.Close(); //all producers
consumer?.Close(); //all consumers
destination?.Dispose(); //all destinations
session?.Dispose(); //all sessions
connection?.Close();

来源:https://stackoverflow.com/questions/50743594/should-each-imessageconsumer-or-imessageproducer-have-its-own-isession

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!