ActiveMQ整合Spring

来源:互联网 发布:2030男女比例真实数据 编辑:程序博客网 时间:2024/05/21 22:56

POM

        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jms</artifactId>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>        </dependency>            <dependency>                <groupId>org.apache.activemq</groupId>                <artifactId>activemq-all</artifactId>            </dependency>

ApplicationContext.xml(生产者,发送消息者,在发送消息时候,有可能当前Service层的事务还未提交,则消息已经发出去了。消费者可能会失败,要注意发生事务隔离)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">        <property name="brokerURL" value="tcp://192.168.25.136:61616" />    </bean>    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->    <bean id="connectionFactory"        class="org.springframework.jms.connection.SingleConnectionFactory">        <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->        <property name="targetConnectionFactory" ref="targetConnectionFactory" />    </bean>    <!-- 配置生产者 -->    <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->        <property name="connectionFactory" ref="connectionFactory" />    </bean><bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">        <constructor-arg>            <value>spring-queue</value>        </constructor-arg>    </bean>    <!--这个是主题目的地,一对多的 -->    <bean id="topicAdd" class="org.apache.activemq.command.ActiveMQTopic">        <constructor-arg value="topicAdd" />    </bean>    <!-- Java代码示范   --> @Autowired    private JmsTemplate template;    @Resource    private Destination topicAdd;public void addLxzd(){    template.send(topicAdd,new MessageCreator() {            @Override            public Message createMessage(Session session) throws JMSException {                TextMessage textMessage = session.createTextMessage(Id+"");                return textMessage;            }        });}

消费者(接受消息,进行处理)

<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">        <property name="brokerURL" value="tcp://192.168.25.136:61616" />    </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="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">        <constructor-arg>            <value>spring-queue</value>        </constructor-arg>    </bean><bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">        <constructor-arg value="topic" />    </bean><bean id="itemAddMessageListener" class="cn.cc.search.message.AddMessageListener"/>    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">        <property name="connectionFactory" ref="connectionFactory" />        <property name="destination" ref="topicDestination" />        <property name="messageListener" ref="itemAddMessageListener" />    </bean>

监听器

public class AddMessageListener implements MessageListener{    @Autowired    private SolrServer solrServer;    @Override    public void onMessage(Message message) {        // TODO Auto-generated method stub        TextMessage textMessage = (TextMessage)message;        try {            String itemId = textMessage.getText();            Long id = new Long(itemId);            Thread.sleep(1000);            SolrInputDocument document = new SolrInputDocument();                document.addField("id", "demo1");            solrServer.add(document );            solrServer.commit();            System.out.println("成功添加。。");        } catch (JMSException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SolrServerException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
原创粉丝点击