规格:IBMMQ7.x,Jdk1.7,TOMCAT7,WINOS-7
问:一个侦听器事件,用于持续监视指定IBM-MQ上的队列,并提醒/触发/通知我有‘and of fresh message’可供使用。
问题:不允许“for -Loop”、Timer或Cron监视队列中的新消息。
非常感谢您的建议。
发布于 2017-02-20 18:01:55
相反,为什么不设置一个消息侦听器,并在消息到达时使用它们?当消息到达队列时,您的应用程序消息侦听器将被调用。
以下是示例代码:
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
// Create connection factory
cf = ff.createConnectionFactory();
// Set MQ properties
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QM1");
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setIntProperty(WMQConstants.WMQ_PORT,1414);
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "localhost");
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "MY.SVRCONN");
System.out.println("Connection Factory created.");
connection = cf.createConnection();
System.out.println("Connection created.");
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("Session created.");
Destination destination = session.createQueue("SUB.Q");
System.out.println("Destination created: " + destination.toString());
consumer = session.createConsumer(destination);
System.out.println("Consumer created.");
// Now receive messages asynchronously
// Create and register a new MessageListener for this consumer
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
try {
// Display the message that just arrived
//System.out.println(msg);
TextMessage txtMsg = (TextMessage)msg;
System.out.println(txtMsg.getText());
} // end try
catch (Exception e) {
System.out.println("Exception caught in onMessage():\n" + e);
}
return;
} // end onMessage()
}); // end setMessageListener
connection.start();
Thread.sleep(5000);
session.close();https://stackoverflow.com/questions/42339318
复制相似问题