IBM MQ侦听器服务-不会触发onMessage [英] IBM MQ listener service - onMessage not getting triggered
问题描述
我想从IBM MQ中读取消息并仅打印它们.
I want to read messages from IBM MQ and just print them.
//我的主要班级
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueSession;
import com.ibm.msg.client.wmq.WMQConstants;
public class QueueConnector {
public static void main(String[] args) throws JMSException {
MQQueueConnectionFactory qcf = new MQQueueConnectionFactory();
MQQueueConnection qc;
Queue queue;
MQQueueSession queueSession;
MessageConsumer consumer;
qcf.setHostName ("XYZ");
qcf.setPort (Integer.parseInt("abc"));
qcf.setQueueManager ("ABC");
qcf.setChannel ("IJK");
qcf.setTransportType (WMQConstants.WMQ_CM_CLIENT);
qc = (MQQueueConnection) qcf.createQueueConnection ();
queue = new MQQueue("QUEUE_NAME");
queueSession = (MQQueueSession) qc.createQueueSession (false,Session.AUTO_ACKNOWLEDGE);
consumer = queueSession.createConsumer(queue);
Listener listener = new Listener();
consumer.setMessageListener(listener);
qc.start();
}
}
//侦听器类
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class Listener implements MessageListener {
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
System.out.println(((TextMessage)message).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("From Listener!");
}
}
即使队列中有消息,主类也将在不调用onMessage的情况下终止.谁能帮我这个忙.
The main class is getting terminated without calling onMessage, even if there are messages in queue. Can anyone help me with this.
推荐答案
您需要保持程序运行.当您到达main的末尾时,该程序将退出,因此您的监听器将不会触发.
You need to keep your program running. When you reach the end of main - the program will exit and hence your listener won't fire.
尝试将new Scanner(System.in).nextLine();
放在方法的末尾以验证其是否有效.当然,您可能需要将此退出条件更改为更合适的条件(或实际上关闭扫描仪).
Try placing new Scanner(System.in).nextLine();
at the end of your method to verify that it works. Of course, you may want to change this exit condition to something more appropriate (or actually close the scanner).
这篇关于IBM MQ侦听器服务-不会触发onMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!