1

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();
Ozkan
  • 3,880
  • 9
  • 47
  • 78
  • In general the larger the value of `SHARECNV` the less throughput each session can achieve since that number sessions are funneled over a single single TCP connection. Conversely setting `SHARECNV(1)` will give the best throughput. – JoshMc Jun 07 '18 at 20:27
  • Thanks for your comment, do you agree with separate sessions for each producer / consumer? @JoshMc – Ozkan Jun 08 '18 at 04:13
  • With `separate` I still mean they are on same thread, but just separate objects – Ozkan Jun 08 '18 at 04:41
  • I think if in the same thread there is no benefit since you are not concurrently producing or consuming with in the same thread so you would not have contention with a single session for that thread. As you quoted "the application must create **a** session on each thread, and then use **that** session for **any** send or receive operation within that thread.". I take that to mean a single session for the thread to be used by 1 or many send receive operations in that thread. – JoshMc Jun 08 '18 at 04:50

0 Answers0