ActivaMQ與Spring結合

使用原生ActiveMQ的API編程 中,我們講述了ActiveMQ的原生API方式的使用,這裏我們再來介紹下ActivaMQ與Spring結合,首先肯定是需要引入相關的依賴

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

這裏除了引入activemq的依賴,還需要引入spring-jms,如下:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

這裏需要注意的是,可能會由於項目使用的Spring的版本問題,比如Spring需要使用JMS 2.0版本,但spring-jms的依賴沒有自動導入JMS 2.0,而activemq-core會導入JMS 1.1的依賴,這就導致出現版本問題,所以這裏我們需要引入JMS 2.0 依賴,同時爲了避免包發送衝突,建議可以從activemq-all中去除JMS 1.1

<dependency>
    <groupId>javax.jms</groupId>
    <artifactId>javax.jms-api</artifactId>
    <version>2.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.15.7</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-context</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jms_1.1_spec</artifactId>
        </exclusion>
    </exclusions>
</dependency>

解決好pom文件的依賴,接下來來查看其項目結構,如下:
在這裏插入圖片描述

首先我們就先來看一看消息的生產者,即producer模塊,這裏主要查看下applicationContext.xml 的配置文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ -->
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.15.7.xsd">

    <!-- 配置掃描路徑 -->
    <context:component-scan base-package="com.kami"/>

    <!-- ActiveMQ連接工廠 -->
    <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://127.0.0.1:61616" userName="" password=""/>

    <!-- Spring Caching連接工廠 -->
    <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="amqConnectionFactory"/>
        <property name="sessionCacheSize" value="100"/>
    </bean>

    <!-- Spring JmsTemplate的消息生產者-->
    <!-- 定義JmsTemplate的Queue類型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <constructor-arg ref="connectionFactory"/>
        <!-- 點對點Queue模式-->
        <property name="pubSubDomain" value="false"/>
    </bean>

    <!-- 定義JmsTemplate的Topic類型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <constructor-arg ref="connectionFactory"/>
        <!-- 發佈訂閱Topic模式-->
        <property name="pubSubDomain" value="true"/>
    </bean>

</beans>

這裏需要注意一點,就是在applicationContext.xml的命名空間中,我們需要加上activemq相關的標籤
在這裏插入圖片描述


然後我們就是在其中進行配置連接工產,然後就會將其交給Spring進行管理,然後我們只需要使用Spring提供的JmsTemplate即可,上述就即配置了Topic模式的JmsTemplate 和 Queue模式的JmsTemplate的Bean


接下來在MessageSender中使用時,我們只需要將想要使用模式的Bean進行注入即可,另外在發送消息時不要忘記添加消息發送的目的地Destination

@Component
public class MessageSender {

    @Resource(name = "jmsQueueTemplate")
    private JmsTemplate queueTemplate;

    @Autowired
    @Qualifier("jmsTopicTemplate")
    private JmsTemplate topicTemplate;

    public void sendQueueMsg(){
        queueTemplate.send("test.queue", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("Hello Queue");
                return textMessage;
            }
        });
    }

    public void sendTopicMsg(){
        topicTemplate.send("test.topic", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("Hello Topic");
                return textMessage;
            }
        });
    }
}

以上我們的消息生產者就完成了,我們就可以運行測試類,進行測試,測試類ProducerTest如下:

public class ProducerTest {

    @Test
    public void testProducer(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:conf/applicationContext.xml");
        applicationContext.start();

        MessageSender messageSender = applicationContext.getBean(MessageSender.class);
        messageSender.sendQueueMsg();
        messageSender.sendTopicMsg();
    }
}



接下來就是我們的消息消費者,同樣的也是主要看其配置文件applicationContext.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ -->
<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:jms="http://www.springframework.org/schema/jms"
       xmlns:amq="http://activemq.apache.org/schema/core"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.15.7.xsd">

    <!-- 配置掃描路徑 -->
    <context:component-scan base-package="com.kami"/>

    <!-- ActiveMQ連接工廠 -->
    <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://127.0.0.1:61616" userName="" password=""/>

    <!-- Spring Caching連接工廠 -->
    <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="amqConnectionFactory"/>
        <property name="sessionCacheSize" value="100"/>
    </bean>

    <!-- 定義消息消費者-->
    <!-- 定義Topic監聽器 -->
    <jms:listener-container destination-type="topic" container-type="default"
                            connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="test.topic" ref="topicConsumer1"/>
        <jms:listener destination="test.topic" ref="topicConsumer2"/>
    </jms:listener-container>
    <!-- 定義Queue監聽器 -->
    <jms:listener-container destination-type="queue" container-type="default"
                            connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="test.queue" ref="queueConsumer1"/>
        <jms:listener destination="test.queue" ref="queueConsumer2"/>
    </jms:listener-container>

</beans>

其中需要注意的就是在消費者的配置文件中,我們需要額外多加入幾個命名空間的標籤
在這裏插入圖片描述


其餘配置連接工產之類的,和消息的提供者配置一致,然後就是在其中需要添加Topic模式或者Queue模式的監聽器了,兩種配置我們依據destination-type="topic/queue" 進行區分


其中我們還需要指定區分的消息目的地Destination,其次其配置的ref就是我們監聽消息後進行處理的Bean了,這裏我們對topic和queue分別配置了兩個(可以在多個項目中進行配置多個監聽器,這裏就在新建其它項目了)

@Component
public class TopicConsumer1 implements MessageListener {
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
@Component
public class TopicConsumer2 implements MessageListener {
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

@Component
public class QueueConsumer1 implements MessageListener {
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
@Component
public class QueueConsumer2 implements MessageListener {
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

配置的四個監聽器其實都是一樣的,就是打印出消費的消息內容而已,然後我們可以進行測試了,測試類ConsumerTest如下:

public class ConsumerTest {
    @Test
    public void testConsumer() throws IOException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:conf/applicationContext.xml");
        applicationContext.start();

        System.in.read();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章