从 MQ 消费消息
Consume message from MQ
我正在尝试使用来自 MQ 的消息,但代码挂起并且不显示输出。
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);
它不显示消息并挂起,甚至不终止。
您需要在 javax.jms.Connection
的实例上调用 start()
才能让消息流向消费者,例如:
connection.start()
此外,值得注意的是调用 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)
并指定超时(以毫秒为单位)。
我正在尝试使用来自 MQ 的消息,但代码挂起并且不显示输出。
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);
它不显示消息并挂起,甚至不终止。
您需要在 javax.jms.Connection
的实例上调用 start()
才能让消息流向消费者,例如:
connection.start()
此外,值得注意的是调用 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)
并指定超时(以毫秒为单位)。