消息的发布与订阅
Publish类
package com.cb01;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Publish {
//1.连接工厂
private ConnectionFactory factory;
//2.连接对象
private Connection connection;
//3.Session对象
private Session session;
//4.生产者
private MessageProducer producer;
public Publish(){
try {
factory=new ActiveMQConnectionFactory(
"cb",
"123456",
"tcp://localhost:61616");
connection=factory.createConnection();
connection.start();
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
producer=session.createProducer(null);
} catch (Exception e) {
e.printStackTrace();
}
}
public void send(){
try {
Destination destination=session.createTopic("topic1");
TextMessage textMessage=session.createTextMessage("Publish发布的测试消息");
producer.send(destination,textMessage);
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
Publish publish=new Publish();
publish.send();
}
}
运行上面Publish类,查看ActiveMQ管控台
此时,启动3个Consumer类,分别为ConsumerOne、ConsumerTwo、ConsumerThree。下面以ConsumerOne的代码为例
package com.cb01;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ConsumerOne {
//1.连接工厂
private ConnectionFactory factory;
//2.连接对象
private Connection connection;
//3.Session对象
private Session session;
//4.消费者
private MessageConsumer consumer;
public ConsumerOne(){
try {
factory=new ActiveMQConnectionFactory(
"cb",
"123456",
"tcp://localhost:61616");
connection=factory.createConnection();
connection.start();
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
} catch (Exception e) {
e.printStackTrace();
}
}
public void receiver(){
try {
Destination destination=session.createTopic("topic1");
consumer=session.createConsumer(destination);
consumer.setMessageListener(new Listener());
} catch (JMSException e) {
e.printStackTrace();
}
}
class Listener implements MessageListener{
public void onMessage(Message message) {
try {
if(message instanceof TextMessage){
System.out.println("ConsumerOne:"+message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ConsumerOne consumer=new ConsumerOne();
consumer.receiver();
}
}
此时,再次运行Publish类,并查看ActiveMQ控制台。Message Enqueued由之前的1变为2,Message Dequeued由之前的0变为3,说明三个消费者都对Publish新发送的消息进行了消费。
上图中的5个Connection分别由下图中5个进程建立
同时,在3个Consumer对应的console中均显示接收到的消息内容,下面以ConsumerOne中的内容为例
ConsumerOne:ActiveMQTextMessage {commandId = 5, responseRequired = true, messageId = ID:cb-PC-53237-1514340360647-1:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:cb-PC-53237-1514340360647-1:1:1:1, destination = topic://topic1, transactionId = null, expiration = 0, timestamp = 1514340360863, arrival = 0, brokerInTime = 1514340360865, brokerOutTime = 1514340360868, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@1b7a12b9, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = Publish发布的测试消息}