1. 程式人生 > >SpringMVC整合ActiveMq

SpringMVC整合ActiveMq

實現message監聽類

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;

@Component
public class TopicMessageListener implements MessageListener{
	@Override
	public void onMessage(Message message) {
		String text = ((TextMessage)
message).getText(); //解析text 處理訊息... } }

text為訊息內容一般為json格式字串 需要解析

ActiveMQ.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" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="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/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd"
>
<amq:connectionFactory id="amqConnectionFactory" brokerURL="${MqserverIP}" userName="${MqserverUsername}" password="${MqserverPassword}" /> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg ref="amqConnectionFactory" /> <property name="sessionCacheSize" value="100" /> </bean> <!-- 定義JmsTemplate的Queue型別 --> <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate"> <constructor-arg ref="connectionFactory" /> <!-- 非pub/sub模型(釋出/訂閱),即佇列模式 --> <property name="pubSubDomain" value="false" /> </bean> <!-- 定義JmsTemplate的Topic型別 --> <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate"> <constructor-arg ref="connectionFactory" /> <!-- pub/sub模型(釋出/訂閱) --> <property name="pubSubDomain" value="true" /> <!-- receiveTimeout表示接收訊息時的超時時間 --> <property name="receiveTimeout" value="5000" /> </bean> <!-- 定義監聽類 --> <bean id="queueMessageListener" class="com.mq.consumer.queue.QueueMessageListener"> </bean> <bean id="topicMessageListener" class="com.mq.consumer.topic.TopicMessageListener"> </bean> <!-- 定義Queue監聽器 --> <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto"> <jms:listener destination="test" ref="queueMessageListener"/> </jms:listener-container> <!-- 定義Topic監聽器 --> <jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto"> <jms:listener destination="GpsInfoNotify" ref="topicMessageListener"/> </jms:listener-container> </beans>

destination為mq訊息的名稱

web.xml

web.xml新增ActiveMQ.xml的載入

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ActiveMQ.xml</param-value>
</context-param>

其中classpath為專案src下的路徑