activemq学习笔记——spring整合(Point-To-Point点对点类型)

来源:互联网 发布:免费装修软件app 编辑:程序博客网 时间:2024/06/14 17:28

所需jar包:



①配置ConnectionFactory

connectionFactory是Spring用于创建到JMS服务器链接的

xml配置:

   <!-- 连接工厂 -->    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">    <property name="brokerURL" value="tcp://localhost:61616" />    <property name="userName" value="admin"></property>    <property name="password" value="admin"></property>    </bean>
②消息发送的目的地

点对点方式仅有一个订阅者会收到消息,消息一旦被处理就不会存在队列中

<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  <constructor-arg value="myQueue"></constructor-arg> </bean>
③消息模板

 <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->          <constructor-arg ref="connectionFactory" />    </bean>

④生产者

<bean id="producer" class="com.activemq.mqq.ProducerServiceImpl">    <property name="jmsTemplate" ref="jmsQueueTemplate"></property>    <property name="destination" ref="destination"></property>    </bean>
package com.activemq.mqq;public interface ProducerService {public void send();}

package com.activemq.mqq;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MapMessage;import javax.jms.Message;import javax.jms.Session;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;/**生产者*/public class ProducerServiceImpl implements ProducerService{JmsTemplate jmsTemplate;Destination destination;@Overridepublic void send() {// TODO Auto-generated method stubMessageCreator mc = new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {// TODO Auto-generated method stubMapMessage mm = session.createMapMessage();mm.setString("name", "张龙赵虎");return mm;}};jmsTemplate.send(destination, mc);}public JmsTemplate getJmsTemplate() {return jmsTemplate;}public void setJmsTemplate(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}public Destination getDestination() {return destination;}public void setDestination(Destination destination) {this.destination = destination;}}
⑤消息监听

Spring整合JMS的应用中我们在定义消息监听器的时候一共可以定义三种类型的消息监听器,分别是MessageListenerSessionAwareMessageListenerMessageListenerAdapter。这里我们使用MessageListenerAdapter

xml配置:

  <bean id="consumerListenerService" class="com.activemq.mqq.ConsumerListenerServiceImpl">     </bean>
package com.activemq.mqq;import java.util.HashMap;public interface ConsumerListenerService {public void receive(HashMap<String,Object> map);}
package com.activemq.mqq;import java.util.HashMap;public class ConsumerListenerServiceImpl implements ConsumerListenerService{@Overridepublic void receive(HashMap<String,Object> map) {// TODO Auto-generated method stubSystem.out.println(map.get("name"));}}

MessageListenerAdapter 

<bean id="queueListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"><constructor-arg ref="consumerListenerService"></constructor-arg><property name="defaultListenerMethod" value="receive"></property></bean>

⑥Container消息监听容器

<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory"></property><property name="destination" ref="destination"></property><property name="messageListener" ref="queueListener"></property></bean>
全部xml配置:

<?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: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/aop      http://www.springframework.org/schema/aop/spring-aop-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" >            <!-- 连接工厂 -->    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">    <property name="brokerURL" value="tcp://localhost:61616" />    <property name="userName" value="admin"></property>    <property name="password" value="admin"></property>    </bean>    <!-- 配置消息发送目的地方式 --> <!-- Queue队列:仅有一个订阅者会收到消息,消息一旦被处理就不会存在队列中 --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  <constructor-arg value="myQueue"></constructor-arg> </bean> <!-- Spring JmsTemplate 的消息生产者 start-->    <!-- 定义JmsTemplate的Queue类型 -->    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->          <constructor-arg ref="connectionFactory" />        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->        <property name="pubSubDomain" value="false" />    </bean>        <bean id="producer" class="com.activemq.mqq.ProducerServiceImpl">    <property name="jmsTemplate" ref="jmsQueueTemplate"></property>    <property name="destination" ref="destination"></property>    </bean>        <bean id="consumerListenerService" class="com.activemq.mqq.ConsumerListenerServiceImpl">     </bean>    <bean id="queueListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"><constructor-arg ref="consumerListenerService"></constructor-arg><property name="defaultListenerMethod" value="receive"></property></bean><bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory"></property><property name="destination" ref="destination"></property><property name="messageListener" ref="queueListener"></property></bean></beans>

web.xml配置:

<context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:queue.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

将应用部署到tomcat上,运行测试代码:

ApplicationContext ac = new ClassPathXmlApplicationContext("queue.xml");ProducerService ps = ac.getBean(ProducerService.class);ps.send();

结果tomcat控制台打印出结果:


代码下载地址:http://download.csdn.net/detail/sunqingzhong44/9598001

发布订阅模式的配置与其类似,只不过消息监听为多个就不在一一介绍了





0 0