- springboot整合activeMQ之queue
- queue默认持久化,jdbc持久化配置及使用journal高速缓存的方式见activemq
- 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.atguigu</groupId>
<artifactId>springboot_mq_produce</artifactId>
<version>1.0-SNAPSHOT</version>
<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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.1.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
-
生产者微服务
- application.yml
server: port: 7777 spring: activemq: broker-url: tcp://localhost:61616 user: admin password: admin jms: pub-sub-domain: false # false表示queue,true表示topic # 自己定义队列名称 myqueue: boot-activemq-queue
- 配置bean
package com.atguigu.boot.activemq.config; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.annotation.EnableJms; import javax.jms.Queue; @Configuration // 开启jms(java message service) @EnableJms public class ConfigBean { @Value("${myqueue}") private String myqueue; @Bean public Queue queue(){ return new ActiveMQQueue(myqueue); } }
- queue消息生产者
package com.atguigu.boot.activemq.produce; import lombok.extern.slf4j.Slf4j; 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; @Component @Slf4j public class Queue_Produce { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; // 调用一次投放一次消息 public void produceMsg(){ jmsMessagingTemplate.convertAndSend(queue, "******: " + UUID.randomUUID().toString().substring(0,6)); } // 间隔时间定投消息 @Scheduled(fixedDelay = 3000L) public void produceMsgScheduled(){ jmsMessagingTemplate.convertAndSend(queue, "******: " + UUID.randomUUID().toString().substring(0,6)); log.info("******produceMsgScheduled send ok"); } }
- 主启动类(使用定时投放直接开启主启动类即可)
package com.atguigu.boot.activemq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication // 开启定时投放消息 @EnableScheduling public class MainApp_Produce { public static void main(String[] args) { SpringApplication.run(MainApp_Produce.class, args); } }
- 测试单元(调用一次投放一次消息的测试类,定时投放直接开启主启动类)
package com.atguigu.boot.activemq; import com.atguigu.boot.activemq.produce.Queue_Produce; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import javax.annotation.Resource; //classes属性用来指定运行测试类需要装载的class 集合 @SpringBootTest(classes = MainApp_Produce.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestActiveMQ { @Resource private Queue_Produce queue_produce; @Test public void testSend() throws Exception{ queue_produce.produceMsg(); } }
-
消费者微服务
- application.yml
server: port: 8888 spring: activemq: broker-url: tcp://localhost:61616 user: admin password: admin jms: pub-sub-domain: false # false表示queue,true表示topic # 自己定义队列名称 myqueue: boot-activemq-queue
- queue消息消费者类
package com.atguigu.boot.activemq.consumer; import lombok.extern.slf4j.Slf4j; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import javax.jms.JMSException; import javax.jms.TextMessage; @Component @Slf4j public class Queue_Consumer { @JmsListener(destination = "${myqueue}") public void receive(TextMessage textMessage) throws JMSException{ log.info("******消费者消费消息: " + textMessage.getText()); } }
- 消费者微服务主启动类
package com.atguigu.boot.activemq; import org.springframework.boot.SpringApplication; public class MainApp_Consumer { public static void main(String[] args) { SpringApplication.run(MainApp_Consumer.class, args); } }
- springboot整合activeMQ之topic
-
jdbc持久化配置见activemq但是topic持久化还是没有成功,需要补充
-
pom.xml同上
-
topic生产者微服务
- application.yml
server: port: 6666 spring: activemq: broker-url: tcp://localhost:61616 user: admin password: admin jms: pub-sub-domain: true # true代表topic,false代表queue # 自己定义的主题名称 mytopic: boot-activemq-topic
- topic生产者配置bean
package com.atguigu.boot.activemq.topic.config; import org.apache.activemq.command.ActiveMQTopic; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.jms.Topic; @Configuration public class ConfigBean { @Value("${mytopic}") private String mytopic; @Bean public Topic topic(){ return new ActiveMQTopic(mytopic); } }
- topic消息生产者
package com.atguigu.boot.activemq.topic.produce; import lombok.extern.slf4j.Slf4j; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.jms.Topic; import java.util.UUID; @Component @Slf4j public class Topic_Produce { @Resource private JmsMessagingTemplate jmsMessagingTemplate; @Resource private Topic topic; // 每3s钟发送一次消息 @Scheduled(fixedDelay = 3000L) public void productTopic(){ jmsMessagingTemplate.convertAndSend(topic, "topic消息:" + UUID.randomUUID().toString().substring(0, 6)); } }
- topic生产者主启动类
package com.atguigu.boot.activemq.topic; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class MainApp_TopicProduce { public static void main(String[] args) { SpringApplication.run(MainApp_TopicProduce.class, args); } }
-
topic消费者微服务
- pom.xml同上
- application.yml
server: port: 5555 # 启动第一个topic消费者为5555端口,启动第二个topic消费者改成5566端口 spring: jms: pub-sub-domain: true activemq: broker-url: tcp://localhost:61616 user: admin password: admin # 自定义topic名称 mytopic: boot-activemq-topic
- topic消息消费者类
package com.atguigu.boot.activemq.consumer; import lombok.extern.slf4j.Slf4j; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import javax.jms.JMSException; import javax.jms.TextMessage; @Component @Slf4j public class Queue_Consumer { @JmsListener(destination = "${mytopic}") public void receive(TextMessage textMessage) throws JMSException { log.info("topic消费者收到的消息: " + textMessage.getText()); } }
- topic消费主启动类
package com.atguigu.boot.activemq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication public class MainApp_Topic5555 { public static void main(String[] args) { SpringApplication.run(MainApp_Topic5555.class, args); } }
package com.atguigu.boot.activemq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication public class MainApp_Topic5566 { public static void main(String[] args) { SpringApplication.run(MainApp_Topic5566.class, args); } }