A1·ActiveMQ在Spring中的配置(純後端)
阿新 • • 發佈:2018-11-26
通過以下配置,可以實現java後端的ActiveMQ配置,如果想要在前段頁面顯示,可以參照https://blog.csdn.net/lycz_tpself/article/details/81123461。
- 配置使用的是maven方式,下面給出AMQ所需的jar包(僅提供了AMQ額外需要的,Spring的請自行搞定)
<!-- Java JMS 原生API --> <dependency> <groupId>javax.jms</groupId> <artifactId>javax.jms-api</artifactId> <version>2.0.1</version> </dependency> <!-- spring-jms API --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <!-- activemq核心包 --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-spring</artifactId> <version>5.15.4</version> </dependency>
- 訊息監聽類的位置
- Spring中的配置檔案 (總的配置程式碼在圖片下面)
下圖的destination對應上上上張圖中設定的通道名稱 ,ref指向上圖中監聽類bean的id,method指向監聽類中的監聽方法名稱
配置程式碼:
<?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" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <!-- activemq相關配置,此配置方式需要新增amq對應的名稱空間 --> <!-- 連線工廠 --> <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://localhost:61616"/> <!-- 設定預取策略 --> <amq:prefetchPolicy id="prefetchPolicy" queuePrefetch="1" topicPrefetch="1000"/> <!--為了提高效率,配置一個連線池 --> <bean id="cachedConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory" p:targetConnectionFactory-ref="amqConnectionFactory" p:sessionCacheSize="10"/> <!-- 配置訊息終點 --> <amq:queue id="spittleQueue" physicalName="online.exam.queue"/> <amq:topic id="spittleTopic" physicalName="online.exam.topic"/> <bean id="messageConverter" class="org.springframework.jms.support.converter.SimpleMessageConverter"/> <!-- 配置JmsTemplate(用於簡化程式碼) --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" p:defaultDestination-ref="spittleTopic" p:messageConverter-ref="messageConverter"> <constructor-arg ref="amqConnectionFactory" /> </bean> <!-- 訊息監聽 --> <bean id="messageListener1" class="controller.amq.MessageListener1" /> <bean id="messageListener2" class="controller.amq.MessageListener2" /> <bean id="messageListener3" class="controller.amq.MessageListener3" /> <bean id="messageListener4" class="controller.amq.MessageListener4" /> <!-- listener-container 預設是queue型別的 --> <jms:listener-container connection-factory="amqConnectionFactory"> <jms:listener destination="spittle.alert.queue" ref="messageListener1" method="onMessage" /> <jms:listener destination="spittle.alert.queue" ref="messageListener2" method="onMessage" /> </jms:listener-container> <jms:listener-container destination-type="topic" connection-factory="amqConnectionFactory"> <jms:listener destination="biz1.topic" ref="messageListener3" method="onMessage" /> <jms:listener destination="biz1.topic" ref="messageListener4" method="onMessage" /> </jms:listener-container>
- 生產者程式碼(主要看紅框)
- 消費者程式碼
系統啟動後,進入http://127.0.0.1:8161/admin/檢視AMQ資訊
點選topic的active subscribers(每一條監聽都有client id)