Queue实现的是点到点模型,在下面的例子中,启动2个消费者共同监听一个Queue,然后循环给这个Queue中发送多个消息,我们依然采用ActiveMQ。
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
public class QueueTest {
/**
* @param args
* @throws JMSException
*/
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER
, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
//JMS 客户端到JMS Provider 的连接
Connection connection = factory.createConnection();
connection.start();
// Session: 一个发送或接收消息的线程
Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
// Destination :消息的目的地;消息发送给谁.
// 获取session注意参数值my-queue是Query的名字
Queue queue = new ActiveMQQueue("queueTest");
MessageConsumer comsumer1 = session.createConsumer(queue);
comsumer1.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
System.out.println("Consumer 1 get " + ((TextMessage)message).getText());
} catch (Exception e) {
e.printStackTrace();
}
}
});
MessageConsumer comsumer2 = session.createConsumer(queue);
comsumer2.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
System.out.println("Consumer 2 get " + ((TextMessage)message).getText());
} catch (Exception e) {
e.printStackTrace();
}
}
});
MessageProducer producer = session.createProducer(queue);
for (int i=0; i<10;) {
producer.send(session.createTextMessage("Message:" + ++i));
}
session.close();
connection.close();
}
}
运行这个例子会得到下面的输出结果:
Consumer 1 get Message:1
Consumer 2 get Message:2
Consumer 1 get Message:3
Consumer 2 get Message:4
Consumer 1 get Message:5
Consumer 2 get Message:6
Consumer 1 get Message:7
Consumer 2 get Message:8
Consumer 1 get Message:9
Consumer 2 get Message:10
可以看出每个消息直被消费了一次,但是如果有多个消费者同时监听一个Queue的话,无法确定一个消息最终会被哪一个消费者消费。
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
public class QueueTest {
/**
* @param args
* @throws JMSException
*/
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER
, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
//JMS 客户端到JMS Provider 的连接
Connection connection = factory.createConnection();
connection.start();
// Session: 一个发送或接收消息的线程
Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
// Destination :消息的目的地;消息发送给谁.
// 获取session注意参数值my-queue是Query的名字
Queue queue = new ActiveMQQueue("queueTest");
MessageConsumer comsumer1 = session.createConsumer(queue);
comsumer1.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
System.out.println("Consumer 1 get " + ((TextMessage)message).getText());
} catch (Exception e) {
e.printStackTrace();
}
}
});
MessageConsumer comsumer2 = session.createConsumer(queue);
comsumer2.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
System.out.println("Consumer 2 get " + ((TextMessage)message).getText());
} catch (Exception e) {
e.printStackTrace();
}
}
});
MessageProducer producer = session.createProducer(queue);
for (int i=0; i<10;) {
producer.send(session.createTextMessage("Message:" + ++i));
}
session.close();
connection.close();
}
}
运行这个例子会得到下面的输出结果:
Consumer 1 get Message:1
Consumer 2 get Message:2
Consumer 1 get Message:3
Consumer 2 get Message:4
Consumer 1 get Message:5
Consumer 2 get Message:6
Consumer 1 get Message:7
Consumer 2 get Message:8
Consumer 1 get Message:9
Consumer 2 get Message:10
可以看出每个消息直被消费了一次,但是如果有多个消费者同时监听一个Queue的话,无法确定一个消息最终会被哪一个消费者消费。