SpringBoot2.0之整合ActiveMQ(點對點模式)
阿新 • • 發佈:2018-11-10
<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.toov5.springboot.avtivemq</groupId> <artifactId>springboot.avtivemq</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web元件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot Activemq --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> </dependencies> <!-- 注意: 這裡必須要新增, 否者各種依賴有問題 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
沒有版本號,表示springboot已經整合好了
專案結構:
yml:
spring: activemq: broker-url: tcp://192.168.91.6:61616 user: admin password: admin my_queue: springboot-queue-toov5 server: port: 8080
config:
package com.toov5.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 ConfigQueue { @Value("${my_queue}") private String myQueue; //首先將佇列注入到SpringBoot容器中去 @Bean public Queue queue() { return new ActiveMQQueue(myQueue); } }
producer
package com.toov5.Producer; 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 P2PProducer { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; //把佇列注入進來 @Autowired //此註解預設是以型別找 在配置檔案中 已經注入的 @Bean private Queue queue; //每隔5s時間向佇列傳送訊息 @Scheduled(fixedDelay=5000) //每間隔2s向佇列傳送訊息 public void send() { String msgString = System.currentTimeMillis()+" "; jmsMessagingTemplate.convertAndSend(queue,msgString); System.out.println("點對點通訊,msg"+msgString); } }
啟動類:
package com.toov5; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling //開啟定時任務 public class AppProducer { public static void main(String[] args) { SpringApplication.run(AppProducer.class, args); } }
執行:
一直在增加
思路總結: queue 注入到springboot容器, 然後producer 去取出來 spring定時任務 5s定時寫入訊息
然後建立另外一個Consumer 的maven 專案:
pom:
<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.springboot.activemqConsumer</groupId> <artifactId>activemqConsumer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web元件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot Activemq --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> </dependencies> <!-- 注意: 這裡必須要新增, 否者各種依賴有問題 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
yml:
spring: activemq: broker-url: tcp://192.168.91.6:61616 user: admin password: admin my_queue: springboot-queue-toov5 server: port: 8080
consumer:
package com.toov5.activemqConsumer; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class P2PConsumer { @JmsListener(destination= "${my_queue}") //用這個註解去監聽 監聽的佇列 public void receiver(String msg) { System.out.println("消費者成功獲取到生產者的訊息,msg"+msg); } }
啟動類:
package com.toov5.activemqConsumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AppConsumer { public static void main(String[] args) { SpringApplication.run(AppConsumer.class, args); } }
修改為兩個不同的埠一起跑: