ActiveMQ集成spring boot
一、整合Springboot队列模式
pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.study</groupId>
<artifactId>ActiveMQ-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ActiveMQ-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
activemq:
broker-url: tcp://0.0.0.0:61616
user: admin
password: admin
pool:
max-connections: 100
jms:
pub-sub-domain: false #false代表队列 true代表主题
#目标的名字
myQueue: activeMQ-springboot-queue
配置类
package com.study.demo.config;
import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
*
* 创建配置类
*
*/
@Component
public class QueueConfig {
@Value("${myQueue}")
private String myQueue;
@Bean
public Queue queue() {
return new ActiveMQQueue(myQueue);
}
}
创建队列生产者
package com.study.demo.queue;
import java.util.Date;
import javax.jms.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
/**
*
* 创建生产者
*
*/
@Component
public class QueueProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@SuppressWarnings("deprecation")
public void produceMsg() {
jmsMessagingTemplate.convertAndSend(queue,"message"+new Date().toLocaleString());
}
}
创建监听
package com.study.demo.queue;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/*
* 创建监听器
*/
@Component
public class QueueConsumer {
@JmsListener(destination = "${myQueue}")
public void receive(TextMessage textMessage) {
try {
System.err.println(textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试类
package com.study.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.study.demo.queue.QueueProducer;
/*
* 测试类
*/
@SpringBootTest
class ActiveMqSpringbootApplicationTests {
@Autowired
QueueProducer queueProducer;
@Test
void contextLoads() {
queueProducer.produceMsg();
}
}
测试结果
延时发送
修改生产者类
package com.study.demo.queue;
import java.util.Date;
import javax.jms.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;
/**
*
* 创建生产者
*
*/
@Component
public class QueueProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@SuppressWarnings("deprecation")
public void produceMsg() {
jmsMessagingTemplate.convertAndSend(queue,"message"+new Date().toLocaleString());
}
//延迟发送
@SuppressWarnings("deprecation")
@Scheduled(fixedDelay = 3000)
public void produceMsgScheduled(){
jmsMessagingTemplate.convertAndSend(queue,"message"+new Date().toLocaleString());
}
}
测试,启动类启动程序
package com.study.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableJms
@EnableScheduling
public class ActiveMqSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(ActiveMqSpringbootApplication.class, args);
}
}
3秒钟发送一次
二、整合Springboot发布/订阅模式
配置application.yml
spring:
activemq:
broker-url: tcp://0.0.0.0:61616
user: admin
password: admin
pool:
max-connections: 100
jms:
pub-sub-domain: true #false代表队列 true代表主题
#目标的名字
myTopic: activeMQ-springboot-topic
创建配置类
package com.study.demo.config;
import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
*
* 创建配置类
*
*/
@Component
public class TopicConfig {
@Value("${myTopic}")
private String myTopic;
@Bean
public Topic topic() {
return new ActiveMQTopic(myTopic);
}
}
创建生产者
package com.study.demo.topic;
import java.util.Date;
import javax.jms.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;
/**
*
* 创建主题生产者
*
*/
@Component
public class TopicProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
@SuppressWarnings("deprecation")
public void produceMsg() {
jmsMessagingTemplate.convertAndSend(topic,"message"+new Date().toLocaleString());
}
//延迟发送
@SuppressWarnings("deprecation")
@Scheduled(fixedDelay = 3000)
public void produceMsgScheduled(){
jmsMessagingTemplate.convertAndSend(topic,"message"+new Date().toLocaleString());
}
}
创建监听器
package com.study.demo.topic;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/*
* 创建主题监听器
*/
@Component
public class TopciConsumer {
@JmsListener(destination = "${myTopic}")
public void receive(TextMessage textMessage) {
try {
System.err.println(textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试
因为是主题模式,要先启动消费者,然后在启动生产者
所以要先启动启动类,然后在启动测试类
启动类代码
package com.study.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableJms
@EnableScheduling
public class ActiveMqSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(ActiveMqSpringbootApplication.class, args);
}
}
测试类代码
package com.study.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.study.demo.topic.TopicProducer;
/*
* 测试类
*/
@SpringBootTest
class ActiveMqSpringbootApplicationTests {
@Autowired
TopicProducer topicProducer;
@Test
void contextLoads() {
topicProducer.produceMsg();
}
}