ActiveMQ-模块代码-02
模块模式
p2p模式
生产者

ConfigBeanQueue
package com.producerp2p.producerp2p; 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; // 配置类--配置文件(Spring的配置文件)
@Configuration
@EnableJms
public class ConfigBeanQueue { // 属性注入的方式
@Value("${queue-boot-name}")
private String queueName; // 创建队列Queue并设置队列名称
@Bean
public Queue createQueue(){
return new ActiveMQQueue(queueName);
}
}
QueueProducer
package com.producerp2p.producerp2p; 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; @Component
public class QueueProducer { // 属性注入
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate; @Autowired
private Queue queue; // 发送消息
public void sendMsg(){
jmsMessagingTemplate.convertAndSend(queue,"QueueProducer发送消息到对列");
} // 定时发送消息: 每隔3秒发送一条消息 微服务项目启动的时候自动执行使用@Scheduled修饰的方法
@Scheduled(fixedDelay = 3000l)
public void sendMsgScheduled(){
jmsMessagingTemplate.convertAndSend(queue,"QueueProducer发送定时消息到队列");
System.out.println("-------定时消息发送完成-------");
} }
Producerp2pApplication
package com.producerp2p.producerp2p; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling
public class Producerp2pApplication { public static void main(String[] args) {
SpringApplication.run(Producerp2pApplication.class, args);
} }
application.properties
server.port=7777
# 配置activemq连接,url
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
# activemq的模式queue(false),topic(true)
spring.jms.pub-sub-domain=false
# 队列名称
queue-boot-name=newqueue
Producerp2pApplicationTests
package com.producerp2p.producerp2p; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest
class Producerp2pApplicationTests { @Autowired
private QueueProducer queueProducer; @Test
void testSendMsg() {
queueProducer.sendMsg();
System.out.println("------消息发送到队列完成-------");
} }
消费者

application.properties
server.port=7778
# 配置activemq连接,url
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
# activemq的模式queue(false),topic(true)
spring.jms.pub-sub-domain=false
# 队列名称
queue-boot-name=newqueue
QueueConsumer
package com.consumerp2p.consumerp2p; import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; import javax.jms.JMSException;
import javax.jms.TextMessage; @Component
public class QueueConsumer { // 接收消息---设置消费者监听的目的地(队列), 如果监听到队列中有消息,通过方法的形参表示接收到的消息
// Message:TextMessage
@JmsListener(destination = "${queue-boot-name}")
public void receiveMsg(TextMessage textMessage) throws JMSException {
System.out.println("p2p模式下的消费者1接收到队列中的消息:"+textMessage.getText()); }
}
Consumerp2pApplication
Consumerp2pApplication11
package com.consumerp2p.consumerp2p; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Consumerp2pApplication11 { public static void main(String[] args) {
SpringApplication.run(Consumerp2pApplication11.class, args);
} }
------------------
----------------------------------------------------------------------------------
----------------------
Topic模式
生产者

ConfigBeanTopic
package com.producertopic.producertopic; import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
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.Topic; @Configuration
@EnableJms
public class ConfigBeanTopic {
// 注入主题属性
@Value("${topic-boot-name}")
private String topicName; // 创建主题并设置主题名称
@Bean
public Topic createTopic(){
return new ActiveMQTopic(topicName);
} }
ProducerTopic
package com.producertopic.producertopic; 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.Topic; @Component
public class ProducerTopic { // 属性注入
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate; @Autowired
private Topic topic; // 定时发送消息
@Scheduled(fixedDelay = 2000l)
public void sendMsg(){
jmsMessagingTemplate.convertAndSend(topic,"发送定时消息到主题");
System.out.println("-----主题消息发送完成------");
} }
ProducertopicApplication
package com.producertopic.producertopic; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling
public class ProducertopicApplication { public static void main(String[] args) {
SpringApplication.run(ProducertopicApplication.class, args);
} }
application.properties
server.port=7779
# 配置activemq连接,url
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
# activemq的模式queue(false),topic(true)
spring.jms.pub-sub-domain=true
# 主题名称
topic-boot-name=newtopic
消费者

TopicConsumer
package com.consumertopic.consumertopic; import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; import javax.jms.JMSException;
import javax.jms.TextMessage; @Component
public class TopicConsumer { // 接收消息---设置消费者监听的目的地(队列), 如果监听到队列中有消息,通过方法的形参表示接收到的消息
// Message:TextMessage
@JmsListener(destination = "${topic-boot-name}")
public void receiveMsg(TextMessage textMessage) throws JMSException {
System.out.println("topic下的消费者接收到主题中的消息:"+textMessage.getText());
}
}
application.properties
server.port=7776
# 配置activemq连接,url
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
# activemq的模式queue(false),topic(true)
spring.jms.pub-sub-domain=true
# 主题名称
topic-boot-name=newtopic
ConsumertopicApplication
package com.consumertopic.consumertopic; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ConsumertopicApplication { public static void main(String[] args) {
SpringApplication.run(ConsumertopicApplication.class, args);
} }
ConsumertopicApplication2
package com.consumertopic.consumertopic; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ConsumertopicApplication2 { public static void main(String[] args) {
SpringApplication.run(ConsumertopicApplication2.class, args);
} }
public class ProducertopicApplication {
ActiveMQ-模块代码-02的更多相关文章
- Verilog HDL基础语法讲解之模块代码基本结构
Verilog HDL基础语法讲解之模块代码基本结构 本章主要讲解Verilog基础语法的内容,文章以一个最简单的例子"二选一多路器"来引入一个最简单的Verilog设计文件的 ...
- 彻底告别加解密模块代码拷贝-JCE核心Cpiher详解
前提 javax.crypto.Cipher,翻译为密码,其实叫做密码器更加合适.Cipher是JCA(Java Cryptographic Extension,Java加密扩展)的核心,提供基于多种 ...
- MDU某产品OMCI模块代码质量现状分析
说明 本文参考MDU系列某产品OMCI模块现有代码,提取若干实例以说明目前的代码质量,亦可作为甄别不良代码的参考. 本文旨在就事论事,而非否定前人(没有前人的努力也难有后人的进步).希望以史为鉴,不破 ...
- 【VS外接程序】利用T4模板生成模块代码
引言 记得第一次做asp.net mvc项目时,可以用model直接生成Html的增删改查页面, 没什么特殊要求都可以不用修改直接用了, 觉得很神奇,效率太高了.后来在做客户端开发时,发现很多模块都是 ...
- Excel VBA在生成副本的工作表中插入本工作簿中的VBA模块代码
即在工作簿中添加一个工作表,然后移出并存为新的工作簿,在移出前将本工作簿的一个模块的代码拷贝至新的工作簿.下面是关键代码: '===================================== ...
- OSGi-入门篇之模块层(02)
1 什么是模块化 模块层是OSGi框架中最基础的一部分,其中Java的模块化特性在这一层得到了很好的实现.但是这种实现与Java本身现有的一些模块化特性又有明显的不同. 在OSGi中模块的定义可以参考 ...
- Zepto.js库touch模块代码解析
Zepto.js也许并不陌生,专门针对移动端开发,Zepto有一些基本的触摸事件可以用来做触摸屏交互(tap事件.swipe事件),Zepto是不支持IE浏览器的. 下面来解析一些Zepto.js触摸 ...
- 问题解决:Spyder不支持OpenCV模块代码提示
在使用中遇到的问题是,Spyder的代码完成功能不支持某些编译模块(.pyd后缀),如OpenCV的Python模块cv/cv2,在编写脚本文件时,在已存在import cv&import c ...
- Linux-3.0.8中基于S5PV210的GPIO模块代码追踪和分析
编写按键驱动时,想知道内核是如何管理GPIO的,所以开始追踪代码,中间走了一些弯路,现记录于此. 追踪代码之前,我猜测:第一,这部分代码应该在系统set up阶段执行:第二,GPIO的代码应该在mac ...
- verilog HDL -模块代码基本结构
1--verilog HDL 语言的预编译指令作用:指示在编译verliog HDL源代码前,需要执行哪些操作. 2--模块内容是嵌在module 和endmodule两个语句之间.每个模块实现特定的 ...
随机推荐
- 简单实现支付密码输入框 By HL
密码输入框在微信,支付宝中比较常见 主要功能点 1.6位(或者N位)密码输入框封装
- CSS3带你实现3D转换效果
前言 在css3中允许使用3D转换来对元素进行格式化,在原本只是2D转化的平面中引入了Z轴.在这之前我们讲解了css3中的2D转换,也就是二维空间变换,本篇的3D转换就是基于原来的2D转换而来,与2D ...
- 一加6刷入kali nethunter
Installing Kali NetHunter On the OnePlus 6 准备工具: adb: https://jingyan.baidu.com/article/22fe7cedf67e ...
- 4.Flink实时项目之数据拆分
1. 摘要 我们前面采集的日志数据已经保存到 Kafka 中,作为日志数据的 ODS 层,从 kafka 的ODS 层读取的日志数据分为 3 类, 页面日志.启动日志和曝光日志.这三类数据虽然都是用户 ...
- 十年OI一场空,不开long long见祖宗
//线段树:单点修改+区间求和 #include<bits/stdc++.h> #define ll unsigned long long using namespace std; ll ...
- 从MVC到DDD的架构演进
DDD这几年越来越火,资料也很多,大部分的资料都偏向于理论介绍,有给出的代码与传统MVC的三层架构差异较大,再加上大量的新概念很容易让初学者望而却步.本文从MVC架构角度来讲解如何演进到DDD架构. ...
- Solution -「CF 575G」Run for beer
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \(m\) 条边的无向图,边有边权,一个人初始速度为 \(1\),每走一条边速度 \(\div10\), ...
- Visual Studio 2017-2019版本创建C#项目时没有创建网站这一选项?
通过了解以后发现Visual Studio 2017之后的版本在新建选项中已经不再有这一选择项了. 解决办法: 1.在创建新项目的面板滑倒最下面,---> 安装多个人工具和功能 2.这时已经打开 ...
- “百度杯”CTF比赛 九月场Upload
首先生成一个php文件以下源代码: <script language="PHP">$fh=fopen("../flag.".strtolower(& ...
- Kubernetes云供应商架构的未来
首先,我想分享SIG的使命,因为我们用它来指导我们现在和将来的工作.从我们的章程中直接来看,SIG的使命是简化,开发和维护云供应商集成,作为Kubernetes集群的扩展或附加组件.这背后的动机是双重 ...