首先去官网下载ActiveMQ,下载地址为
http://activemq.apache.org/download.html
选择ActiveMQ5,然后选择linux版本,如果你没有Linux也可以选择Windows版本
如果你是win系统,解压缩之后找到如下地址:binwin64双击activemq.bat启动mq在浏览器中输入地址,这里使用默认的端口访问http://localhost:8161/:
点击Manage ActiveMQ broker即可进入管理页面,默认账号密码都为admin。
Windows版本讲解到此告一段落,本文主要讲解linux版本的ActiveMQ,其实都是大同小异的,可以借鉴一下。
一、解压缩
1、将下载好的ActiveMQ包上传到Linux中2、解压缩
tar -zxvf apache-activemq-5.15.11-bin.tar.gz
二、启动ActiveMQ
cd apache-activemq-5.15.11
./bin/activemq start
看到以下提示说明运行成功
浏览器中输入xxx.xx.xx.xx:8161即可访问,如下图所示
emmm,不急先来解释下名词吧;
1. queue(p2p模式):生产者生产的一个消息只能被一个消费者使用。比如微信公众号私信给我^_^
2. topic(pub/sub):生产者生产了一个消息,可以被多个消费者进行消费。比如公众号给你们推送文章^_^
3. Producer:生产者。即一个消息的生产者、发布者。
4. Consumer:消费者。即一个消息的消费者、订阅者。
三、整合SpringBoot
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2、配置文件
#链接账号
spring.activemq.user=admin
#链接密码
spring.activemq.password=admin
#链接地址
spring.activemq.broker-url=tcp://xx.xx.xx.xx:61616
#如果是点对点(queue),那么此处默认应该是false,如果发布订阅,那么一定设置为true
spring.jms.pub-sub-domain=true
注意:8161是管理页面的端口,而61616是程序使用的端口,是不一致的。
3、创建生产者
package com.ifilldream.activemq_lean.manager;
import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.*;
/**
* @ClassName Producer
* @Description TODO
* @Author RickSun && iFillDream
* @Date 2020/1/16 14:50
* @Version 1.0
*/
@Service
public class Producer {
protected final Log logger = LogFactory.getLog(getClass());
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* 普通queue队列
* @param destinationName
* @param message
*/
public void sendMsg(String destinationName,String message){
Destination destination=new ActiveMQQueue(destinationName);
jmsMessagingTemplate.convertAndSend(destination,message);
}
/**
* 延迟发送
* @param queueName
* @param text
* @param time
*/
public void delaySend( String queueName, String text, Long time) {
//获取连接工厂
ConnectionFactory connectionFactory = this.jmsMessagingTemplate.getConnectionFactory();
try {
//获取连接
Connection connection = connectionFactory.createConnection();
connection.start();
//获取session
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建一个消息队列
Destination destination = session.createQueue(queueName);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage message = session.createTextMessage(text);
//设置延迟时间
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
//发送
producer.send(message);
session.commit();
producer.close();
session.close();
connection.close();
} catch (Exception e) {
e.getMessage();
}
}
/**
* 发送主题消息
* @param topic
* @param message
*/
public void sendMsgTopic(String topic,String message){
Topic destination = new ActiveMQTopic(topic);
jmsMessagingTemplate.convertAndSend(destination,message);
}
}
4、创建消费者
package com.ifilldream.activemq_lean.manager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
/**
* @ClassName Consumer
* @Description TODO
* @Author RickSun && iFillDream
* @Date 2020/1/16 15:00
* @Version 1.0
*/
@Service
public class Consumer {
protected final Log logger = LogFactory.getLog(getClass());
@JmsListener(destination ="test.queue")
public void test(String content) {
logger.info("*************test收到消息*************" content);
//TODO something
}
@JmsListener(destination ="test2.queue")
public void test2(String content) {
logger.info("*************test2收到消息*************" content);
//TODO something
}
//*********以下是Topic*********
@JmsListener(destination = "active.topic")
public void readActiveTopic1(String content) {
logger.info("*************topic1收到消息*************" content);
//TODO something
}
@JmsListener(destination = "active.topic")
public void readActiveTopic2(String content) {
logger.info("*************topic2收到消息*************" content);
//TODO something
}
}
5、测试Controller
package com.ifilldream.activemq_lean.controller;
import com.ifilldream.rocketmq_lean.manager.Producer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @ClassName ActiveController
* @Description TODO
* @Author RickSun && iFillDream
* @Date 2020/1/16 15:15
* @Version 1.0
*/
@RestController
@RequestMapping("/ifilldream/activemq")
public class ActiveController {
@Resource
private Producer producer;
@GetMapping("/test")
public String test(String content) {
producer.sendMsg("test.queue", content);
producer.delaySend("test2.queue",content, 10000L);
producer.sendMsgTopic("active.topic", content);
return "ok.";
}
}
在浏览器中输入测试地址:
http://localhost:8888/ifilldream/activemq/test?content=ifilldream
我们将spring.jms.pub-sub-domain设置为false,再测试一遍
有同学就要问了,这样配置只能支持topic和queue中的一个,如果要同时支持两种形式,该怎么办呢
四、兼容topic和queue
首先将配置文件的spring.jms.pub-sub-domain注释掉
然后就需要加一个配置类了,如下操作
package com.ifilldream.activemq_lean.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import javax.jms.ConnectionFactory;
/**
* @ClassName ActivemqConfig
* @Description TODO
* @Author RickSun && iFillDream
* @Date 2020/1/16 15:18
* @Version 1.0
*/
@Configuration
public class ActivemqConfig {
/**
* JmsListener注解默认只接收queue消息,如果要接收topic消息,需要设置containerFactory
*/
@Bean
public JmsListenerContainerFactory<?> topicListenerContainer(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory topicListenerContainer = new DefaultJmsListenerContainerFactory();
topicListenerContainer.setPubSubDomain(true);
topicListenerContainer.setConnectionFactory(connectionFactory);
return topicListenerContainer;
}
}
修改消费者(Consumer),操作如下:
好了,重启项目,可以看到效果:
到此SpringBoot整合ActiveMQ的教程就全部讲解完毕了。
![]()
统一首发平台为-微|信-*公|众-号"轻梦致新",搜索关注-公|众-**号,第一时间阅读最新内容。