当前位置: 首页 >服务端 > SpringBoot2.0之整合ActiveMQ【点对点模式】

SpringBoot2.0之整合ActiveMQ【点对点模式】

<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>

 

SpringBoot2.0之整合ActiveMQ【点对点模式】 _ JavaClub全栈架构师技术笔记

没有版本号,表示springboot已经整合好了

项目结构:

SpringBoot2.0之整合ActiveMQ【点对点模式】 _ JavaClub全栈架构师技术笔记

yml:

spring:  activemq:broker-url: tcp://192.168.91.6:61616user: adminpassword: adminmy_queue: springboot-queue-toov5server:  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;@Componentpublic class ConfigQueue {@Value("${my_queue}")private String myQueue;//首先将队列注入到SpringBoot容器中去@Beanpublic Queue queue() {retu 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;@Componentpublic class P2PProducer {@Autowiredprivate 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);}}

运行:

SpringBoot2.0之整合ActiveMQ【点对点模式】 _ JavaClub全栈架构师技术笔记

一直在增加

SpringBoot2.0之整合ActiveMQ【点对点模式】 _ JavaClub全栈架构师技术笔记

思路总结: queue 注入到springboot容器, 然后producer 去取出来 spring定时任务 5s定时写入消息

 

然后创建另外一个Consumer 的maven 项目:

SpringBoot2.0之整合ActiveMQ【点对点模式】 _ JavaClub全栈架构师技术笔记

 

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:61616user: adminpassword: adminmy_queue: springboot-queue-toov5server:  port: 8080

consumer:

package com.toov5.activemqConsumer;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;@Componentpublic 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;@SpringBootApplicationpublic class AppConsumer {public static void main(String[] args) {SpringApplication.run(AppConsumer.class, args);}}

 

 修改为两个不同的端口一起跑:

SpringBoot2.0之整合ActiveMQ【点对点模式】 _ JavaClub全栈架构师技术笔记

 

 

 

作者:toov5
来源链接:https://www.cnblogs.com/toov5/p/9937989.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/113094.html

标签:ActiveMQ
分享给朋友:

“SpringBoot2.0之整合ActiveMQ【点对点模式】” 的相关文章