您好,登錄后才能下訂單哦!
1、下載ActiveMQ:http://activemq.apache.org/
2、解壓包,并找到activemq.bat,雙擊打開一個控制臺,跟你打開tomcat一樣的。如圖:
3、在瀏覽器查看你的activemq是否正確啟動:http://localhost:8161/admin,打開之后看到這樣:
用戶名:admin 密碼:admin
確定即可登錄。
4、你可以新建一個Queue,在 Queue Name后面的方框中填入“MyFirstQueue”,點(diǎn)擊正后方“Create”按鈕即可。
在下面Queues:列表中,就會顯示你剛剛新建的Queue:
這是已經(jīng)使用過的,其實(shí)一開始數(shù)值是這樣的:
Name : MyFirstQueue
Number Of ending Message : 0
Number Of Consumers :0
Message Enqueued:0
Message Dequeued:0
5、接下來我們通過IDEA來創(chuàng)建兩個java類,一個是消息生產(chǎn)者,一個是消息消費(fèi)者。
這是消息生產(chǎn)者:
package com.zfm.activemq; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Sender { private static final int SEND_NUMBER =5; public static void main(String[] args){ //ConnectionFactory:連接工廠 ConnectionFactory connectionFactory; //Connection:JMS客戶端到JMS Provider的連接 Connection connection = null; //Session :一個會話,發(fā)送或接收消息的線程 Session session; //Destination:消息的目的地 Destination destination; //MessageProducer:消息產(chǎn)生者:發(fā)送 MessageProducer messageProducer; //構(gòu)造connectionFactory實(shí)例對象 connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"/*ActiveMQ默認(rèn)使用的TCP連接端口是61616,*/ ); try{ //構(gòu)造從工廠得到的連接 connection = connectionFactory.createConnection(); //啟動 connection.start(); //獲取操作連接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //獲取session的參數(shù) destination = session.createQueue("FMDemo"); //得到消息生成者 messageProducer = session.createProducer(destination); //設(shè)置·不持久化 messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //構(gòu)造消息 sendMeaasge(session,messageProducer); session.commit(); }catch (Exception e){ e.printStackTrace(); }finally { try{ if(null!=connection){ connection.close(); } }catch (Throwable t){ } } } public static void sendMeaasge(Session session,MessageProducer producer) throws JMSException { for(int i=1;i<=SEND_NUMBER;i++){ TextMessage message = session.createTextMessage("ActiveMq 發(fā)送的消息" +i); //發(fā)送消息到目的地 System.out.println("發(fā)送消息: ActiveMq發(fā)送消息:" + i); producer.send(message); } } }
消息消費(fèi)者:
package com.zfm.activemq; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Receiver { public static void main(String[] rags) throws JMSException { //Connection連接工廠 ConnectionFactory connectionFactory; //Connection:JMS客戶端到JMS provider Connection connection = null; //Session : 一個發(fā)送或接受消息的·會話 Session session; //Destination:消息的目的地,消息發(fā)送給誰 Destination destination; //消費(fèi)者,消息接收者 MessageConsumer messageConsumer; connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"http://ActiveMQ默認(rèn)使用的TCP連接端口是61616, ); try{ //從構(gòu)造工廠里獲得連接 connection = connectionFactory.createConnection(); //一定要啟動 connection.start(); //從連接獲得會話 session = connection.createSession(Boolean.FALSE ,Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("FMDemo"); messageConsumer = session.createConsumer(destination); while(true){ //設(shè)置接收者接受新消息的時間 TextMessage message = (TextMessage)messageConsumer.receive(100000); if(null!=message){ System.out.println("收到消息" + message.getText()); }else{ break; } } }catch (Exception e){ }finally { try{ if(null != connection) { connection.close(); } }catch(Throwable t){ } } } }
當(dāng)運(yùn)行Sender.java之后,刷新http://localhost:8161/admin/queues.jsp時,
Queues表中的參數(shù)有變化咯:
Name : MyFirstQueue
Number Of Pending Message :5
Number Of Consumers :0
Message Enqueued :5
Message Dequeued :0
此時,由于消息已經(jīng)發(fā)完,并且我們沒有讓Sender的main函數(shù)一直運(yùn)行,所以,在控制臺打印了:
發(fā)送消息: ActiveMq發(fā)送消息:1
發(fā)送消息: ActiveMq發(fā)送消息:2
發(fā)送消息: ActiveMq發(fā)送消息:3
發(fā)送消息: ActiveMq發(fā)送消息:4
發(fā)送消息: ActiveMq發(fā)送消息:5
Process finished with exit code 0
進(jìn)程結(jié)束了。
這時我們再運(yùn)行Receiver.java,而后控制臺打?。?/span>
收到消息ActiveMq 發(fā)送的消息1
收到消息ActiveMq 發(fā)送的消息2
收到消息ActiveMq 發(fā)送的消息3
收到消息ActiveMq 發(fā)送的消息4
收到消息ActiveMq 發(fā)送的消息5
Process finished with exit code 0
此時,再刷新http://localhost:8161/admin/queues.jsp,此時表格數(shù)值變?yōu)椋?span >
Name : MyFirstQueue
Number Of Pending Message :0
Number Of Consumers :0
Message Enqueued :5
Message Dequeued :5
還有一點(diǎn)是:當(dāng)Receiver.java并沒有運(yùn)行結(jié)束的時候,由于我只開啟了一個Receiver進(jìn)程,所以這時Number Of Consumers:1。
可見,由Sender發(fā)出的消息已經(jīng)被Receiver收到了。你可以先把Sender消息發(fā)出來,這個時候只要消息已經(jīng)到消息隊(duì)列上了,只要你不期望發(fā)送者還要接收什么回復(fù),你就可以把Sender停掉了。Receiver還是一樣的接收到信息。
下面介紹一下ActiveMQ的幾種基本通信方式:發(fā)布-訂閱模式和點(diǎn)對點(diǎn)模式。
基礎(chǔ)流程:
1、獲得ActiveMQConnectionFactory。
2、利用factory獲得Connection。
3、啟動connection。
4、通過connection創(chuàng)建Session。
5、指定Session的Destination。
6、發(fā)送消息者則創(chuàng)建MessageProducer/接收消息者創(chuàng)建MessageConsumer。
7、發(fā)送和接收J(rèn)MS Message。
8、關(guān)閉所有JMS資源。
ActiveMQ詳情見http://shmilyaw-hotmail-com.iteye.com/blog/1897635
講到ActiveMQ,其實(shí)他是java消息服務(wù)(JMS)的其中一種規(guī)范,JMS詳情見:http://blog.csdn.net/jiuqiyuliang/article/details/46701559 和 http://blog.csdn.net/jiuqiyuliang/article/details/47160259。
多個項(xiàng)目之間集成
(1) 跨平臺
(2) 多語言
(3) 多項(xiàng)目
降低系統(tǒng)間模塊的耦合度,解耦
(1) 軟件擴(kuò)展性
系統(tǒng)前后端隔離
(1) 前后端隔離,屏蔽高安全區(qū)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。