spring boot 整合activemq
1 Spring Boot与ActiveMQ整合
1.1使用内嵌服务
(1)在pom.xml中引入ActiveMQ起步依赖
<properties> <spring.version>2.0.7.RELEASE</spring.version> </properties> <dependencies> <!--springmvc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.version}</version> </dependency> <!--activemq--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <version>1.5.1.RELEASE</version> </dependency> <!--spirngboot热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
(2)创建消息生产者(application 引导类 必须 和要访问的类 在同一级包下) QueueController.java
@RestController @RequestMapping("/queue") public class QueueController { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @RequestMapping("/send") public void send(String text){ jmsMessagingTemplate.convertAndSend("code",text); } }
(3)创建消息消费者 QueueConsumer
@Component public class QueueConsumer { @JmsListener(destination = "code") public void readMessage(String text){ System.out.println("接收到的消息 : " + text); } }
测试:启动服务后,在浏览器执行
http://localhost:8088/send.do?text=aaaaa
即可看到控制台输出消息提示。Spring Boot内置了ActiveMQ的服务,所以我们不用单独启动也可以执行应用程序。

1.2使用外部服务
在src/main/resources下的application.properties增加配置, 指定ActiveMQ的地址
|
spring.activemq.broker-url=tcp://192.168.25.130:61616 |
application.properties配置
# tomcat服务器端口号
server.port=8080
url=http://www.baidu.com
# activemq地址
spring.activemq.broker-url=tcp://192.168.200.128:61616
运行后,会在activeMQ中看到发送的queue

1.3发送Map信息
(1)向QueueController.java添加代码
@RequestMapping("/sendmap")
public void sendMap(){
Map map = new HashMap();
map.put("name", "曜");
map.put("sex", "男");
map.put("age", "18");
jmsMessagingTemplate.convertAndSend("codemap",map);
}
(2)向Consumer.java添加代码
@JmsListener(destination = "codemap") public void readMap(Map map){ System.out.println(map); }
控制台打印

浙公网安备 33010602011771号