spring+activemq配置多个生产者,多个消费者并发处理消息
2015-11-11 17:26
621 查看
先贴配置
<?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/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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 配置要扫描的包 -->
<context:component-scan base-package="service.jms"/>
<bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
<property name="useAsyncSend">
<value>true</value>
</property>
</bean>
</property>
</bean>
<bean id="connectionFactory_1" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<!-- 可以是其他的broker地址 -->
<value>tcp://localhost:61616</value>
</property>
<property name="useAsyncSend">
<value>true</value>
</property>
</bean>
</property>
</bean>
<!-- 发送消息的目的地(一个队列) -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg index="0" value="activeMQQueue" />
</bean>
<!-- 消息转换 -->
<bean id="messageConverter" class="service.jms.ObjectMessageConverter"/>
<!-- 消费者 -->
<bean id="queueConsumer" class="service.jms.MessageConsumer"/>
<bean id="queueListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="queueConsumer"/>
<property name="defaultListenerMethod" value="receive"/>
<property name="messageConverter" ref="messageConverter"/>
</bean>
<!-- 监听 -->
<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="destination" />
<property name="messageListener" ref="queueListener" />
<!-- 配置多个消费者,可以是具体数值,也可以是数值区间,根据消息的规模来确定消费者的多少 -->
<property name="concurrency" value="4-8"></property>
</bean>
<!-- 生产消息配置 -->
<bean id="queueProducer" class="service.jms.MessageProducer">
<property name="destination" ref="destination"/>
<property name="jmsTemplate">
<list>
<ref bean="jmsTemplate_1" />
<ref bean="jmsTemplate_2" />
</list>
</property>
</bean>
<!-- 配置JMS模版 -->
<bean id="jmsTemplate_1" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="messageConverter" ref="messageConverter" />
</bean>
<bean id="jmsTemplate_2" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory_1" />
<property name="messageConverter" ref="messageConverter" />
</bean>
</beans>
<?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/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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 配置要扫描的包 -->
<context:component-scan base-package="service.jms"/>
<bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
<property name="useAsyncSend">
<value>true</value>
</property>
</bean>
</property>
</bean>
<bean id="connectionFactory_1" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<!-- 可以是其他的broker地址 -->
<value>tcp://localhost:61616</value>
</property>
<property name="useAsyncSend">
<value>true</value>
</property>
</bean>
</property>
</bean>
<!-- 发送消息的目的地(一个队列) -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg index="0" value="activeMQQueue" />
</bean>
<!-- 消息转换 -->
<bean id="messageConverter" class="service.jms.ObjectMessageConverter"/>
<!-- 消费者 -->
<bean id="queueConsumer" class="service.jms.MessageConsumer"/>
<bean id="queueListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="queueConsumer"/>
<property name="defaultListenerMethod" value="receive"/>
<property name="messageConverter" ref="messageConverter"/>
</bean>
<!-- 监听 -->
<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="destination" />
<property name="messageListener" ref="queueListener" />
<!-- 配置多个消费者,可以是具体数值,也可以是数值区间,根据消息的规模来确定消费者的多少 -->
<property name="concurrency" value="4-8"></property>
</bean>
<!-- 生产消息配置 -->
<bean id="queueProducer" class="service.jms.MessageProducer">
<property name="destination" ref="destination"/>
<property name="jmsTemplate">
<list>
<ref bean="jmsTemplate_1" />
<ref bean="jmsTemplate_2" />
</list>
</property>
</bean>
<!-- 配置JMS模版 -->
<bean id="jmsTemplate_1" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="messageConverter" ref="messageConverter" />
</bean>
<bean id="jmsTemplate_2" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory_1" />
<property name="messageConverter" ref="messageConverter" />
</bean>
</beans>
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 解析ActiveMQ的使用说明总结
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- spring boot实战之内嵌容器tomcat配置
- 基于Spring框架的Shiro配置方法
- Spring MVC中上传文件实例
- spring aop两种配置方式
- 实例讲解Java的Spring框架中的AOP实现
- 解析Java中如何获取Spring中配置的bean