最近在學習activeMQ相關內容
ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。
1、activeMQ安裝
windows下直接在activeMQ官網下載最新的安裝包,解壓即可
2、activeMQ運行
可以修改conf目錄下的若干配置,更改activeMQ配置。可以在jetty-realm.properties中修改服務器的用戶名(默認有admin與user兩個用戶)與密碼,在jetty.xml修改管理activeMQ服務器的訪問地址端口號(默認8161)
可以在activemq.xml修改使用者的端口號(默認61616)用戶名與密碼(注意此處的端口號和用戶爲直接使用消息隊列的應用程序的配置與之前的登陸activeMQ後臺的端口號與用戶不同),注意此處修改用戶名與密碼須在標籤裏的標籤前加入
<!-- plugins is added by weigq .use for setting uid and pwd by msg users -->
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
用戶名密碼在credentials.properties 中修改
3、我這些都沒有做改動,使用的是默認的配置,直接運行bin/win64目錄下的activemq.bat文件即可開啓消息總線服務器
4、默認情況下訪問http://127.0.0.1:8161/admin/可以進入到後臺管理界面(用戶名與密碼均爲admin),可以查看目前系統中的queues和topics(Topic和queue的最大區別在於topic是以廣播的形式,通知所有在線監聽的客戶端有新的消息,沒有監聽的客戶端將收不到消息;而queue則是以點對點的形式通知多個處於監聽狀態的客戶端中的一個。)
以上就完成了activeMQ的總線服務器配置,以下爲在spring項目中使用這個總線消息服務器
1、在pom文件中引入依賴:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.10.0</version>
</dependency>
關於activemq-all包的版本,我本地測試時,如果應用5.10之後的版本會導致c3p0的datasource注入老是報錯,因此暫時使用的是5.10的版本,這個問題以後在研究
resource文件夾下新建文件JMSConfigure.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jsm/spring-jms-4.3.xsd
">
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>
<!-- 定義消息隊列(Queue(ActiveMQQueue)/Topic(ActiveMQTopic)) -->
<bean id="dnkxQueueDestination" class="org.apache.activemq.command.ActiveMQTopic">
<!-- 設置消息隊列的名字,如果總線中不存在這個,會自動新建一個這個名字的消息隊列 -->
<constructor-arg>
<value>testtopic</value>
</constructor-arg>
</bean>
<!-- 配置JMS模板(Queue),Spring提供的JMS工具類,它發送、接收消息。 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="dnkxQueueDestination" />
<property name="receiveTimeout" value="10000" />
<!-- true是topic,false是queue,默認是false,此處顯示寫出false -->
<property name="pubSubDomain" value="true" />
</bean>
<!-- 顯示注入消息監聽容器(Queue),配置連接工廠,監聽的目標是dnkxQueueDestination,監聽器是上面定義的監聽器 -->
<bean id="queueListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="dnkxQueueDestination" />
<property name="messageListener" ref="messageReceiver" />
</bean>
</beans>
注意dnkxQueueDestination,此處使用的是topic如果要使用queue只需要將ActiveMQTopic換成ActiveMQQueue即可,在applicationContext.xml中引入這個配置文件
<import resource="JMSConfigure.xml"/>
在需要寫入消息的程序中注入jmsTemplate即可,調用jmsTemplate.convertAndSend(text);方法即可寫入一個String到消息隊列
額外需要注意的是消息監聽容器,注意注入變量messageReceiver,是我們自己實現的一個消息監聽容器
import javax.jms.Message;
import javax.jms.MessageListener;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@Component
public class MessageReceiver implements MessageListener {
private static final Logger logger = Logger.getLogger(MessageReceiver.class);
@Override
public void onMessage(Message message) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("打印消息內容:"+message.toString());
}
}
這個監聽容器只是簡單的打印出了消息對象
以上即可完成spring使用activeMQ的配置,(注意本例中我們在同一個工程中同時配置了消息寫入與消息監聽,在實際工程中這兩項可能會分佈在不同的工程中,此時只需要分開配置即可),我們可以同時部署多個工程,同時進行多個監聽與寫入消息