-1
我使用默認端口61616在本地主機上運行ActiveMQ。我在一個名爲topic.STUDENTS
的主題中放置了一些消息。我有一個客戶端程序,我試圖用它來讀取主題中的消息。這是程序的主要部分,不能按預期工作。我期待收到一些消息,但我只收到null
。爲什麼我的JMS客戶端不使用主題中的消息?
public void receiveMessages() throws URISyntaxException, JMSException, Exception {
try {
//Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
//Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
//Create a session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Create the destination (Topic or Queue)
Destination destination = session.createTopic("topic.STUDENTS");
//Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);
//Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}
consumer.close();
session.close();
connection.close();
} catch(Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
此代碼基本上是從猿的ActiveMQ的「Hello World」例如代碼:http://activemq.apache.org/hello-world.html
有誰知道爲什麼我沒有收到來自任何主題的消息?這裏有一個消息是,我可以通過./activemq browse topic.STUDENTS
命令請參閱主題:
JMS_BODY_FIELD:JMSText = Invention-my-dear-friends-is-93%-perspiration-6%-electricity
JMS_HEADER_FIELD:JMSExpiration = 0
JMS_HEADER_FIELD:JMSMessageID = ID:user-thinkpad-335i-59919-1457977678059-1:1:1:1:3
JMS_HEADER_FIELD:JMSPriority = 4
JMS_HEADER_FIELD:JMSDestination = topic.STUDENTS
JMS_HEADER_FIELD:JMSTimestamp = 1457977678409
JMS_HEADER_FIELD:JMSRedelivered = false
JMS_HEADER_FIELD:JMSDeliveryMode = persistent
我認爲你對此可能是正確的。我在創建'Destination'到'createQueue'時改變了對會話參考的調用,並且我收到了我期望的消息。 :D這工作。 – user3870315