上一讲我们将ActiveMQ整合到Spring中,这一讲我们尝试将ActiveMQ整合到Springboot中。
1.创建Springboot项目
略;
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jzt</groupId>
<artifactId>activemq-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--spring boot整合activemq的jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.application.yml配置文件
# web占用的端口
server:
port: 7777
spring:
activemq:
# activemq的broker的url
broker-url: tcp://192.168.106.131:61616
# 连接activemq的broker所需的账号和密码
user: admin
password: admin
jms:
# 目的地是queue还是topic, false(默认) = queue true = topic
pub-sub-domain: false
# 自定义队列名称。这只是个常量
myqueue: boot-activemq-queue
4.配置目的地bean,并开启JMS功能
package com.jzt;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;
/**
* @author sj
*/ // 让spring管理的注解,相当于spring中在xml 中写了个bean
@Component
// 开启jms适配
@EnableJms
public class ConfigBean {
// 注入配置文件中的 myqueue
@Value("${myqueue}")
private String myQueue ;
@Bean
public ActiveMQQueue queue(){
return new ActiveMQQueue(myQueue);
}
}
5.基于queue模型的实现
生产者:
package com.jzt.queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
import java.util.UUID;
/**
* @author sj
*/
@Component
public class QueueProducer {
// JMS模板JmsMessagingTemplate 通过模板来实现消息的发送
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
//消息的目的地
@Autowired
private Queue queue ;
//发送消息
public void sendMessage(){
jmsMessagingTemplate.convertAndSend(queue,"****" + UUID.randomUUID().toString().substring(0,6));
System.out.println("发送成功");
}
// 开启一个定时任务。每5秒执行一次。仅用于测试发送消息
@Scheduled(fixedDelay = 5000)
public void produceMessageScheduled(){
sendMessage();
}
}
运行服务,发现消息发送成功,控制台也能看到结果了。
消费者:
package com.jzt.queue;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* @author sj
*/
@Component
public class QueueConsumer {
//注册一个监听器,通过监听器接收消息
@JmsListener(destination = "${myqueue}")
public void receive(TextMessage textMessage) throws JMSException {
System.out.println("收到的消息:"+textMessage.getText());
}
}
运行结果如下:
6.基于发布/订阅模式
首先需要修改application.yml配置文件:
# web占用的端口
server:
port: 7777
spring:
activemq:
# activemq的broker的url
broker-url: tcp://192.168.106.131:61616
# 连接activemq的broker所需的账号和密码
user: admin
password: admin
jms:
# 目的地是queue还是topic, false(默认) = queue true = topic
pub-sub-domain: true
# 自定义队列名称。这只是个常量
myqueue: boot-activemq-queue
# 自定义主题名称。这只是个常量
mytopic: boot-activemq-topic
主题配置类
package com.jzt.topic;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;
/**
* @author sj
*/
@Component
@EnableJms
public class ConfigTopicBean {
// 注入配置文件中的 mytopic
@Value("${mytopic}")
private String myTopic ;
@Bean
public ActiveMQTopic topic(){
return new ActiveMQTopic(myTopic);
}
}
生产者:
package com.jzt.topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Topic;
import java.util.UUID;
/**
* @author sj
*/
@Component
public class TopicProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
public void sendTopic(){
jmsMessagingTemplate.convertAndSend(topic, "主体消息"+ UUID.randomUUID().toString().substring(0,6));
}
@Scheduled(fixedDelay = 5000)
public void sendTopicSchedule(){
sendTopic();
}
}
运行服务,结果如下:
消费者:
package com.jzt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author sj
*/
@SpringBootApplication
// 是否开启定时任务调度功能
@EnableScheduling
public class ActiveMQApplication {
public static void main(String[] args) {
SpringApplication.run(ActiveMQApplication.class, args);
}
}
运行结果如下:
小结:
实际项目中大部分与Springboot和spring整合来实现消息中间件服务,这样便于开发。以上便是与springboot整合的demo。