最近学习到ActiveMQ,之前也没有用过相关或者类似的工具,因此特地写个文章进行相关的学习记录。

相关参考博文:https://www.cnblogs.com/cyfonly/p/6380860.htmlhttps://blog.csdn.net/qq_26641781/article/details/80408987https://blog.csdn.net/qinweili751/article/details/80620104

1.安装ActiveMQ

(1)进入官网http://activemq.apache.org/,选择最新的版本下载

(2)再选择对应的系统环境(我这里选择的是windows版本)

(3)下载完成后将其解压(我这里将它存放在D盘根目录下),目录结构如下

(4)进入bin/win64/目录,启动activemq.bat文件(注意:MQ与jdk版本必须要匹配。我这里下载的MQ是5.15版本,对应的jdk最低要求是1.8)。

(5)启动完成后,输入浏览器地址http://localhost:8161/admin,会弹出用户名/密码输入框

我们的账号密码是存放在ActiveMQ根目录的conf/jetty-realm.properties文件中。

打开可以看到最下面已经有两个创建好了的用户了。如果我们需要添加自己的用户,或是修改它们的角色,都可以按照上面所写的格式"用户名:密码 [,角色]"来进行配置(角色被定义在~/conf/jetty.xml中)。

这里我们使用默认帐号,admin/admin

(6)至此,我们的ActiveMQ已经安装完成。

2.使用ActiveMQ

  • ActiveMQ的使用一般分为以下几个步骤:
  • connectionFactory:创建连接工厂;
  • connection:从连接工厂中得到连接;
  • session:从连接中获得一个会话;
  • destination:从会话中获取一个destination。可以是Queue(P2P)或Topic(Pub/Sub)
  • Producer:根据session和destination创建服务生产者。
  •   Message:根据session创建消息。
  •   send:消息生产者将message发送给MQ
  • Consumer:根据session和destination创建服务消费者。
  •   receive:接收MQ中的消息。可以是同步接收,也可以是创建监听器异步接收。
  • 关闭资源。

由于我这里是Springboot的项目,因此有部分步骤已经在自动配置中处理好了。

(1)引入依赖

        <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.15.</version>
</dependency>

(2)添加相关配置

#默认端口是61616,而不是我们访问网站的8161端口
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=false

(3)添加配置类(PS:如果不配置该类,默认只会使用P2P,即设置Queue为destination。如果要使用Topic,则必须要配置下面的类)。

@Configuration
public class JmsConfig { @Bean
public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(false); //Queue是P2P,因此Pub/Sub设置为false。默认是false。
factory.setConnectionFactory(connectionFactory);
return factory;
} @Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(true); //Topic是Pub/Sub,需要显示声明。
factory.setConnectionFactory(connectionFactory);
return factory;
} }

(4)编写服务消费者

这里为了能区别P2P和Pub/Sub,创建了两个服务消费者。

消费者1

@Service
public class ConsumerService { @JmsListener(destination = "springboot.queue.test", containerFactory = "jmsListenerContainerQueue")
public void receiveQueue(String msg) {
System.out.println(LocalDateTime.now().toString() + " consumer接收到Queue消息:" + msg);
} @JmsListener(destination = "springboot.topic.test", containerFactory = "jmsListenerContainerTopic")
public void receiveTopic(String msg) {
System.out.println(LocalDateTime.now().toString() + " consumer接收到Topic消息:" + msg);
} }

消费者2

@Service
public class Consumer2Service { @JmsListener(destination = "springboot.queue.test", containerFactory = "jmsListenerContainerQueue")
public void receiveQueue(String msg) {
System.out.println(LocalDateTime.now().toString() + " consumer2接收Queue消息:" + msg);
} @JmsListener(destination = "springboot.topic.test", containerFactory = "jmsListenerContainerTopic")
public void receiveTopic(String msg) {
System.out.println(LocalDateTime.now().toString() + " consumer2接收到Topic消息:" + msg);
}
}

(5)编写服务生产者

@Service
public class ProducerService { @Autowired
private JmsTemplate jmsTemplate; public void sendMessage(Destination destination, String msg) {
System.out.println(LocalDateTime.now().toString() + " productor发送消息:" + msg);
jmsTemplate.convertAndSend(destination, msg);
} }

(6)测试类

先测试P2P下的消息:

    @Test
public void testMQQueue() {
Destination destination = new ActiveMQQueue("springboot.queue.test");
for (int i = ; i < ; i++) {
producerService.sendMessage(destination, "hellow world " + i);
}
}

输出结果

2019-05-22T17:29:39.324 productor发送消息:hellow world 0
2019-05-22T17:29:39.373 consumer2接收Queue消息:hellow world 0
2019-05-22T17:29:39.379 productor发送消息:hellow world 1
2019-05-22T17:29:39.385 productor发送消息:hellow world 2
2019-05-22T17:29:39.388 consumer接收到Queue消息:hellow world 1
2019-05-22T17:29:39.391 consumer2接收Queue消息:hellow world 2

可以看到生产者每发出一个消息,都只会有一个消费者对消息进行处理。并且这里采用的是轮询的方式,即这次是消费者1接收了消息,下次就是消费者2接收,再下次又是消费者1。以此类推。

然后我们再测试下Pub/Sub的消息:

    @Test
public void testMQTopic() {
Destination destination = new ActiveMQTopic("springboot.topic.test");
for (int i = ; i < ; i++) {
producerService.sendMessage(destination, "hellow world " + i);
}
}

