ActiveMQ使用教程

简介:

ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。 
ActiveMQ特性列表 
1. 多种语言和协议编写客户端。语言: Java, C, C++, C#, Ruby, Perl, Python, PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP 
2. 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务) 
3. 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性 
4. 通过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上 
5. 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA 
6. 支持通过JDBC和journal提供高速的消息持久化 
7. 从设计上保证了高性能的集群,客户端-服务器,点对点 
8. 支持Ajax 
9. 支持与Axis的整合 
10. 可以很容易得调用内嵌JMS provider,进行测试 

1:下载 ActiveMQ 5.6.0 Release 
http://activemq.apache.org/download.html 
放到d盘 
ActiveMQ使用教程

2:运行apache-activemq服务:双击 activemq.bat 
ActiveMQ使用教程 

3:效果 
ActiveMQ使用教程

4:所需jar包 
ActiveMQ使用教程

5:spring配置文件applicationContext.xml 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<? xml  version = "1.0"  encoding = "UTF-8" ?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 

     "http://www.springframework.org/dtd/spring-beans.dtd">

< beans >
     < bean  id = "connectionFactory"  class = "org.apache.activemq.ActiveMQConnectionFactory" >
         < property  name = "brokerURL" >
             < value >tcp://localhost:61616?wireFormat.maxInactivityDuration=0</ value >
         </ property >
     </ bean >
     < bean  id = "jmsTemplate"  class = "org.springframework.jms.core.JmsTemplate" >
         < property  name = "connectionFactory" >
             < ref  bean = "connectionFactory" />
         </ property >
     </ bean >
     < bean  id = "destination"  class = "org.apache.activemq.command.ActiveMQQueue" >
         < constructor-arg  index = "0" >
             < value >MessageQueue</ value >
         </ constructor-arg >
     </ bean >
</ beans >



这时主要是配置activemq服务信息与实现springjms的对应接口 

6:消息产生者 

?

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
package  test;
import  javax.jms.JMSException;
import  javax.jms.Message;
import  javax.jms.Session;
 
import  org.springframework.jms.core.MessageCreator;
 
/**
  * 消息产生者
  * User: liuwentao
  * Time: 12-6-14 上午11:31
  */
public  class  MyMessageCreator  implements  MessageCreator {
     public  int  n =  0 ;
     private  static  String str1 =  "这个是第 " ;
     private  static  String str2 =  " 个测试消息!" ;
     private  String str =  "" ;
     @Override
     public  Message createMessage(Session paramSession)  throws  JMSException {
         System.out.println( "MyMessageCreator  n="  + n);
         if  (n ==  9 ) {
             //在这个例子中表示第9次调用时,发送结束消息
             return  paramSession.createTextMessage( "end" );
         }
         str = str1 + n + str2;
         return  paramSession.createTextMessage(str);
     }
}


7:发送消息方 

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
package  test;
import  javax.jms.Destination;
 
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  org.springframework.jms.core.JmsTemplate;
/**
  * 发送消息方
  * User: liuwentao
  * Time: 12-6-14 上午11:29
  */
public  class  MessageSender  extends  Thread {
     public  static  void  main(String args[])  throws  Exception {
         String[] configLocations =  new  String[] { "test/applicationContext.xml" };
         ApplicationContext context =  new  ClassPathXmlApplicationContext(configLocations);
         JmsTemplate jmsTemplate = (JmsTemplate) context.getBean( "jmsTemplate" );
         Destination destination = (Destination) context.getBean( "destination" );
         for  ( int  i =  1 ; i <  100 ; i++) {
             System.out.println( "发送 i="  + i);
             //消息产生者
             MyMessageCreator myMessageCreator =  new  MyMessageCreator();
             myMessageCreator.n = i;
             jmsTemplate.send(destination, myMessageCreator);
             sleep( 10000 ); //10秒后发送下一条消息
         }
     }
}


8:消息接收方 

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
package  test;
import  javax.jms.Destination;
import  javax.jms.TextMessage;
 
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  org.springframework.jms.core.JmsTemplate;
/**
  * 消息接收方
  * User: liuwentao
  * Time: 12-6-14 上午11:32
  */
public  class  MessageReciver{
     public  static  void  main(String args[])  throws  Exception {
         String[] configLocations =  new  String[] { "test/applicationContext.xml" };
         ApplicationContext context =  new  ClassPathXmlApplicationContext(configLocations);
 
         JmsTemplate jmsTemplate = (JmsTemplate) context.getBean( "jmsTemplate" );
         Destination destination = (Destination) context.getBean( "destination" );
 
         TextMessage msg =  null ;
         //是否继续接收消息
         boolean  isContinue =  true ;
         while  (isContinue) {
             msg = (TextMessage) jmsTemplate.receive(destination);
             System.out.println( "收到消息 :"  + msg.getText());
             if  (msg.getText().equals( "end" )) {
                 isContinue =  false ;
                 System.out.println( "收到退出消息,程序要退出!" );
             }
         }
         System.out.println( "程序退出了!" );
     }
}

9:测试 

运行 发送方和接收方 (顺序随便) main文件,效果如下: 

ActiveMQ使用教程 

ActiveMQ使用教程 
注:即使 接收方启动晚,或者 发送方关闭了, 接收方都会正常接收完所有数据 

特别说明:尊重作者的劳动成果,转载请注明出处哦~~~http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt247
相关文章
|
消息中间件 Java Linux
activeMQ入门安装
activeMQ入门安装
202 0
activeMQ入门安装
|
15天前
|
消息中间件 存储 数据安全/隐私保护
RabbitMQ使用教程
RabbitMQ使用教程
20 2
|
6月前
|
消息中间件 Java Linux
消息中间件系列教程(02) -ActiveMQ -安装&入门案例
消息中间件系列教程(02) -ActiveMQ -安装&入门案例
30 0
|
8月前
|
消息中间件 存储 Kafka
(四)kafka从入门到精通之安装教程
Kafka是一个高性能、低延迟、分布式的分布式数据库,可以在分布式环境中实现数据的实时同步和分发。Zookeeper是一种开源的分布式数据存储系统,它可以在分布式环境中存储和管理数据库中的数据。它的主要作用是实现数据的实时同步和分发,可以用于实现分布式数据库、分布式文件系统、分布式日志系统等。Zookeeper的设计目标是高可用性、高性能、低延迟,它支持多种客户端协议,包括TCP和HTTP,可以方便地与其他分布式系统进行集成。
99 0
|
9月前
|
消息中间件 中间件 微服务
RabbitMQ 入门简介及安装
RabbitMQ 入门简介及安装
88 0
|
12月前
|
消息中间件 存储 缓存
RabbitMQ快速入门 | 帮助快速上手
🐇🐇🐇前言:RabbitMQ是Apache公司的顶级项目之一, 也是目前各大互联网公司常用的🔥主流MQ🔥之一, 因此在这里向大家介绍RabbitMQ的基础知识和相关应用, 供大家参考学习, 也望大佬给出指点与建议,谢谢大家❤️❤️❤️
83 0
|
消息中间件 监控 Java
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)中
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)中
207 1
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)中
|
消息中间件 监控 Java
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)下
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)下
183 1
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)下
|
消息中间件 监控 Java
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)上
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)上
264 0
重大发现!消息中间件——RocketMQ(一) 环境搭建(完整版)上
|
消息中间件 存储 缓存
RabbitMQ 实战教程(一)
RabbitMQ 实战教程(一)
185 0
RabbitMQ 实战教程(一)