TemporaryQueue和TemporaryTopic,从字面上就可以看出它们是"临时"的目的地。可以通过Session来创建,例如:
TemporaryQueue replyQueue = session.createTemporaryQueue();
虽然它们是由Session来创建的,但是它们的生命周期确是整个Connection。如果在一个Connection上创建了两个Session,则一个Session创建的TemporaryQueue或TemporaryTopic也可以被另一个Session访问。那如果这两个Session是由不同的Connection创建,则一个Session创建的TemporaryQueue不可以被另一个Session访问。
另外,它们的主要作用就是用来指定回复目的地, 即作为JMSReplyTo。
在下面的例子中,先创建一个Connection,然后创建两个Session,其中一个Session创建了一个TemporaryQueue,另一个Session在这个TemporaryQueue上读取消息。
package com.bijian.study;
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
public class TemporaryQueueTest {
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
connection.start();
Queue queue = new ActiveMQQueue("testQueue2");
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 接收消息,并回复到指定的Queue中(即replyQueue)
MessageConsumer comsumer = session.createConsumer(queue);
comsumer.setMessageListener(new MessageListener() {
public void onMessage(Message m) {
try {
System.out.println("Get Message: " + ((TextMessage) m).getText());
MessageProducer producer = session.createProducer(m.getJMSReplyTo());
producer.send(session.createTextMessage("ReplyMessage"));
} catch (JMSException e) {
}
}
});
// 使用session创建一个TemporaryQueue。
TemporaryQueue replyQueue = session.createTemporaryQueue();
// 使用同一个Connection创建另一个Session,来读取replyQueue上的消息。
Session session2 = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
MessageConsumer replyComsumer = session2.createConsumer(replyQueue);
replyComsumer.setMessageListener(new MessageListener() {
public void onMessage(Message m) {
try {
System.out.println("Get reply: " + ((TextMessage) m).getText());
} catch (JMSException e) {
}
}
});
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage("SimpleMessage");
message.setJMSReplyTo(replyQueue);
producer.send(message);
}
}
运行结果为:
Get Message: SimpleMessage Get reply: ReplyMessage
如果将:
Session session2 = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
更改为:
Connection connection2 = factory.createConnection();
Session session2 = connection2.createSession(true, Session.AUTO_ACKNOWLEDGE);
就会得到类似于下面的异常:
Exception in thread "main" javax.jms.InvalidDestinationException: Cannot use a Temporary destination from another Connection at org.apache.activemq.ActiveMQMessageConsumer.<init>(ActiveMQMessageConsumer.java:197) at org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:1303) at org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:1247) at org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:1159) at org.apache.activemq.ActiveMQSession.createConsumer(ActiveMQSession.java:1131) at com.bijian.study.TemporaryQueueTest.main(TemporaryQueueTest.java:37)
PS:ActiveMQ Connection方法:createSession(boolean transacted, int acknowledgeMode);其中transacted为使用事务标识,acknowledgeMode为签收模式。
文章来源:http://www.cnblogs.com/guthing/archive/2010/06/12/1757187.html