浅聊ActiveMQ与SpringBoot集成

集成步骤

(1)消息发送者

  创建SpringBoot工程activemq-boot-sender作为消息发送者

在pom.xml文件中添加依赖

  这个依赖在创建Module的时候,如果勾选了集成ActiveMQ会自动生成

<!--SpringBoot集成ActiveMQ的起步依赖-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

在SpringBoot的核心配置文件application.properties中配置ActiveMQ的连接信息

#配置activemq的连接信息
spring.activemq.broker-url=tcp://192.168.32.130:61616
#目的地
spring.jms.template.default-destination=bootQueue
#默认是缓存了jms的session的,所以主程序发送完消息后,不会退出
# 改为false主程序才可以退出 从SpringBoot2.1.0以后新增的
spring.jms.cache.enabled=false

创建com.beijing.activemq.service包,并在下面创建一个MessageService类,并提供发送消息的方法

 1 package com.beijing.activemq.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.jms.core.JmsTemplate;
 5 import org.springframework.jms.core.MessageCreator;
 6 import org.springframework.stereotype.Service;
 7 
 8 import javax.jms.JMSException;
 9 import javax.jms.Message;
10 import javax.jms.Session;
11 @Service
12 public class MessageService {
13     //这里的JmsTemplate是SpringBoot自动配置的
14     @Autowired
15     private JmsTemplate jmsTemplate;
16     public void sendMessage(){
17         jmsTemplate.send(new MessageCreator() {
18             public Message createMessage(Session session) throws JMSException {
19                 return session.createTextMessage("Hello,Spring ActiveMQ");
20             }
21         });
22     }
23 }

 在SpringBoot的主程序中编写测试代码,运行测试发送消息

 1 @SpringBootApplication
 2 public class Application {
 3    public static void main(String[] args) {
 4       //获取Spring容器
 5       ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
 6       //根据容器获取bean对象
 7       MessageService messageService = context.getBean("messageService", MessageService.class);
 8       //调用bean对象的方法, 发送消息
 9       messageService.sendMessage();
10    }
11 }

 在ActiveMQ控制台查看效果

(2)消息接收者

  创建SpringBoot工程activemq-boot-receiver作为消息接收者(和上面的图示一样)

在pom.xml文件中添加相关依赖

这个依赖在创建Module的时候,如果勾选了集成ActiveMQ会自动生成

<!--SpringBoot集成ActiveMQ的起步依赖-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

在SpringBoot的核心配置文件application.properties中配置ActiveMQ的连接信息

#配置activemq的连接信息
spring.activemq.broker-url=tcp://192.168.32.130:61616
#目的地
spring.jms.template.default-destination=bootQueue
#默认是缓存了jms的session的,所以主程序发送完消息后,不会退出
# 改为false主程序才可以退出 从SpringBoot2.1.0以后新增的
spring.jms.cache.enabled=false

在com.bjpowernode.activemq.service包下创建一个MessageService类,并提供接收消息的方法

 1 @Service
 2 public class MessageService {
 3     //这里的JmsTemplate是SpringBoot自动配置的
 4     @Autowired
 5     private JmsTemplate jmsTemplate;
 6     public void receiveMessage(){
 7         Message message = jmsTemplate.receive();
 8         if(message instanceof TextMessage){
 9             try {
10                 String text = ((TextMessage) message).getText();
11                 System.out.println("SpringBoot接收到的消息为:" + text);
12             } catch (JMSException e) {
13                 e.printStackTrace();
14             }
15         }
16     }
17 }

 在SpringBoot的主程序中编写测试代码,运行测试接收消息 

 1 @SpringBootApplication
 2 public class Application {
 3    public static void main(String[] args) {
 4       ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
 5       //根据容器获取bean对象
 6       MessageService messageService = context.getBean("messageService", MessageService.class);
 7       //调用bean对象的方法, 发送消息
 8       messageService.receiveMessage();
 9    }
10 }

 在ActiveMQ控制台查看效果

猜你喜欢

转载自www.cnblogs.com/really199/p/10493209.html