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

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或者win64文件夹下的wrapper.exe启动,

由于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来尝试一下这个消息中间件。

 

一、显示发送者的对象

复制代码
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {

    public static void main(String[] args) throws Exception {
        aa();
    }
    public static void aa() throws Exception{
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");  
          
        Connection connection = connectionFactory.createConnection();  
        connection.start();  
        
        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);  
        Destination destination = session.createQueue("Test.foo");  

        MessageProducer producer = session.createProducer(destination);  
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        for(int i=0; i<100; i++) {  
            int id = i+1;
            ObjectMessage message = session.createObjectMessage();
            message.setObject(new User(id, "张三"+id, "123456"));
            producer.send(message);  
        }  
        System.out.println("ok...");
        session.commit();
        session.close();  
        connection.close();  
    }
}
复制代码

 

二、接受者对象

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
47
48
49
50
51
52
53
54
55
56
57
58
import  java.util.concurrent.TimeUnit;
 
import  javax.jms.Connection;
import  javax.jms.ConnectionFactory;
import  javax.jms.Destination;
import  javax.jms.JMSException;
import  javax.jms.Message;
import  javax.jms.MessageConsumer;
import  javax.jms.MessageListener;
import  javax.jms.ObjectMessage;
import  javax.jms.Session;
 
import  org.apache.activemq.ActiveMQConnectionFactory;
 
public  class  Receiver {
 
     public  static  void  aa()  throws  Exception{
         ConnectionFactory connectionFactory =  new  ActiveMQConnectionFactory( "tcp://127.0.0.1:61616" ); 
           
         Connection connection = connectionFactory.createConnection(); 
         connection.start();
       
         final  Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); 
         Destination destination = session.createQueue( "Test.foo" );
         
         MessageConsumer consumer = session.createConsumer(destination);
         //listener 方式
         consumer.setMessageListener( new  MessageListener() {
      
             public  void  onMessage(Message msg) {
                 ObjectMessage message = (ObjectMessage) msg;
                 //TODO something....
                 try  {
                     User user = (User) message.getObject();
                     System.out.println( "收到消息:" +user.getName());
                 catch  (JMSException e1) {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();
                 }
                 try  {
                     session.commit();
                 catch  (JMSException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
      
         });
         TimeUnit.MINUTES.sleep( 1 );
       
         session.close(); 
         connection.close();
     }
     
     public  static  void  main(String[] args)  throws  Exception{
         aa();
     }
}

  

复制代码
 1 import java.io.Serializable;
 2 
 3 public class User implements Serializable {
 4 
 5     private int id;
 6     
 7     private String name;
 8     
 9     private String pwd;
10     
11     public User(int id,String name,String pwd){
12         this.id=id;
13         this.name=name;
14         this.pwd=pwd;
15     }
16 
17     public int getId() {
18         return id;
19     }
20 
21     public void setId(int id) {
22         this.id = id;
23     }
24 
25     public String getName() {
26         return name;
27     }
28 
29     public void setName(String name) {
30         this.name = name;
31     }
32 
33     public String getPwd() {
34         return pwd;
35     }
36 
37     public void setPwd(String pwd) {
38         this.pwd = pwd;
39     }
40     
41 }
复制代码

 

先运行发送者,然后运行接收者即可。。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值