我试图将一些消息从 JMS 代码放入本地队列管理器中定义的本地队列。我已经在 WebSphere MQ 中定义了一个本地队列 LQ
并使用 JMS 代码来放置消息。我在这里做得对吗。我没有看到 WebSphere 队列中的消息。
这是代码:
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
//set up the Connection Configuration values
// Set the properties
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "HostName");
cf.setIntProperty(WMQConstants.WMQ_PORT, 1414);
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "SYSTEM.DEF.SVRCONN");
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "TEST.JMS");
//JNDI is Not used Here
MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
MQQueueSession session = (MQQueueSession) connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("queue:///LQ");
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session.createTextMessage("MQJMSTest "+ uniqueNumber);
//MQMessage message = (MQMessage) session.createTextMessage("MQJMSTest "+ uniqueNumber);
// Start the connection
connection.start();
sender.send(message);
System.out.println("Sent message:\\n" + message);
//JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
// System.out.println("\\nReceived message:\\n" + receivedMessage);
sender.close();
receiver.close();
session.close();
connection.stop();
connection.close();
System.out.println("\\nSUCCESS\\n");
} catch (JMSException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println(e);
System.out.println("\\nFAILURE\\n");
}
catch (Exception e){
System.out.println(e);
System.out.println("\\nFAILURE\\n");
}
最佳答案
据此,您已经创建了一个事务处理 session :
connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
根据 the docs ,
回滚会在队列中的任何消息在放置它们的应用程序之外变得可见之前删除它们。所以我想你可能想要 COMMIT 交易,是吗?
sender.send(message);
// ADD THIS HERE ===========================
session.commit();
// ADD THIS HERE ===========================
System.out.println("Sent message:\\n" + message);
// THE COMMIT MUST HAVE OCCURRED BEFORE YOU CAN GET THE MESSAGE
// BACK OUT OF THE QUEUE IN THE NEXT LINES
//JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
// System.out.println("\\nReceived message:\\n" + receivedMessage);
// ===================================================
// DON'T FORGET TO COMMIT THE MESSAGE ON THE WAY BACK OUT OR IT WILL
// JUST GE TPUT BACK ON THE QUEUE AGAIN.
// session.commit();
// ===================================================
sender.close();
receiver.close();
session.close();
PS:请参阅 JMS Exception handling 部分。 JMS Exception 是一个多层结构,在该结构中,特定于供应商的诊断应放置在较低级别的元素中。只有打印那些较低级别的元素,才有可能知道传输层认为什么是错误的。
关于jms - 无法在 WebSphere MQ 队列中看到 JMS 消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14445409/