spring整合JMS

來源:互聯網
上載者:User

一. 開篇語

繼上一篇apache ActiveMQ之初體驗後, 因為最近一直在複習spring的東西, 所以本文就使用spring整合下JMS.


二. 環境準備

1. ActiveMQ5.2.0 (activemq-all-5.2.0.jar)

2. spring2.5 (spring.jar)

3. JavaEE5

4. JDK1.6


注意: 測試前請先啟動ActiveMQ伺服器


三. 代碼測試(P2P)

1. MsgSender: 訊息生產者

/** * message sender */public class MsgSender {public static void main(String[] args) throws Exception {// load xml and create bean factoryApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");// get JmsTemplate object from spring containerJmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");// get Destination object from spring containerDestination destination = (Destination) ctx.getBean("destination");// send msg to activeMQ serverjmsTemplate.send(destination, new MessageCreator() {TextMessage message = null;public Message createMessage(Session session) {try {String str = "hello activeMQ!";message = session.createTextMessage(str);System.out.println("send: " + str);} catch (Exception e) {throw new RuntimeException("error happens...", e);}return message;}});}}

2. MsgReceiver: 訊息消費者

/** * message receiver */public class MsgReceiver {public static void main(String[] args) throws Exception {// load xml and create bean factoryApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");// get JmsTemplate object from spring containerJmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");// get Destination object from spring containerDestination destination = (Destination) ctx.getBean("destination");while (true) {// receive msg from activeMQ serverTextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);if (null != txtmsg){System.out.println("receive: " + txtmsg.getText());}else{break;}}}}

3. 配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- config JMS connection factory --><bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616" /></bean><!-- config JMS template --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /></bean><!-- config message send destination(queue) --><bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"><!-- set the name of message queue --><constructor-arg index="0" value="myQueue" /></bean></beans>

4. 源碼: http://download.csdn.net/detail/zdp072/7422385

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.