消息中间件 ActiveMQ的简单使用

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介:

一、AactiveMQ的下载和安装

1. 下载ActiveMQ

地址:http://activemq.apache.org/activemq-5152-release.html

我这里下载的是window的版本

2. 下载后,解压

 

 里面有win32位和win64两种文件夹,找到你电脑上对应的win版本,我这里用的win64

右击activemq.bat,并且以管理员身份运行

启动成功后,会打印http的地址

打开这个网址http://127.0.0.1:8186

 

二、代码的使用

1. 创建工程

创建一个Maven工程,

 

2. 创建生产者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public  class  AppProducer
{
     private  static  final  String url =  "tcp://192.168.2.121:61616" ;
 
     private  static  final  String queueName= "queue-test" ;
 
     public  static  void   main(String[] args){
         //1. 创建ConnectionFactory
         ConnectionFactory connectionFactory =  new  ActiveMQConnectionFactory(url);
 
 
         try  {
             //2. 创建Connection
             Connection connection = connectionFactory.createConnection();
 
             //3. 启动连接
              connection.start();
 
              //4. 创建会话
             Session session = connection.createSession( false , Session.AUTO_ACKNOWLEDGE);
 
             //5. 创建一个目标
             Destination destination = session.createQueue(queueName);
 
             //6.  创建一个目标
             MessageProducer producer = session.createProducer(destination);
 
             for ( int  i= 0 ; i< 100 ; i++){
                 //7. 创建消息
                 TextMessage textMessage = session.createTextMessage( "test"  + i);
                 //8. 发布消息
                 producer.send(textMessage);
                 System.out.println( "发送消息"  + textMessage.getText());
             }
             //9.关闭连接
             connection.close();
 
 
 
         catch  (JMSException e) {
             e.printStackTrace();
         }
 
 
     }

  

3. 创建消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public  class  AppConsumer {
     private  static  final  String url =  "tcp://192.168.2.121:61616" ;
 
     private  static  final  String queueName= "queue-test" ;
 
     public  static  void   main(String[] args)  throws  JMSException{
         //1. 创建ConnectionFactory
         ConnectionFactory connectionFactory =  new  ActiveMQConnectionFactory(url);
 
         //2. 创建Connection
         Connection connection = connectionFactory.createConnection();
 
         //3. 启动连接
         connection.start();
 
         //4. 创建会话
         Session session = connection.createSession( false , Session.AUTO_ACKNOWLEDGE);
 
         //5. 创建一个目标
         Destination destination = session.createQueue(queueName);
 
         //6. 创建一个消费者
         MessageConsumer consumer = session.createConsumer(destination);
 
         //7. 创建一个监听器
         consumer.setMessageListener( new  MessageListener() {
             public  void  onMessage(Message message) {
                 TextMessage textMessage = (TextMessage)message;
 
                 try  {
                     System.out.println( "接收消息"  + textMessage.getText());
                 catch  (JMSException e) {
                     e.printStackTrace();
                 }
             }
         });
 
     }
}

  

三、主题模式下的消息

1. 消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public  class  AppConsumer {
     private  static  final  String url =  "tcp://192.168.2.121:61616" ;
 
     private  static  final  String topicName= "topic-test" ;
 
     public  static  void   main(String[] args)  throws  JMSException{
         //1. 创建ConnectionFactory
         ConnectionFactory connectionFactory =  new  ActiveMQConnectionFactory(url);
 
         //2. 创建Connection
         Connection connection = connectionFactory.createConnection();
 
         //3. 启动连接
         connection.start();
 
         //4. 创建会话
         Session session = connection.createSession( false , Session.AUTO_ACKNOWLEDGE);
 
         //5. 创建一个目标
         Destination destination = session.createTopic(topicName);
 
         //6. 创建一个消费者
         MessageConsumer consumer = session.createConsumer(destination);
 
         //7. 创建一个监听器
         consumer.setMessageListener( new  MessageListener() {
             public  void  onMessage(Message message) {
                 TextMessage textMessage = (TextMessage)message;
                 try  {
                     System.out.println( "接收消息"  + textMessage.getText());
                 catch  (JMSException e) {
                     e.printStackTrace();
                 }
             }
         });
         
     }
}

  

2. 创建生产者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public  class  AppProducer
{
     private  static  final  String url =  "tcp://192.168.2.121:61616" ;
 
     private  static  final  String topicName= "topic-test" ;
 
     public  static  void   main(String[] args){
         //1. 创建ConnectionFactory
         ConnectionFactory connectionFactory =  new  ActiveMQConnectionFactory(url);
 
 
         try  {
             //2. 创建Connection
             Connection connection = connectionFactory.createConnection();
 
             //3. 启动连接
              connection.start();
 
              //4. 创建会话
             Session session = connection.createSession( false , Session.AUTO_ACKNOWLEDGE);
 
             //5. 创建一个目标
             Destination destination = session.createTopic(topicName);
 
             //6.  创建一个目标
             MessageProducer producer = session.createProducer(destination);
 
             for ( int  i= 0 ; i< 100 ; i++){
                 //7. 创建消息
                 TextMessage textMessage = session.createTextMessage( "test"  + i);
                 //8. 发布消息
                 producer.send(textMessage);
                 System.out.println( "发送消息"  + textMessage.getText());
             }
             //9.关闭连接
             connection.close();
 
 
 
         catch  (JMSException e) {
             e.printStackTrace();
         }
 
 
     }
}

  


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/8047357.html,如需转载请自行联系原作者


目录
相关文章
|
7月前
|
消息中间件 存储 网络协议
MQ(消息中间件)概述及 RabbitMQ 的基本介绍
MQ(消息中间件)概述及 RabbitMQ 的基本介绍
167 0
|
5月前
|
消息中间件 Linux 虚拟化
消息中间件系列教程(04) -RabbitMQ -简介&安装
消息中间件系列教程(04) -RabbitMQ -简介&安装
38 0
|
5月前
|
消息中间件 缓存 API
消息中间件系列教程(14) -RabbitMQ-自动补偿机制
消息中间件系列教程(14) -RabbitMQ-自动补偿机制
94 0
|
5月前
|
消息中间件 存储 Java
消息中间件系列教程(09) -RabbitMQ -案例代码(发布订阅模式)
消息中间件系列教程(09) -RabbitMQ -案例代码(发布订阅模式)
36 0
|
5月前
|
消息中间件 Java Linux
消息中间件系列教程(02) -ActiveMQ -安装&入门案例
消息中间件系列教程(02) -ActiveMQ -安装&入门案例
29 0
|
5月前
|
消息中间件 Java Maven
消息中间件系列教程(13) -RabbitMQ-SpringBoot集成RabbitMQ
消息中间件系列教程(13) -RabbitMQ-SpringBoot集成RabbitMQ
37 0
|
9月前
|
消息中间件 负载均衡 安全
RabbitMQ设计原理解析
RabbitMQ现在用的也比较多,但是没有过去那么多啦。现在很多的流行或者常用技术或者思路都是从过去的思路中演变而来的。了解一些过去的技术,对有些人来说可能会产生众里寻他千百度的顿悟,加深对技术的理解,更好的应用于工作中去。
107 0
|
消息中间件 网络协议 Java
RabbitMQ消息中间件学习3:快速入门案例
rabbitmq是spring一个公司的,所以很多公司 企业选择用rabbitmq。
RabbitMQ消息中间件学习3:快速入门案例
|
消息中间件 存储 Java
有关于RabbitMQ的简单介绍与主流MQ框架
主要解决异步处理、应用解耦、流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构 1.异步处理 用户注册后,需要发注册邮件和注册短信 2.应用解耦 用户下单后,订单系统需要通知库存系统 3.流量削锋(重点) 流量削锋也是消息队列中的常用场景,一般在秒杀或团抢活动中使用广泛
|
消息中间件 SQL Java
ActiveMQ系列:一个MQ产品的基础知识
AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。基于此协议的客户端与消息中间件可传递消息,并不受客户端/中间件不同产品、不同开发语言等条件的限制。
157 0
ActiveMQ系列:一个MQ产品的基础知识