Integrate spring with JMS

Source: Internet
Author: User

I. Opening Speech

Following the previous experience of apache ActiveMQ, we have been reviewing spring recently. Therefore, this article uses spring to integrate JMS.


2. Prepare the environment

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

2. spring2.5 (spring. jar)

3. JavaEE5

4. JDK1.6


Note: Start the ActiveMQ server before testing.


Iii. Code testing (P2P)

1. MsgSender: Message producer

/** * 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 consumer

/** * 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. Configure 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. Source: http://download.csdn.net/detail/zdp072/7422385

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.