最近想要学习MOM(消息中间件:Message Oriented Middleware),就从比较基础的activeMQ学起,rabbitMQ、zeroMQ、rocketMQ、Kafka等后续再去学习。

上面说activeMQ是一种消息中间件,可是为什么要使用activeMQ?

在没有使用JMS的时候,很多应用会出现同步通信(客户端发起请求后需要等待服务端返回结果才能继续执行)、客户端服务端耦合、单一点对点(P2P)通信的问题,JMS可以通过面向消息中间件的方式很好的解决了上面的问题。

JMS规范术语:

Provider/MessageProvider:生产者

Consumer/MessageConsumer:消费者

消息形式: 
1、点对点(queue) 
2、一对多(topic)

ConnectionFactory:连接工厂,JMS用它创建连接

Connnection:JMS Client到JMS Provider的连接

Destination:消息目的地,由Session创建

Session:会话,由Connection创建,实质上就是发送、接受消息的一个线程,因此生产者、消费者都是Session创建的

我这里安装的是Windows版本的,安装好了之后就是这样的目录

到bin目录下,启动activemq.bat

这样就启动成功了。

访问http://localhost:8161/admin/index.jsp可以看到管控台,如下图:

spring boot整合activeMQ:

pom.xml中引用

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<!-- <version>5.7.0</version> -->
</dependency>

在application.properties中配置activeMQ连接:

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false

创建消息生产者:

/**
* @author huangzhang
* @description
* @date Created in 2018/7/16 18:57
*/
@Service("provider")
public class Provider {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate; public void sendMessage(Destination destination, final String message){
jmsMessagingTemplate.convertAndSend(destination,message); }
  //消费消费者返回的队列"return.queue"中的消息
@JmsListener(destination="return.queue")
public void consumerMessage(String string){
System.out.println("从return.queue队列收到的回复报文为:"+string);
} }

创建第一个消费者:

/**
* @author huangzhang
* @description
* @date Created in 2018/7/16 19:31
*/
@Component
public class Consumer {
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text){
System.out.println("Consumer收到的报文为:"+text);
}
}

创建第二个消费者(这里不光消费了生产者插入队列中的message,而且将返回值插入到了"return.queue"队列中):

/**
* @author huangzhang
* @description
* @date Created in 2018/7/16 19:33
*/
@Component
public class Consumer1 {
@JmsListener(destination = "mytest.queue")
@SendTo("return.queue")
public String receiveQueue(String message){
System.out.println("Consumer1收到的报文为:"+message);
return "========return message "+message;
}
}

测试方法:

@Service
public class SpringbootJmsApplicationTests {
@Autowired
private Provider provider; public void contextLoads() throws InterruptedException {
Destination destination = new ActiveMQQueue("mytest.queue");
for(int i=0; i<10; i++){
provider.sendMessage(destination, "huangzhang "+i);
}
} }

这里我在controller中调用了测试方法:

/**
* @author huangzhang
* @description
* @date Created in 2018/7/16 20:23
*/
@Controller
public class Test {
@Autowired
SpringbootJmsApplicationTests springbootJmsApplicationTests;
@RequestMapping("/")
@ResponseBody
public String test01()throws Exception{ springbootJmsApplicationTests.contextLoads(); return "success!";
}
}

访问http://localhost:8080/对此demo进行测试

这里是执行结果,可以看出,两个消费者分别消费了生产者放入消息队列中的消息,并且Consumer1消费者将返回结果放入了队列中供生产者消费。

查看Queues

这里可以看出我们生产者循环往mytest.queue队列中写入10次,由两个消费者消费了这10次消息

consumer1消费了5次消息,并往返回队列return.queue中写入5次,由原生产者消费了者5次消息

到这里一个简单的spring boot整合activeMQ的一对一(queue)模式的demo就完成了(请多指正)。

下面我们对provider和consumer进行改造,实现多对多(topic)模式:

对test类进行修改,修改为以下代码:

@Service
public class SpringbootJmsApplicationTests {
@Autowired
private Provider provider;
@Autowired
private TopicSender topicSender; /*public void contextLoads() throws Exception {
Destination destination = new ActiveMQQueue("mytest.queue");
for(int i=0; i<10; i++){
provider.sendMessage(destination, "huangzhang "+i);
}
}*/
public void topicSend(){
Destination destination = new ActiveMQTopic("test.topic");
for (int i = 0; i < 10 ; i++){
provider.sendMessage(destination, "topic"+i);
}
} }

订阅者为:

/**
* @author huangzhang
* @description
* @date Created in 2018/7/16 19:31
*/
@Component
public class Consumer {
/*@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text){
System.out.println("Consumer收到的报文为:"+text);
}*/ @JmsListener(destination = "test.topic")
public void receiveQueue1(String text){
System.out.println("Consumer收到的----topic----报文为:"+text);
}
}

两个订阅者修改方式一样。

