生活随笔
收集整理的這篇文章主要介紹了
深入掌握JMS(七):DeliveryMode例子
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?在下面的例子中,分別發(fā)送一個(gè)Persistent和nonpersistent的消息,然后關(guān)閉退出JMS。
view plaincopy to clipboardprint?
import?javax.jms.Connection;??import?javax.jms.DeliveryMode;??import?javax.jms.MessageProducer;??import?javax.jms.Queue;??import?javax.jms.Session;??import?org.apache.activemq.ActiveMQConnectionFactory;??import?org.apache.activemq.command.ActiveMQQueue;??public?class?DeliveryModeSendTest?{??????public?static?void?main(String[]?args)?throws?Exception?{??????????ActiveMQConnectionFactory?factory?=?new?ActiveMQConnectionFactory("vm://localhost");???????????????Connection?connection?=?factory.createConnection();??????????connection.start();???????????????????Queue?queue?=?new?ActiveMQQueue("testQueue");??????????Session?session?=?connection.createSession(false,?Session.AUTO_ACKNOWLEDGE);???????????????????????????MessageProducer?producer?=?session.createProducer(queue);??????????producer.setDeliveryMode(DeliveryMode.PERSISTENT);??????????producer.send(session.createTextMessage("A?persistent?Message"));???????????????????producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);??????????producer.send(session.createTextMessage("A?non?persistent?Message"));???????????????????System.out.println("Send?messages?sucessfully!");??????}??}??
?
??? 運(yùn)行上面的程序,當(dāng)輸出“Send messages sucessfully!”時(shí),說明兩個(gè)消息都已經(jīng)發(fā)送成功,然后我們結(jié)束它,來停止JMS Provider。
??? 接下來我們重新啟動(dòng)JMS Provicer,然后添加一個(gè)消費(fèi)者:
view plaincopy to clipboardprint?
import?javax.jms.Connection;??import?javax.jms.JMSException;??import?javax.jms.Message;??import?javax.jms.MessageConsumer;??import?javax.jms.MessageListener;??import?javax.jms.Queue;??import?javax.jms.Session;??import?javax.jms.TextMessage;??import?org.apache.activemq.ActiveMQConnectionFactory;??import?org.apache.activemq.command.ActiveMQQueue;??public?class?DeliveryModeReceiveTest?{??????public?static?void?main(String[]?args)?throws?Exception?{??????????ActiveMQConnectionFactory?factory?=?new?ActiveMQConnectionFactory("vm://localhost");???????????????Connection?connection?=?factory.createConnection();??????????connection.start();???????????????????Queue?queue?=?new?ActiveMQQueue("testQueue");??????????Session?session?=?connection.createSession(false,?Session.AUTO_ACKNOWLEDGE);???????????????????MessageConsumer?comsumer?=?session.createConsumer(queue);??????????comsumer.setMessageListener(new?MessageListener(){??????????????public?void?onMessage(Message?m)?{??????????????????try?{??????????????????????System.out.println("Consumer?get?"?+?((TextMessage)m).getText());??????????????????}?catch?(JMSException?e)?{??????????????????????e.printStackTrace();??????????????????}??????????????}??????????});??????}??}??
運(yùn)行上面的程序,可以得到下面的輸出結(jié)果:
Consumer get A persistent Message
可以看出消息消費(fèi)者只接收到一個(gè)消息,它是一個(gè)Persistent的消息。而剛才發(fā)送的non persistent消息已經(jīng)丟失了。
另外, 如果發(fā)送一個(gè)non persistent消息, 而剛好這個(gè)時(shí)候沒有消費(fèi)者在監(jiān)聽, 這個(gè)消息也會丟失.
總結(jié)
以上是生活随笔為你收集整理的深入掌握JMS(七):DeliveryMode例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。