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

来源:互联网 发布:jetbrains 软件更新源 编辑:程序博客网 时间:2024/04/29 19:06

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

支持Java消息服务 (JMS) 1.1 版本
Spring Framework
集群 (Clustering)
支持的编程语言包括:C、C++、C#、Delphi、Erlang、Adobe Flash、Haskell、Java、JavaScript、Perl、PHP、Pike、Python和Ruby [1]
协议支持包括:OpenWire、REST、STOMP、WS-Notification、XMPP以及AMQP


好,我们先写个demo来试试 ActiveMQ的效果.

首先我们要下载ActiveMQ,下载地址:

http://www.apache.org/dyn/closer.cgi?path=/activemq/apache-activemq/5.8.0/apache-activemq-5.8.0-bin.zip


解压后,在x:/apache-activemq-5.8.0-bin/bin 目录下执行 activemq.bat即可启动 ActiveMQ,

由于ActiveMQ内置了Jetty web服务器,当ActiveMQ启动成功后,可以通过:http://localhost:8161/admin/访问ActiveMQ的控制台,默认的用户名和密码是:admin。

至此ActiveMQ 服务已经启动了,接下来我们先将x:/apache-activemq-5.8.0-bin/activemq-all-5.8.0.jar拷贝到你的classpath目录下,利用Java写个demo来尝试一下这个消息中间件。


JmsSender:

[java] view plaincopyprint?
  1. ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");    
  2.             
  3.         Connection connection = connectionFactory.createConnection();    
  4.         connection.start();    
  5.           
  6.         Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);    
  7.         Destination destination = session.createQueue("Test.foo");    
  8.   
  9.         MessageProducer producer = session.createProducer(destination);    
  10.         producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
  11.         for(int i=0; i<100; i++) {    
  12.             int id = i+1;  
  13.             ObjectMessage message = session.createObjectMessage();  
  14.             message.setObject(new User(id, "张三"+id, "123456"));  
  15.             producer.send(message);    
  16.         }    
  17.         session.commit();  
  18.         session.close();    
  19.         connection.close();    

JmsReceiver:

[java] view plaincopyprint?
  1. ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");    
  2.             
  3.         Connection connection = connectionFactory.createConnection();    
  4.         connection.start();  
  5.         
  6.         final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);    
  7.         Destination destination = session.createQueue("Test.foo");  
  8.           
  9.         MessageConsumer consumer = session.createConsumer(destination);  
  10.         //listener 方式   
  11.         consumer.setMessageListener(new MessageListener() {   
  12.        
  13.             public void onMessage(Message msg) {   
  14.                 ObjectMessage message = (ObjectMessage) msg;   
  15.                 //TODO something....   
  16.                 try {  
  17.                     User user = (User) message.getObject();  
  18.                     System.out.println("收到消息:"+user);  
  19.                 } catch (JMSException e1) {  
  20.                     // TODO Auto-generated catch block  
  21.                     e1.printStackTrace();  
  22.                 }   
  23.                 try {  
  24.                     session.commit();  
  25.                 } catch (JMSException e) {  
  26.                     // TODO Auto-generated catch block  
  27.                     e.printStackTrace();  
  28.                 }   
  29.             }   
  30.        
  31.         });   
  32.         TimeUnit.MINUTES.sleep(1);   
  33.         
  34.         session.close();    
  35.         connection.close();  

运行后,得到如下消息:

log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
收到消息:User [id=1, username=张三1, password=123456, now=Fri Jun 28 12:04:32 CST 2013]
收到消息:User [id=2, username=张三2, password=123456, now=Fri Jun 28 12:04:33 CST 2013]

.......

0 0
原创粉丝点击