点对点和发布订阅模式比较
点对点:消息不会丢失,即消费方不在线(没有启动),发送方发送的消息,下一次消费方上线(启动)还是会接收到
发布订阅:消息可能会丢失,即消费方不在线(没有启动),发送方发送的消息,可能就会丢失。即消费方不在线时,发送方发送的消息,消费方就不管了,就接收不到了。
示例
一、创建Maven工程(普通Jar工程)
二、引入pom依赖
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.13.4</version>
</dependency>
</dependencies>
点对点模式Queue
1.创建QueueProducer - 消息发送方/提供方
public class QueueProducer {
public static void main(String[] args) throws Exception {
// 创建连接工厂
ActiveMQConnectionFactory ConnectionFactory = new ActiveMQConnectionFactory("tcp://10.35.30.33:61616");
// 创建连接
Connection connection = ConnectionFactory.createConnection();
// 启动连接
connection.start();
// 建立会话
// 参数:是否启动事务、消息确认模式
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建队列对象
Queue queue = session.createQueue("test-queue");
TextMessage hello_mq = session.createTextMessage("Hello MQ");
MessageProducer producer = session.createProducer(queue);
producer.send(hello_mq);
// 关闭资源
producer.close();
session.close();
connection.close();
}
}
2.创建QueueConsumer - 消息接收方/消费方
public class QueueConsumer {
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://10.35.30.33:61616");
// 获取连接
Connection connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 获取session (参数1:是否启动事务,参数2:消息确认模式)
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建队列对象
Queue queue = session.createQueue("test-queue");
// 创建消息消费
MessageConsumer consumer = session.createConsumer(queue);
// 监听消息
consumer.setMessageListener(
(message) -> {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println("接收到消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
);
// 等待键盘输入
System.in.read();
// 关闭资源
consumer.close();
session.close();
connection.close();
}
}
订阅发布模式Topic
1.提供方
public class TopicProducer {
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://10.35.30.88:61616");
// 获取连接
Connection connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 获取session (参数1:是否启动事务,参数2:消息确认模式)
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建主题对象
Topic topic = session.createTopic("test-topic");
// 创建消息生产者
MessageProducer producer = session.createProducer(topic);
// 创建消息
TextMessage textMessage = session.createTextMessage("Hello Topic ActiveMQ");
// 发送消息
producer.send(textMessage);
// 关闭资源
producer.close();
session.close();
connection.close();
}
}
2.消费方1、消费方2
public class TopicConsumer1 {
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://10.35.30.88:61616");
// 获取连接
Connection connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 获取session (参数1:是否启动事务,参数2:消息确认模式)
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建主题对象
Topic topic = session.createTopic("test-topic");
// 创建消息消费
MessageConsumer consumer = session.createConsumer(topic);
// 监听消息
consumer.setMessageListener(
(message)-> {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println("接收到消息:"+textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
);
// 等待键盘输入
System.in.read();
// 关闭资源
consumer.close();
session.close();
connection.close();
}
}
public class TopicConsumer2 {
public static void main(String[] args) throws Exception {
// 创建连接工厂
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://10.35.30.88:61616");
// 获取连接
Connection connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 获取session (参数1:是否启动事务,参数2:消息确认模式)
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建主题对象
Topic topic = session.createTopic("test-topic");
// 创建消息消费
MessageConsumer consumer = session.createConsumer(topic);
// 监听消息
consumer.setMessageListener(
(message)-> {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println("接收到消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
);
// 等待键盘输入
System.in.read();
// 关闭资源
consumer.close();
session.close();
connection.close();
}
}