實現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下的路徑