我试图将一些来自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");
}
答案 0 :(得分:3)
据此,您已经创建了一个交易会话:
connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
根据the docs,
当应用程序关闭事务会话时,隐式回滚 发生。当应用程序关闭连接时,隐式回滚 发生在所有连接的交易会话中。
回滚会删除队列中的所有消息,然后才能在应用程序外部显示它们。所以我想你可能想要提交交易,是吗?
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异常是一种多层结构,期望特定于供应商的诊断放置在较低级别的元素中。只有通过打印这些较低级别的元素,才有可能知道传输层认为错误的内容。