开发者

Can I send batched messages through a Websphere MQ Queue using Java JMS?

I'm working in a project that requires to send multiple messages to another application via asynchronous Websphere MQ messages. Actually I'm opening and closing sessions for every message I send. I appreciate your answers. By开发者_JAVA百科 the way this is my first post here.


In your question header, you mentioned the word batched which made me think you might want to send all the grouped messages in a single transaction so that all messages in the group are delivered or none at all. (An atomic send). If this is an important piece, I would slightly modify Friek's (clean and concise) code as follows:

Session session = connection.createSession(true, SESSION_TRANSACTED);
....
producer.send(msgOne);
producer.send(msgTwo);
session.commit();
....


I figure something like this should work:

Session session = connection.createSession(false, SESSION.AUTO_ACKNOWLEDGE);
// Create first message
Message msgOne = session.createTextMessage("Message One");
// Set reply-to queue to REPLY1QUEUE
msgOne.setJMSReplyTo(session.createQueue("REPLY1QUEUE"));
// Create another message.
Message msgTwo = session.createTextMessage("Message Two");
msgTwo.setJMSReplyTo(session.createQueue("REPLY2QUEUE"));

// Initialize destination queue and message producer.
MessageProducer producer = session.createProducer(session.createQueue("DESTQUEUE"));

// Connect, send and close.
connection.start();
producer.send(msgOne);
producer.send(msgTwo);
connection.close();

// Close the session.
session.close();

If I'm not mistaken, the reply-to queue is optional.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