1

要建立连接,需要以下对象:

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

我的情况是我必须连接到 5 个队列Consumer和 5 个队列Producer。看了很多,得出的结论IConnection应该是单实例对象。但是我开始提问的地方是;我是否必须ISession为每个队列连接创建新对象?或者这也可以是单实例?

引自IBM 文档

会话是用于发送和接收消息的单线程上下文。

如果应用程序必须在多个线程上同时处理消息,则应用程序必须在每个线程上创建一个会话,然后将该会话用于该线程内的任何发送或接收操作。

那么我们可以得出结论,每个队列连接都必须有一个会话吗?换句话说,这意味着我必须创建 10 ISession、 10 IDestination、 5IMessageConsumer和 5 个IMessageProducer对象。我对吗?这里的最佳做法是什么?

我还在某处读到,新ISession创建将根据SHARECNV值建立一个新的 TCP 连接(如果为 1,则每个会话都是一个新的 TCP 连接)。那么使用单实例有什么意义IConnection呢?

伪代码

创建连接

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();

对所有传入消息执行 5 次

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

对所有传出消息执行 5 次

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

开始连接

connection.Start();

处置连接

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

0 回答 0