输出结果:

2019-05-22T17:35:58.535 productor发送消息:hellow world 0
2019-05-22T17:35:58.576 productor发送消息:hellow world 1
2019-05-22T17:35:58.581 consumer接收到Topic消息:hellow world 0
2019-05-22T17:35:58.582 consumer接收到Topic消息:hellow world 1
2019-05-22T17:35:58.582 consumer2接收到Topic消息:hellow world 0
2019-05-22T17:35:58.584 consumer2接收到Topic消息:hellow world 1

这里可以看到,每一个消息被发出来后,会被所有的服务消费者接收并处理。

ActiveMQ消息中间件的更多相关文章

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

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

  2. ActiveMQ消息中间件的作用以及应用场景

    ActiveMQ消息中间件的作用以及应用场景 一.ActiveMQ简介 ActiveMQ是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ是一个完全支持JMS1.1和J2EE1.4 ...

  3. ActiveMQ消息中间件Producer和Consumer

    ActiveMQ消息中间件Producer和Consumer 原创jethai2015-08-18 18:08:56评论(0)1480人阅读   生产者代码: 1 2 3 4 5 6 7 8 9 10 ...

  4. Apache ActiveMQ消息中间件的基本使用

    Apache ActiveMQ是Apache软件基金会所研发的开放源码消息中间件:由于ActiveMQ是一个纯Java程式,因此只需要操作系统支援Java虚拟机,ActiveMQ便可执行. 支持Jav ...

  5. JMS学习篇《一》ActiveMQ消息中间件的简单介绍与用法-概念篇

    原创说明:本篇博文为本人原创作品,转载请注明出处 1.何为消息中间件 消息中间件是一种在分布式应用中互相交换信息的一种技术,常见的成熟消息中间件有:RabbitMQ.SonicMQ,activeMQ. ...

  6. Kafka、RabbitMQ、RocketMQ、ActiveMQ消息中间件的对比--多年生产经验实践总结

    引言 分布式系统中,我们广泛运用消息中间件进行系统间的数据交换,便于异步解耦.现在开源的消息中间件有很多,前段时间我们自家的产品 RocketMQ (MetaQ的内核) 也顺利开源,得到大家的关注. ...

  7. 分布式--ActiveMQ 消息中间件(一) https://www.jianshu.com/p/8b9bfe865e38

    1. ActiveMQ 1). ActiveMQ ActiveMQ是Apache所提供的一个开源的消息系统,完全采用Java来实现,因此,它能很好地支持J2EE提出的JMS(Java Message ...

  8. 应用activeMQ消息中间件同步索引库

    mq是一个消息服务器: 安装包内置了tomcat,直接登录访问,登录:http://ip:8161/admin/    (相当于dubbo的moniter监控中心) admin admin传统串行化, ...

  9. activemq消息中间件的依赖

    <dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artif ...

随机推荐

  1. Ubuntu下使用SSH 命令用于登录远程桌面

    https://blog.csdn.net/yucicheung/article/details/79427578 问题描述 做DL的经常需要在一台电脑(本地主机)上写代码,另一台电脑(服务器,计算力 ...

  2. 19-11-1-N

    就剩一个键了…… 以后怎么办呢? 也许可以试试字符映射表……(滑稽 ZJ一下: 我还以为我要死了…… 40 Miemeng 10 03:21:50 80 03:21:51 10 03:21:51 10 ...

  3. <每日一题>题目10:求斐波拉契数列

    def func(x): m,n = 0,1 i = 0 while i < x: yield m m,n = n,m+n i += 1 fib = [] get_func = func(100 ...

  4. JavaWEB过滤器和监听器技术

    过滤器介绍 什么是过滤器 生活中的例子: 滤水器,口罩,杯子上滤网,渔网 生活中的过滤器:留下我们想要的,排除,我们不想要的. 高考: 只有分数够高的同学才能进入理想的大学.有一部分同学被拦截在大学之 ...

  5. mongodb什么时候使用

    转自:https://blog.csdn.net/justlpf/article/details/80392944 简介 MongoDB[1] 是一个基于分布式文件存储的数据库.由C 语言编写.旨在为 ...

  6. [Ceoi2007]Royaltreasury

    #1945. [Ceoi2007]Royaltreasury Online Judge:Bzoj-1945 Label:树形Dp,高精度 题目描述 在很久很久以前的一个王国里,王国的财产开始变得越来越 ...

  7. 跟我一起了解koa之koa-generator(一)

    cnpm install -g koa-generator koa2 -e koa2-learn cd koa2-learn/ cnpm install 使用如下运行 DEBUG=koa2-learn ...

  8. TP5隐藏index.php

    一,找到/public/.htaccess文件,如果你的入口文件已经移动到根目录下,那么你的.htaccess文件也要剪切到根目录下,总之要确保.htaccess跟入口的index.php保持同级. ...

  9. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---观察者模式之WeatherReport[转]

      1   2{<HeadFirst设计模式>之观察者模式 }   3{ 主题与观察者                    }   4{ 编译工具 :Delphi7.0          ...

  10. Java中"str1.equals(str2)"和"str1==str2"的区别

    大家好,这是我的第一篇博客,作为即将入职的学生,我现在的心情是既好奇又兴奋,对未知的职场生活充满了无限的憧憬,也想赶紧对大学生活say goodbye,因为自己的能力现在还比较有限,我想通过博客这个平 ...