问题
i am new to jms and activemq. i produce one message to one static queue in activemq and get reply back to temporary queue using getJMSReplyTo. the code is following
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 = (Destination) session.createQueue("PostWithParameter_Queue");
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);
//consumer.setMessageListener(new StaticQueueListener());
// Wait for a message
Message message = (Message) consumer.receive();
MessageConsumer consumer1 = session.createConsumer(message.getJMSReplyTo());
consumer1.setMessageListener(new ReplyHandler());
consumer.close();
session.close();
My Reply Handler follows
@Override
public void onMessage(Message message) {
try
{
System.out.println(message.getStringProperty("status"));
}
catch (JMSException e)
{
e.printStackTrace();
}
}
Now i am getting the result from temporary queue. because its in the same session. my question is i want to get the message from temporary queue in differesnt session using temporary queue name. if i want to get the message from temporary queue in different client using temporary queue name how i will do that ? .
回答1:
Usually, you would set the reply-to jms header, and then wherever you consume the message (eg, certainly in a different session..) you can pull out that JMSReplyTo header which would be the temp destination and just reply to that.
You should check the ActiveMQ wiki:
http://activemq.apache.org/how-should-i-implement-request-response-with-jms.html
回答2:
Your listener can work with any JMS session that you provide,it doesn't have to be the same session as the one that you placed the message with.
来源:https://stackoverflow.com/questions/19705413/how-to-get-the-message-from-temporary-queue-in-different-session