介绍
- 使用activeMQ5, 在window下搭建单节点环境
- 使用JMS 测试AMQ Queue和Topic
安装
下载地址:activemq
- 解压
- 启动
- 访问
http://localhost:8161/index.html
登陆: admin/admin
依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.3</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.17</version>
</dependency>
队列 Queue
生产者将消息发送到队列,消费者消费消息。生产者不用关注消费者状态。消息不会被重复消费
步骤
1.创建工厂类
2.建立连接
3.创建session
4.创建队列
5.发送消息
6.关闭连接
生产者
public class MQProcuder {
private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
public void testProcuder() throws JMSException {
//创建工厂
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//建立获取连接
Connection connection = factory.createConnection();
connection.start();
//创建session, 是否开启事物, 接收方式
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建队列
Queue queue = session.createQueue("guan-queue");
//创建生产者
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < 6; i++) {
TextMessage textMessage = session.createTextMessage("hello active" + i);
//发送消息
producer.send(textMessage);
}
//关闭连接
producer.close();
session.close();
connection.close();
}
//测试
public static void main(String[] args) throws JMSException {
MQProcuder procuder = new MQProcuder();
procuder.procuder();
}
}
启动生产者
Queues
队列如下
消费者
public class MQCustomer {
private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
public void testCustomer() throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("guan-queue");
MessageConsumer consumer = session.createConsumer(queue);
while (true) {
TextMessage messge = (TextMessage) consumer.receive();
if (messge != null) {
String text = messge.getText();
System.out.println(text);
} else {
break;
}
}
}
//测试
public static void main(String[] args) throws JMSException {
MQCustomer mqCustomer = new MQCustomer();
mqCustomer.testCustomer();
}
}
启动消费者
注册了1 个消费者, 消费了6 条消息
Topic
topic 是发布/订阅
生产者
public class TopicProducer {
private static final String ACTIVEMQ_URL = "tcp://localhost:61616";
public void testTopicProducer() throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("guan-queue");
MessageProducer produce = session.createProducer(topic);
for (int i = 0; i < 6; i++) {
TextMessage textMessage = session.createTextMessage("hello active" + i);
//发送消息
produce.send(textMessage);
}
//关闭资源
produce.close();
session.close();
connection.close();
}
public static void main(String[] args) throws JMSException {
TopicProducer producer = new TopicProducer();
producer.testTopicProducer();
}
}
启动生产者
消费者
public class TopicCustomer {
private static final String ACTIVEMQ_URL = "tcp://localhost:61616";
public void testTopicCustomer() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("guan-queue");
MessageConsumer topConsumer = session.createConsumer(topic);
topConsumer.setMessageListener((message -> {
if (message != null && message instanceof TextMessage) {
try {
TextMessage textMessage = (TextMessage) message;
System.out.println(textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}));
System.in.read();
System.out.println("接收完毕====");
//关闭资源
topConsumer.close();
session.close();
connection.close();
}
public static void main(String[] args) throws Exception {
TopicCustomer topicCustomer = new TopicCustomer();
topicCustomer.testTopicCustomer();
}
}
启动消费者
topic 需启动消费者