然后像queue一样,通过controller调用test方法:

/**
* @author huangzhang
* @description
* @date Created in 2018/7/10 20:23
*/
@Controller
public class Test {
@Autowired
SpringbootJmsApplicationTests springbootJmsApplicationTests;
@RequestMapping("/")
@ResponseBody
public String test01()throws Exception{
// springbootJmsApplicationTests.contextLoads();
springbootJmsApplicationTests.topicSend(); return "success!";
}
}

同样的启动项目之后调用http://localhost:8080/接口,控制台打印如下:

我们发现,这里consumer和consumer1两个订阅者都收到了10条订阅的"test.topic"消息,再次证明:

生产者发送一条消息到queue,只有一个消费者能收到;

发布者发送到topic的消息,只要订阅了topic的订阅者就会收到消息。

activeMQ入门+spring boot整合activeMQ的更多相关文章

  1. spring boot整合activemq消息中间件

    spring boot整合activemq消息中间件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  2. spring boot 整合activemq

    1 Spring Boot与ActiveMQ整合 1.1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <properties> <spring.version& ...

  3. Spring Boot 整合 ActiveMQ

    依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  4. liunx 安装ActiveMQ 及 spring boot 初步整合 activemq

    源码地址:  https://gitee.com/kevin9401/microservice.git 一.安装 ActiveMQ: 1. 下载 ActiveMQ wget  https://arch ...

  5. ActiveMQ与Spring / SpringBoot 整合(四)

    1. 对 Spring 的整合 1.1 所需jar 包 <!-- activeMQ jms 的支持 --> <dependency> <groupId>org.sp ...

  6. Spring Boot入门 and Spring Boot与ActiveMQ整合

    1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...

  7. Spring Boot与ActiveMQ整合

    Spring Boot与ActiveMQ整合 1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <dependency> <groupId>org.spri ...

  8. spring boot集成activemq

    spring boot集成activemq 转自:https://blog.csdn.net/maiyikai/article/details/77199300

  9. Spring boot 集成ActiveMQ(包含双向队列实现)

    集百家之长,成一家之言.  1. 下载ActiveMQ https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.15.9/apache-activ ...

随机推荐

  1. yii2项目实战-路由美化以及如何正确的生成链接

    yii2项目实战-路由美化以及如何正确的生成链接 更新于 2016年12月17日 by 白狼 被浏览了 705 次 美化路由 何为美化路由呢?美化嘛,无外乎就是给路由化化妆,让她好看点.我虽没化过妆, ...

  2. Java 栈与堆简介

    一.前言 长久以来,一直被Java的内存分配问题,堆和栈问题困扰好久,面试的时候也非常心虚,这几天好好通过看书和技术博客来整理了一下,希望能找到我自己的理解方式. 二.内存 内存分物理内存和虚拟内存, ...

  3. 编写高质量代码改善C#程序的157个建议——建议135: 考虑使用肯定性的短语命名布尔属性

    建议135: 考虑使用肯定性的短语命名布尔属性 布尔值无非就是True和False,所以应该用肯定性的短语来表示它,例如,以Is.Can.Has作为前缀. 布尔属性正确命名的一个示例如下: class ...

  4. spring mvc 入门程序

    入门程序 1.环境准备 myeclipse Spring jar 2.前端控制器设置 (web.xml) 所有的*.action请求通过org.springframework.web.servlet. ...

  5. Android-广播发送与接收(Java代码中订阅)

    Android四大组件之一广播,使用的也比较多,广播可大致分为两种,一种是Android系统区域的广播,是由系统指令发出,例如:点亮屏幕广播,开机过程中的一些广播 省略…, 然而还有一种广播就是我们自 ...

  6. [LeetCode 题解]:Gray Code

    题目描述: The gray code is a binary numeral system where two successive values differ in only one bit. G ...

  7. IIS 发布webservice 需要用户名和密码访问 解决

    今天,我在IIS上发布了一个自己写的webservice,然后我在远程通过浏览器来访问这个webservice的时候出现一个登录界面如下  之前我朋友发布webservice的时候也出现过一次,那次好 ...

  8. 在一台服务器上搭建相对高可用HiveServer实践

    本文来自网易云社区 作者:刘杰 问题及原因 组里之前搭建了一个hiveserver提供给猛犸上的ETL程序调用,hiveserver隔三差五地挂掉重启,网上查了些资料,也咨询了猛犸那边维护hivese ...

  9. HAOI2010 最长公共子序列

    题目链接:戳我 30分暴力....暴力提取子序列即可qwqwq #include<iostream> #include<cstdio> #include<algorith ...

  10. 如何使用安卓4.4的SD卡?

    安卓4.4默认情况下,后安装的程序无权写入数据到SD卡中,那么是否我们就不能用了?看了很多文章,都说要Root,随后修改配置文件.我觉得这不是很好的方法,Root之后的安卓会有很大风险,这不是最好的办 ...