spring 整合 activemq

来源:互联网 发布:淘宝寄到日本 编辑:程序博客网 时间:2024/06/07 20:02
<!-- activeMq -->        <dependency>             <groupId>org.apache.activemq</groupId>            <artifactId>activemq-core</artifactId>            <version>5.7.0</version>        </dependency>        <dependency>            <groupId>org.apache.activemq</groupId>            <artifactId>activemq-pool</artifactId>            <version>5.13.0</version>        </dependency><?xml version="1.0" encoding="UTF-8"?><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.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/jms        http://www.springframework.org/schema/jms/spring-jms.xsd         http://activemq.apache.org/schema/core        http://activemq.apache.org/schema/core/activemq-core.xsd">    <context:component-scan base-package="com.ita.activemq"></context:component-scan>    <!-- ActiveMQ 连接工厂 -->    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->    <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码 -->    <amq:connectionFactory id="amqConnectionFactory"        brokerURL="tcp://10.222.29.155:61616" userName="admin" password="admin" />    <!-- Spring Caching连接工厂 -->    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->    <bean id="connectionFactory"        class="org.springframework.jms.connection.CachingConnectionFactory">        <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->        <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>        <!-- 同上,同理 -->        <!-- <constructor-arg ref="amqConnectionFactory" /> -->        <!-- Session缓存数量 -->        <property name="sessionCacheSize" value="100" />    </bean>    <!-- A2M 队列1 -->    <bean id="A2M" class="org.apache.activemq.command.ActiveMQQueue">        <constructor-arg>            <value>A2M</value>        </constructor-arg>    </bean>    <!-- M2A 队列2 -->    <bean id="M2A" class="org.apache.activemq.command.ActiveMQQueue">        <constructor-arg>            <value>M2A</value>        </constructor-arg>    </bean>    <!-- 生产者 1 -->    <!-- 定义JmsTemplate的Queue类型 -->    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->        <property name="connectionFactory" ref="connectionFactory" />        <property name="defaultDestination" ref="M2A" />        <property name="receiveTimeout" value="10000" />        <property name="pubSubDomain" value="false" />    </bean>    <!-- 消息消费者监听器 配置对应的class -->    <bean id="queueListener" class="com.ita.activemq.QueueMessageListener"></bean>    <!-- 消费者1 监听器 -->    <!-- -->    <bean id="queueListenerContainer"        class="org.springframework.jms.listener.DefaultMessageListenerContainer">        <property name="connectionFactory" ref="connectionFactory" />        <property name="destination" ref="A2M" />        <property name="messageListener" ref="queueListener" />    </bean></beans>        public class QueueMessageListener implements MessageListener {    // 当收到消息后,自动调用该方法    @Override    public void onMessage(Message message) {        TextMessage tm = (TextMessage) message;        try {            System.out.println("QueueMessageListener监听到了文本消息:\t" + tm.getText());            // do something ...        } catch (JMSException e) {            e.printStackTrace();        }    }}//@Repository(value = "sender")@Component(value = "sender")public class Sender {    @Resource(name = "jmsTemplate")    private JmsTemplate jmsTemplate;    /**     * 向指定队列发送消息     */    public void sendMessage(Destination destination, final String msg) {        System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg);        jmsTemplate.send(destination, new MessageCreator() {            public Message createMessage(Session session) throws JMSException {                return session.createTextMessage(msg);            }        });    }    /**     * 向默认队列发送消息     */    public void sendMessage(final String msg) {        String destination = jmsTemplate.getDefaultDestination().toString();        System.out.println("向队列" + destination + "发送了消息------------" + msg);        jmsTemplate.send(new MessageCreator() {            public Message createMessage(Session session) throws JMSException {                return session.createTextMessage(msg);            }        });    }}