消息总是从生产者发送到中间件再有中间件发送给消费者。
spring和activemq集成的关键配置和使用点:
需要引入的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
配置连接工程
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.winner.spring"/>
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://192.168.0.129:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>
<!--使用缓存可以提升效率-->
<bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="jmsFactory"/>
<property name="sessionCacheSize" value="1"/>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="cachingConnectionFactory"/>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean>
<!--测试Queue,队列的名字是spring-queue-->
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<!--<constructor-arg index="0" value="spring-queue"/>-->
<constructor-arg name="name" value="spring-queue"/>
</bean>
<!--测试Topic-->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-topic"/>
</bean>
</beans>
运行案例:
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;
//目的地队列的明证,我们要向这个队列发送消息
@Resource(name = "destinationQueue")
private Destination destination;
//向特定的队列发送消息
@Override
public void sendMsg(MqParamDto mqParamDto) {
final String msg = JSON.toJSONString(mqParamDto);
try {
logger.info("将要向队列{}发送的消息msg:{}", destination, msg);
jmsTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
} catch (Exception ex) {
logger.error("向队列{}发送消息失败,消息为:{}", destination, msg);
}
}
消费者、和监听器的配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://192.168.0.129:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>
<!--使用缓存可以提升效率-->
<bean id="cachingConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="jmsFactory"/>
<property name="sessionCacheSize" value="1"/>
</bean>
<!--测试Queue,队列的名字是spring-queue-->
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="spring-queue"/>
</bean>
<!--测试Topic-->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-topic"/>
</bean>
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="cachingConnectionFactory"/>
<property name="destination" ref="destinationQueue"/>
<property name="messageListener" ref="messageListener"/>
</bean>
<!--消息监听器-->
<bean id="messageListener" class="com.winner.spring.MyMessageListener">
</bean>
</beans>
自定义监听器:
public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message msg) {
if (msg instanceof TextMessage) {
try {
TextMessage txtMsg = (TextMessage) msg;
String message = txtMsg.getText();
//实际项目中拿到String类型的message(通常是JSON字符串)之后,
//会进行反序列化成对象,做进一步的处理
System.out.println("receive txt msg===" + message);
} catch (JMSException e) {
throw new RuntimeException(e);
}
} else {
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}
}
上面的案例都是配置的jmsContainer监听一个队列或者topic,接下里配置jmsContainer进行多destination的配置(暂且这样配置)。
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 使用Spring组件扫描的方式来实现自动注入bean -->
<context:component-scan base-package="com.vve"/>
<context:component-scan base-package="com.vveplat"/>
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- <property name="brokerURL" value="failover:tcp://172.30.34.106:8777?wireFormat.maxInactivityDuration=0"/> -->
<property name="brokerURL" value="failover:tcp://localhost:61616?wireFormat.maxInactivityDuration=0"/>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!--这个是第一个队列目-->
<bean id="orderQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>order</value>
</constructor-arg>
</bean>
<!--这个是第二个队列-->
<bean id="registerValueIndexQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>registerValueIndex</value>
</constructor-arg>
</bean>
<!-- 第一个消息监听器 -->
<bean id="orderMessageListener" class="com.vveplat.message.queue.listener.OrderMessageListener">
<!-- property name="messageConverter" ref="emailMessageConverter"/-->
</bean>
<!-- 第二个消息监听器 -->
<bean id="registerValueIndexMessageListener" class="com.vveplat.message.queue.listener.RegisterValueIndexMessageListener">
<!-- property name="messageConverter" ref="emailMessageConverter"/-->
</bean>
<!-- 第一个消息监听容器 -->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="orderQueueDestination" />
<property name="messageListener" ref="orderMessageListener" />
<property name="sessionTransacted" value="false"></property>
<!-- 设置固定的线程数 -->
<property name="concurrentConsumers" value="6"></property>
<!-- 设置动态的线程数 -->
<property name="concurrency" value="2-9"></property>
<!-- 设置最大的线程数 -->
<property name="maxConcurrentConsumers" value="15"></property>
</bean>
<!-- 第二个消息监听容器 -->
<bean id="jmsContainer1"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="registerValueIndexQueueDestination" />
<property name="messageListener" ref="registerValueIndexMessageListener" />
<property name="sessionTransacted" value="false"></property>
<!-- 设置固定的线程数 -->
<property name="concurrentConsumers" value="6"></property>
<!-- 设置动态的线程数 -->
<property name="concurrency" value="2-9"></property>
<!-- 设置最大的线程数 -->
<property name="maxConcurrentConsumers" value="15"></property>
</bean>
</beans>