原文連接:https://blog.csdn.net/jia_costa/article/details/79354478java
新建springboot項目, pom文件以下web
<?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.example</groupId>
<artifactId>activemq-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>activemq-test</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置類spring
package com.example.config; import javax.jms.ConnectionFactory; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; @Configuration public class ActivemqConfig { /** * 自定義了4個Destination,兩個queue,兩個topic */ @Bean public Destination queue1() { return new ActiveMQQueue("queue-1"); } @Bean public Destination queue2() { return new ActiveMQQueue("queue-2"); } @Bean public Destination topic1() { return new ActiveMQTopic("topic-1"); } @Bean public Destination topic2() { return new ActiveMQTopic("topic-2"); } /** * JmsListener註解默認只接收queue消息,若是要接收topic消息,須要設置containerFactory */ @Bean public JmsListenerContainerFactory<?> topicListenerContainer(ConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory topicListenerContainer = new DefaultJmsListenerContainerFactory(); topicListenerContainer.setPubSubDomain(true); topicListenerContainer.setConnectionFactory(activeMQConnectionFactory); return topicListenerContainer; } } controller package com.example.controller; import javax.annotation.Resource; import javax.jms.Destination; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 經過頁面訪問的方式生產消息 */ @RestController public class ProviderController { @Autowired private JmsTemplate jmsTemplate; @Resource(name = "queue1") private Destination queue1; @Resource(name = "topic1") private Destination topic1; @GetMapping("/send/queue1") public String send1() { jmsTemplate.convertAndSend(queue1, "hello queue-1"); return "ok"; } @GetMapping("/send/topic1") public String send2() { jmsTemplate.convertAndSend(topic1, "hello topic-1"); return "ok"; } }
接收消息apache
package com.example.message; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; /** * 接收處理消息 */ @Component public class MessageHandler { @JmsListener(destination="queue-1") public void recieve1(String message) { System.out.println("###################" + message + "###################"); } @JmsListener(destination="topic-1", containerFactory="topicListenerContainer") public void recieve2(String message) { System.out.println("###################" + message + "###################"); } }
配置文件application.propertiesspringboot
spring.activemq.broker-url=tcp://192.168.32.128:61616app
啓動類maven
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ActivemqTestApplication { public static void main(String[] args) { SpringApplication.run(ActivemqTestApplication.class, args); } }
訪問http://localhost:8080/send/queue1或者http://localhost:8080/send/topic1,能夠看到控制檯的輸出
tcp