简体   繁体   中英

Consume message from MQ

I am trying to consume message from MQ but the code hangs and doesn't show output.

JmsFactoryFactory FF = jmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = FF.createConnectionFactory();
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, HOST);
cf.setStringProperty(WMQConstants.WMQ_PORT, PORT);
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, CHANNEL);
cf.setStringProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, QMGR);
cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "JMS");
Connection connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(queuename);
MessageConsumer consumer1 = session.createConsumer(destination);

Message reply = consumer1.receive();
System.out.println(reply);

It doesn't show the message and hangs and doesn't even terminate.

You need to call start() on your instance of javax.jms.Connection in order to get messages to flow to the consumer, eg:

connection.start()

Also, it's worth noting that calling javax.jms.MessageConsumer#receive() is expected to block until a message is received so you're seeing the expected behavior there. If you don't want to block you can call javax.jms.MessageConsumer#receive(long) and specify a timeout (in milliseconds).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM