模块模式

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的更多相关文章

  1. Verilog HDL基础语法讲解之模块代码基本结构

    Verilog HDL基础语法讲解之模块代码基本结构   本章主要讲解Verilog基础语法的内容,文章以一个最简单的例子"二选一多路器"来引入一个最简单的Verilog设计文件的 ...

  2. 彻底告别加解密模块代码拷贝-JCE核心Cpiher详解

    前提 javax.crypto.Cipher,翻译为密码,其实叫做密码器更加合适.Cipher是JCA(Java Cryptographic Extension,Java加密扩展)的核心,提供基于多种 ...

  3. MDU某产品OMCI模块代码质量现状分析

    说明 本文参考MDU系列某产品OMCI模块现有代码,提取若干实例以说明目前的代码质量,亦可作为甄别不良代码的参考. 本文旨在就事论事,而非否定前人(没有前人的努力也难有后人的进步).希望以史为鉴,不破 ...

  4. 【VS外接程序】利用T4模板生成模块代码

    引言 记得第一次做asp.net mvc项目时,可以用model直接生成Html的增删改查页面, 没什么特殊要求都可以不用修改直接用了, 觉得很神奇,效率太高了.后来在做客户端开发时,发现很多模块都是 ...

  5. Excel VBA在生成副本的工作表中插入本工作簿中的VBA模块代码

    即在工作簿中添加一个工作表,然后移出并存为新的工作簿,在移出前将本工作簿的一个模块的代码拷贝至新的工作簿.下面是关键代码: '===================================== ...

  6. OSGi-入门篇之模块层(02)

    1 什么是模块化 模块层是OSGi框架中最基础的一部分,其中Java的模块化特性在这一层得到了很好的实现.但是这种实现与Java本身现有的一些模块化特性又有明显的不同. 在OSGi中模块的定义可以参考 ...

  7. Zepto.js库touch模块代码解析

    Zepto.js也许并不陌生,专门针对移动端开发,Zepto有一些基本的触摸事件可以用来做触摸屏交互(tap事件.swipe事件),Zepto是不支持IE浏览器的. 下面来解析一些Zepto.js触摸 ...

  8. 问题解决:Spyder不支持OpenCV模块代码提示

    在使用中遇到的问题是,Spyder的代码完成功能不支持某些编译模块(.pyd后缀),如OpenCV的Python模块cv/cv2,在编写脚本文件时,在已存在import cv&import c ...

  9. Linux-3.0.8中基于S5PV210的GPIO模块代码追踪和分析

    编写按键驱动时,想知道内核是如何管理GPIO的,所以开始追踪代码,中间走了一些弯路,现记录于此. 追踪代码之前,我猜测:第一,这部分代码应该在系统set up阶段执行:第二,GPIO的代码应该在mac ...

  10. verilog HDL -模块代码基本结构

    1--verilog HDL 语言的预编译指令作用:指示在编译verliog HDL源代码前,需要执行哪些操作. 2--模块内容是嵌在module 和endmodule两个语句之间.每个模块实现特定的 ...

随机推荐

  1. rpm与yum安装及管理程序

    安装及管理程序 1.Linux应用程序基础 2.RPM软件包管理工具 3.yum源仓库创建 1.应用程序与系统命令的关系如图:  典型应用程序的目录结构如图: 常见的软件包封装类型如图: 2.RPM包 ...

  2. Linux忘记Root密码怎么找回

    进入1级别,单用户模式 ,修改root密码即可(运行级别不懂看这里) 具体操作如下: 1.开机时按enter键 2.进入GRUB界面 3.输入 e,在引导系统前编辑命令 4.选择第二行 kernel ...

  3. 系统基础优化( 创建yum私有仓库最详细操作及解释 )

    目录 系统基础优化 一.Linux中安装软件的方式 安装方式 三种安装方式的区别 二.RPM安装☆ 1.安装及其他命令 2.手动下载软件包,可将其拖入shell中自动传入 1).手动网页下载软件包 2 ...

  4. Solution -「HNOI 2009」「洛谷 P4727」图的同构计数

    \(\mathcal{Description}\)   Link.   求含 \(n\) 个点的无标号简单无向图的个数,答案模 \(997\). \(\mathcal{Solution}\)   首先 ...

  5. VS2019如何设置程序以管理员权限启动

    最重要的一点.本文解释的是C#项目如何以管理员权限启动. 一个很大的误导项 该图片是C++程序的项目配置属性.C#项目中并找不到.然而网上的很多教程没有说清楚.导致我找了这个菜单找了很久. C#项目的 ...

  6. Apache-log4j漏洞复现

    前言:昨天晚上当我还在睡梦中时,圈内爆出了核弹级的漏洞,今天我复现一下, 再开始前我们先建立一个maven项目,将pom.xml文件导入 <?xml version="1.0" ...

  7. nessus安装破解

    Nessus2.0-20211012插件包 Nessus-8.15.2-x64.msi 0x01 Nessus更新介绍 Nessus下载地址 1https://www.tenable.com/down ...

  8. ensp上防火墙上配置nat

    博文大纲:一.华为防火墙NAT的六个分类:二.解决NAT转换时的环路及无效ARP:三.server-map表的作用:四.NAT对报文的处理流程:五.各种常用NAT的配置方法: 一.华为防火墙NAT的六 ...

  9. RFC2544背靠背测试——信而泰Renix测试软件实操

    文章关键词:背靠背测试.合法最小帧间隙.缓存区结构.吞吐量测试. 背靠背测试背景: 随着网络规模的扩大,大量的路由更新消息.频繁的文件传输和数据备份等操作都会导致数据在一段时间内急剧增加,甚至达到该物 ...

  10. 【C#版本】微信公众号模板消息对接(一)(图文详解)

    特此说明:本篇文章为个人原创文章,创作不易,未经作者本人同意.许可等条件,不得以任何形式搬运.转载.抄袭(等包括但不限于此手段)本文章,否则保留追究有关侵权人责任的权利 一.认识微信公众号模板消息 什 ...