Linux安装ActiveMQ以及SpringBoot使用ActiveMQ
activeMQ下载地址:http://activemq.apache.org/components/classic/download/
ActiveMQ依赖JDK版本,根据自己在Linux安装好的JDK进行选择安装合适的版本。
参考版本链接:https://blog.csdn.net/qq_26483671/article/details/79297258
ActiveMQ安装教程:
我在自己虚拟机Linux系统上安装的jdk1.7,所以我下载的activeMQ本版为apache-activemq-5.9.0-bin.tar.gz,我是在windows上下好,然后Xftp传输到我的虚拟机上。
在Linux将文件进行解压,使用下面命令
tar -zxvf apache-activemq-5.9.0.0-bin.tar.gz
修改目录名称
mv apache-activemq-5.9.0 activemq-5.9.0
启动 ActiveMQ
cd activemq-5.9.0
./bin/activemq start ## 启动ActiveMQ
需要注意的问题
注意:我们需要在虚拟机上的Linux开启activeMQ需要的端口号,让外部可以访问,开启端口号前,需将防火墙打开。
- firewall-cmd --zone=public --add-port=61616/tcp --permanent
- firewall-cmd --zone=public --add-port=8161/tcp --permanent
- firewall-cmd --reload
ActiveMQ 的默认端口号是 61616 负责接收发送消息的tcp端口,使用 #netstat -an|grep 61616 可以检测是否正常启动,8161 是图形管理界面访问端口 默认用户名和密码是admin/admin
SpringBoot使用activeMQ
1.新建SpringBoot项目,项目依赖如下:
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--activemq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<!---->
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.项目的配置信息
################# port
server.port=8088
################# activemq
spring.activemq.broker-url=tcp://192.168.213.208:61616
spring.activemq.in-memory=true
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=false
发送方
@Component
public class Producer {
@Autowired
private JmsMessagingTemplate jmsTemplate;
/**
*发送消息
* @param destination
* @param message
*/
public void sendMessage(Destination destination,String message){
jmsTemplate.convertAndSend(destination,message);
}
}
接收方
@Component
public class Consumer {
@JmsListener(destination = "myqueues")
public void acceptMessage(String text){
System.out.println(text);
}
}
控制器
@Controller
public class TestController {
@Autowired
private Producer producer;
@RequestMapping("/activemq")
@ResponseBody
public String activemq(){
//点对点消息
Destination destination = new ActiveMQQueue("myqueues");
for (int i=1;i<=3;i++) {
producer.sendMessage(destination, "hello");
}
return "success";
}
}
访问结果:
hello
hello
hello