Spring-JMS(一)spring整合JSM之activeMQ

導入依賴

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-client</artifactId>
    <version>5.13.4</version>
</dependency>

1.消息生產者

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd">




    <!-- 1、真正可以產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供 -->
    <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.25.132:61616"></property>
    </bean>
    <!-- 2、Spring用於管理真正的ConnectionFactory的ConnectionFactory -->
    <bean id="connectionFactory"
        class="org.springframework.jms.connection.SingleConnectionFactory">
        <!-- 2-1目標ConnectionFactory對應真實的可以產生JMS Connection的ConnectionFactory -->
        <property name="targetConnectionFactory" ref="activeMQConnectionFactory"></property>
    </bean>

    <!--3-1這個是隊列目的地,點對點的 文本信息 -->
    <bean class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="queue_text"></constructor-arg>
    </bean>
    <!--3-2 這個是訂閱模式 文本信息 -->
    <bean class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="topic_solr_html"></constructor-arg>
    </bean>


    <!-- 4、Spring提供的JMS工具類,它可以進行消息發送、接收等 -->
    <bean class="org.springframework.jms.core.JmsTemplate">
        <!-- 3-1 這個connectionFactory對應的是我們定義的Spring提供的那個ConnectionFactory對象 -->
        <constructor-arg ref="connectionFactory"></constructor-arg>
    </bean>


</beans>

測試demo

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-producer.xml")
public class TestQueue {
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private ActiveMQQueue queueTextDestination;
    @Test 
    public void testSend(){

        jmsTemplate.send(queueTextDestination, new MessageCreator() { 

            public Message createMessage(Session session) throws JMSException {

                return session.createTextMessage("SpringJms-測試demo");
            }
       });

    } 
}

2 消息消費者

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd">

    <!-- 1、真正可以產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供 -->
    <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <!-- 1-1 -->
        <property name="brokerURL" value="tcp://192.168.11.11:61616"></property>
    </bean>

    <!-- 2、Spring用於管理真正的ConnectionFactory的ConnectionFactory -->
    <bean id="connectionFactory"
        class="org.springframework.jms.connection.SingleConnectionFactory">
        <!-- 2-2 目標ConnectionFactory對應真實的可以產生JMS Connection的ConnectionFactory -->
        <property name="targetConnectionFactory" ref="activeMQConnectionFactory"></property>
    </bean>

    <!--3-1這個是隊列目的地,點對點的 文本信息 -->
    <bean class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="queue_text"></constructor-arg>
    </bean>
    <!--3-2這個是訂閱模式 文本信息 -->
    <bean id="pygSolrHtml" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="topic_solr_html"></constructor-arg>
    </bean>


    <!-- 4、我的監聽類 -->
    <bean id="myMessageListener" class="com.it.listenner.SolrMessageListenner"></bean>
    <!-- 5、消息監聽容器 -->
    <bean
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="destination" ref="pygSolrHtml"></property>
        <property name="messageListener" ref="myMessageListener"></property>
    </bean>

</beans>

消息監聽類

public class MyMessageListener implements MessageListener {
   public void onMessage(Message message) {
        TextMessage textMessage=(TextMessage)message;
        try {
            System.out.println("接收到消息:"+textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

測試demo

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-consumer-queue.xm
l")
public class TestQueue {
@Test
    public void testQueue(){
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    } 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章