我正在尝试使用来自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);
它不显示消息并挂起,甚至没有终止。
您需要在start()
实例上调用start()
,以使消息流向使用者,例如:
javax.jms.Connection
[另外,值得注意的是,在收到消息之前,调用connection.start()
应该会阻塞,因此您会在此处看到期望的行为。如果您不想阻止,则可以调用javax.jms.MessageConsumer#receive()
并指定超时(以毫秒为单位)。