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

jopen 11年前發布 | 57K 次閱讀 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,

由于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:

    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);    
            }    
            session.commit();  
            session.close();    
            connection.close();    
JmsReceiver:
    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);  
                    } 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();  
運行后,得到如下消息:

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]

.......

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!