hazzy
hazzy

Reputation: 107

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.

Upvotes: 2

Views: 804

Answers (1)

Justin Bertram
Justin Bertram

Reputation: 34973

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

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).

Upvotes: 6

Related Questions