1、在spring-activemq.xml中新加入listenerContainer和syxTopicDest等配置,实现消息监听容器,在connectionFactory中加入clientId。
<?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:mvc="http://www.springframework.org/schema/mvc" 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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.8.0.xsd">
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://127.0.0.1:61616" />
<!-- 配置connectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标ConnectionFactory 引用至 amqConnectionFactory-->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="100" />
<property name="clientId" value="syxConsumer1"/>
</bean>
<!-- Spring JmsTemplate 的消息生产者 -->
<!-- 定义JmsTemplate的Queue类型 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 引用自定义的 ConnectionFactory对象 -->
<constructor-arg ref="connectionFactory" />
<!-- 非pub/sub模型(发布/订阅),即队列模式 -->
<property name="pubSubDomain" value="false" />
<!-- 默认目标名称(队列名) -->
<property name="defaultDestinationName" value="syxQueue" />
</bean>
<!-- 定义JmsTemplate的Topic类型 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 引用自定义的 ConnectionFactory对象 -->
<constructor-arg ref="connectionFactory" />
<!-- pub/sub模型(发布/订阅) -->
<property name="pubSubDomain" value="true" />
<!-- 默认目标名称(订阅名) -->
<property name="defaultDestinationName" value="syxTopic" />
</bean>
<bean id="messageReceiver" class="com.syx.customer.receiver.MessageReceiver">
<property name="jmsTopicTemplate" ref="jmsTopicTemplate"></property>
</bean>
<!-- 消息监听容器 -->
<bean id="syxTopicDest" class="org.apache.activemq.command.ActiveMQTopic">
<!-- 设置消息队列的名字 -->
<constructor-arg index="0" value="syxTopic" />
</bean>
<bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="pubSubDomain" value="true"/><!-- default is false -->
<property name="destination" ref="syxTopicDest" /> <!-- listen topic: syxTopic -->
<property name="subscriptionDurable" value="true"/>
<property name="messageListener" ref="messageReceiverListener" /><!-- 消息监听类 -->
<!-- 监听并行区间范围 最小10个 最大15个 -->
<property name="concurrency" value="10-15"></property>
</bean>
</beans>
2、新建一个MessageReceiverListener类,来实现消息监听
采用@Component注解方式实现spring的配置
package com.syx.customer.receiver;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import org.springframework.stereotype.Component;
/**
* 接收消息监听类
*
* @author sunyx
* @since JDK 1.8
*/
@Component("messageReceiverListener")
public class MessageReceiverListener implements MessageListener {
public void onMessage(Message m) {
System.out.println("[receive message]");
try {
final MapMessage mapmessage=(MapMessage) m;
final String info = mapmessage.getString("info");
System.out.println("info:"+info);
System.out.println("model:"+m.getJMSDeliveryMode());
System.out.println("destination:"+m.getJMSDestination());
System.out.println("type:"+m.getJMSType());
System.out.println("messageId:"+m.getJMSMessageID());
System.out.println("time:"+m.getJMSTimestamp());
System.out.println("expiredTime:"+m.getJMSExpiration());
System.out.println("priority:"+m.getJMSPriority());
} catch (final JMSException e) {
e.printStackTrace();
}
}
}
3、重启项目就可以实现消息监听了,发多少个都能实时接收到。