Integration of activeMQ and spring

http://www.cnblogs.com/shuai-server/p/8966299.htmlThis   blog introduces two ways for activemq to deliver messages. Today, what is shared is the integration of activemq framework and spring framework.

(1) Import the core jar package of activemq and integrate the two jar packages context-support and jms.jar required by spring (the version using spring is 4.2.7 and the version of activemq is 5.11.2)

<!--锁定版本号-->
<properties>
  <spring.version>4.2.4.RELEASE</spring.version>
  <activemq.version>5.11.2</activemq.version>
</properties>

<!--添加依赖-->
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>

<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-all</artifactId>
</dependency>

(2) Activemq integrates spring, you first need to configure connectionFactory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">


    <!-- The ConnectionFactory that can really generate Connection is provided by the corresponding JMS service vendor -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.25.168:61616" />
    </bean>
    <!-- The ConnectionFactory that Spring uses to manage the real ConnectionFactory -->
    <bean id="connectionFactory"
        class="org.springframework.jms.connection.SingleConnectionFactory">
        <!-- The target ConnectionFactory corresponds to the real ConnectionFactory that can generate JMS Connection -->
        <property name="targetConnectionFactory" ref="targetConnectionFactory" />
    </bean>
</beans>

(3) Configure the producer object

Use the jmsTemplate object to send messages

<!-- Configure producer -->
    <!-- The JMS tool class provided by Spring, which can send and receive messages, etc. -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- This connectionFactory corresponds to the ConnectionFactory object provided by Spring we defined -->
        <property name="connectionFactory" ref="connectionFactory" />
    </bean>

(4) Configure Destination in spring (there are two ways: queue queue and topic topic)

<!--This is the queue destination, point-to-point -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg>
            <value>spring-queue</value>
        </constructor-arg>
    </bean>
    <!--This is the theme destination, one-to-many-->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="topic" />
    </bean>

(5) junit can be used to test and send messages

@Test
     public  void testSpringActiveMq() throws Exception {
         // Initialize spring container 
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml" );
         // Get JmsTemplate object from spring container 
        JmsTemplate jmsTemplate = applicationContext.getBean( JmsTemplate.class );
         // Get the Destination object from the spring container 
        Destination destination = (Destination) applicationContext.getBean("queueDestination" ) ;
         // Use the JmsTemplate object to send messages. 
        jmsTemplate.send(destination,new MessageCreator() {
            
            @Override
            public Message createMessage(Session session) throws JMSException {
                 // Create a message object and return 
                TextMessage textMessage = session.createTextMessage("spring activemq queue message" );
                 return textMessage;
            }
        });
    }

(6) Receive messages

Step 1: You need to customize the implementation class of messagerListener

public class MyMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        
        try {
            TextMessage textMessage = (TextMessage) message;
            //取消息内容
            String text = textMessage.getText();
            System.out.println(text);
        } catch (JMSException e) {
            e.printStackTrace ();
        }
    }

}

Step 2: Configure a custom message listener object in the spring container

    <!-- Receive message-->
    <!-- configure listener -->
    <bean id="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener" />
    <!-- Message monitoring container, the object referenced in the property should be consistent with the producer -->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="queueDestination" />
        <property name="messageListener" ref="myMessageListener" />
    </bean>

Step 3: Initialize a spring container and wait to receive messages

@Test
    public void testQueueConsumer() throws Exception {
        //初始化spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
        //等待
        System.in.read();
    }

At this point, the integration process of spring and activemq is completed. It should be noted that some methods in the spring framework are integrated in the core jar package of the new version of activemq, but it is not very complete. It is recommended that you pay attention when choosing a version.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066476&siteId=291194637