ActiveMQ详解(1)——ActiveMQ简介与入门程序

时间:2022-07-24
本文章向大家介绍ActiveMQ详解(1)——ActiveMQ简介与入门程序,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

ActiveMQ详解(1)——ActiveMQ简介与入门程序

一. ActiveMQ简介

ActiveMQ是Apache发布的一款功能强大的消息中间件,它基于JMS 1.1 和 J2EE 1.4规范,目前使用十分广泛。

ActiveMQ的主要特点如下:

  • 实现了JMS规范
  • 支持多语言的客户端,包括Java, C, C++, C#, Ruby, Perl, Python, PHP等;
  • 提供了AMQP v1.0 规范和MQTT v3.1规范的支持;
  • 支持许多消息队列的高级特性,如消息组、虚拟目的地、组合目的地等;
  • 可支持JDBC的高性能、快速消息持久化;
  • 方便与Spring进行整合; …..

二. 环境搭建

本节介绍ActiveMQ在Linux系统上的安装与运行:

  1. 下载 在ActiveMQ官网可以下载到安装包,目前最新版本为5.15.5。地址:http://activemq.apache.org/activemq-5155-release.html
  2. 安装 解压安装包
tar -zxvf apache-activemq-5.15.5-bin.tar.gz
  1. 运行bin/active脚本启动服务
cd apache-activemq-5.15.5
sh bin/activemq start
  1. ActiveMQ成功启动,默认监听61616端口。可以使用netstat命令查看端口监听状态。
netstat -nltp | grep 61616
tcp        0      0 :::61616                    :::*                        LISTEN      10337/java
  1. ActiveMQ提供了一个管控台,可以监控队列状态并且对消息进行操作。管控台端口为8161,可访问http://localhost:8161/admin进入管控台,默认用户名和密码都为admin。
  2. 关闭 可通过bin/activemq脚本进行关闭
sh bin/activemq stop

三 .SpringBoot整合ActiveMQ的入门案例

SpringBoot提供了对JMS的支持。因为ActiveMQ实现了JMS规范,因此可以使用SpringBoot快速地整合ActiveMQ。下面介绍具体步骤:

  1. 在pom文件中引入activemq依赖
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
  1. 添加ActiveMQ配置
#ActiveMQ配置

spring:
 activemq:
   broker-url: 'tcp://localhost:61616'   #配置Broker URL,默认为tcp://localhost:61616
   in-memory: true
   pool:
     enabled:  false
  1. 消息生产者
/**
* @Auther: ZhangShenao
* @Date: 2018/8/29 07:48
* @Description: ActiveMQ消息生产者
*/
@Service
public class MessageProducer {
   //也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
   @Autowired
   private JmsMessagingTemplate jmsMessagingTemplate;

   /**
    * 发送消息
    * @param destination 要发送到的队列
    * @param payload 待发送的消息
    */
   public void sendMessage(Destination destination,String payload){
       jmsMessagingTemplate.convertAndSend(destination,payload);
   }
}
  1. 消息消费者
/**
* @Auther: ZhangShenao
* @Date: 2018/8/29 08:24
* @Description: ActiveMQ 消息消费者
*/
@Service
public class MessageConsumer {
   //使用JmsListener配置消费者监听指定的队列,其中message是接收到的消息
   @JmsListener(destination = "my-queue")
   public void receiveMessage(String message){
       System.err.println("接收到了消息: " + message);
   }
}
  1. 测试
/**
* @Auther: ZhangShenao
* @Date: 2018/8/29 08:28
* @Description: ActiveMQ 测试
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestActiveMQ {
   @Autowired
   private MessageProducer producer;

   @Test
   public void testSendMessage(){
       Destination destination = new ActiveMQQueue("my-queue");
       for (int i = 1;i <= 100;i++){
           producer.sendMessage(destination,"message-" + i);
       }
   }
}
  1. 查看控制台输出:
接收到了消息: message-1
接收到了消息: message-2
接收到了消息: message-3
接收到了消息: message-4
接收到了消息: message-5
接收到了消息: message-6
接收到了消息: message-7
接收到了消息: message-8
接收到了消息: message-9
接收到了消息: message-10
接收到了消息: message-11
接收到了消息: message-12
...