(六)springboot整合activemq
1、现下载activemq,下载链接:http://activemq.apache.org/download.html,windows系统解压后进入bin目录,分32位、64位操作系统,运行activemq.bat启动程序,访问http://http://127.0.0.1:8161
出现该界面说明安装成功,点击broker,输入账号admin 密码admin 进入管理界面
点击queue按钮,可以创建消息队列。
2、pom文件中增加以下依赖,在application.properties对activema进行配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency>
#访问地址
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
#是否开启线程池
spring.activemq.pool.enabled=true
#最大连接数
spring.activemq.pool.max-connections=50
3、在Application.class中添加如下代码,方便注入队列

package com.zc.app.test; import javax.jms.Queue; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class TestApplication { @Bean public Queue queue(){ return new ActiveMQQueue("test.queue"); } public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
4、新建一个service接口用来发送消息,并实现发送消息

/** * */ package com.zc.app.test.service; import javax.jms.Destination; public interface MsgService { public void sendMessage(Destination destination,String message); public void sendMessage(String message); }

/** * */ package com.zc.app.test.service.impl; import javax.jms.Destination; import javax.jms.Queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Service; import com.zc.app.test.service.MsgService; @Service public class MsgServiceImpl implements MsgService{ @Autowired private Queue queue; @Autowired private JmsMessagingTemplate jms; //用来发送消息 @Override public void sendMessage(Destination destination,String message) { jms.convertAndSend(this.queue, message); } @Override public void sendMessage(String message) { jms.convertAndSend(message); } }
5、写controller来调用接口

/** * */ package com.zc.app.test.controller; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.zc.app.test.service.MsgService; @RestController @RequestMapping("/msg") public class SendController { @Autowired private MsgService msgService; @GetMapping("send") public String order(String message) { Destination destination = new ActiveMQQueue("test.queue"); msgService.sendMessage(destination, message); return "send message success"; } }
6、启动程序,这时程序出现错误,提示JmsMessagingTemplate注入失败
Field jms in com.zc.app.test.service.impl.MsgServiceImpl required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' that could not be found.
7、通过测试,将acticemq线程池配置删除后,程序可以正常启动,最后找到原因是因为少了jms的pool依赖包,在pom文件添加以下依赖后可以正常启动
<dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> </dependency>
8、然后访问url:localhost:8080/msg/send?message=124515,提示成功后,可以从activemq的管理界面看到test.queue消息增加