文章目录
1. ActiveMQ JMS入门案例
环境配置
- 官网地址: https://activemq.apache.org/
- 下载成功后解压,进入bin目录执行
./active start
即可 - 访问web页面, 服务器地址:8161 用户名密码均为 admin
1.1 环境准备
- pom文件引入坐标依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.0</version>
</dependency>
1.2 JMS-点对点模式发送消息
- 后台代码
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.junit.Test;
import javax.jms.*;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-05 13:46
*/
public class MQProvider {
/*
* 模式: 点对点模式
* 需求: 发送消息
* */
@Test
public void sendMessage() throws Exception{
// 创建连接工厂 对象,指定连接地址 ,协议,ip,通信端口
String brokerUrl = "tcp://192.168.25.120:61616";
ConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl);
// 从工厂中获取连接对象
Connection connection = cf.createConnection();
// 开启连接
connection.start();
// 从连接中获取session session存储回话数据
// 参数一: true 使用AUTO_ACKNOWLEDGE之外的消息发送模式 自动确认模式
// 参数二: 自动确认模式 AUTO_ACKNOWLEDGE
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 设置消息发送的目标地 在acticeMQ消息服务器开辟空间并起名字 ningning
Queue ningning = session.createQueue("ningning");
// 指定消息发送者 并告知空间名称
MessageProducer producer = session.createProducer(ningning);
// 创建消息对象封装消息
TextMessage message = new ActiveMQTextMessage();
message.setText("宁宁小可爱");
// 发送消息
producer.send(message);
// 关闭资源
producer.close();
session.close();
connection.close();
}
}
- 可以看到消息发送成功
1.3 JMS-点对点模式接收消息
点对点模式接收消息
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.junit.Test;
import javax.jms.*;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-05 14:06
*/
public class ActiveMqConsumer {
/*
* 模式: 点对点
* 需求: 接收消息
* */
@Test
public void recvMessage() throws Exception{
// 创建连接工厂 对象,指定连接地址 ,协议,ip,通信端口
String brokerUrl = "tcp://192.168.25.120:61616";
ConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl);
// 从工厂中获取连接对象
Connection connection = cf.createConnection();
// 开启连接
connection.start();
// 从连接中获取session session存储回话数据
// 参数一: true 使用AUTO_ACKNOWLEDGE之外的消息发送模式 自动确认模式
// 参数二: 自动确认模式 AUTO_ACKNOWLEDGE
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 指定消息空间
Queue ningning = session.createQueue("ningning");
// 指定消息消费者 并告知空间名称
MessageConsumer consumer = session.createConsumer(ningning);
// 接收消息
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
// 获取消息
if (message instanceof TextMessage){
TextMessage msg = (TextMessage) message;
try {
String text = msg.getText();
System.out.println("接收消息: "+text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
// 关闭资源
consumer.close();
session.close();
connection.close();
}
}
结果展示
1.4 JMS-发布订阅模式-发送消息
- 发布订阅模式发送消息
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.junit.Test;
import javax.jms.*;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-05 13:46
*/
public class TopicMQProvider {
/*
* 模式: 发布订阅模式
* 需求: 发送消息
* */
@Test
public void sendMessage() throws Exception{
// 创建连接工厂 对象,指定连接地址 ,协议,ip,通信端口
String brokerUrl = "tcp://192.168.25.120:61616";
ConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl);
// 从工厂中获取连接对象
Connection connection = cf.createConnection();
// 开启连接
connection.start();
// 从连接中获取session session存储回话数据
// 参数一: true 使用AUTO_ACKNOWLEDGE之外的消息发送模式 自动确认模式
// 参数二: 自动确认模式 AUTO_ACKNOWLEDGE
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 设置消息发送的目标地 在acticeMQ消息服务器开辟空间并起名字 ningning
Topic ningning = session.createTopic("ningning");
// 指定消息发送者 并告知空间名称
MessageProducer producer = session.createProducer(ningning);
// 创建消息对象封装消息
TextMessage message = new ActiveMQTextMessage();
message.setText("宁宁小可爱,星河滚烫,你是人间理想");
// 发送消息
producer.send(message);
// 关闭资源
producer.close();
session.close();
connection.close();
}
}
- 结果展示
1.5 JMS-发布订阅模式-接收消息
- 后台代码
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
import javax.jms.*;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-05 14:06
*/
public class TopicActiveMqConsumer {
/*
* 模式: 发布订阅
* 需求: 接收消息
* */
@Test
public void recvMessage() throws Exception{
// 创建连接工厂 对象,指定连接地址 ,协议,ip,通信端口
String brokerUrl = "tcp://192.168.25.120:61616";
ConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl);
// 从工厂中获取连接对象
Connection connection = cf.createConnection();
// 开启连接
connection.start();
// 从连接中获取session session存储回话数据
// 参数一: true 使用AUTO_ACKNOWLEDGE之外的消息发送模式 自动确认模式
// 参数二: 自动确认模式 AUTO_ACKNOWLEDGE
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 指定消息空间 Topic
Topic ningning = session.createTopic("ningning");
// 指定消息消费者 并告知空间名称
MessageConsumer consumer = session.createConsumer(ningning);
// 接收消息
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
// 获取消息
if (message instanceof TextMessage){
TextMessage msg = (TextMessage) message;
try {
String text = msg.getText();
System.out.println("接收消息: "+text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
// 阻塞
System.in.read();
// 关闭资源
consumer.close();
session.close();
connection.close();
}
}
- 结果展示