JMS消息服务器ActiveMQ项目应用

一、MQ接收配置以及接收消息代码书写

1pom.xml配置jar包引用

      <!-- activemq start -->

        <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.7.0</version>

        </dependency>

        <!-- activemq end -->

2、配置ActiveMq.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:jee="http://www.springframework.org/schema/jee"

    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"

    xmlns:util="http://www.springframework.org/schema/util"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 

       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd

">

    <!--Spring提供的JMS工具类,他可以进行消息的发送、接收等-->

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">

        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->

        <property name="connectionFactory" ref="connectionFactory" />

        <!-- message格式转换 -->

        <property name="messageConverter" ref="msgConverter"></property>

    </bean>

    <!-- message格式转换器 -->

    <bean id="msgConverter" class="com.taikang.healthcare.cust.converter.MsgConverter"></bean>

    <!-- 真正可以产生ConnectionConnectionFactory,由对应的JMS服务厂商提供 -->

    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" >

        <!-- 此处需要填写activeMQ的地址和端口号 -->

        <property name="brokerURL" value="${amq.url}" />

        <property name="clientID" value="HCUST:10.130.201.47:61616:taikang.retirement.customerprofile" />

    </bean>

    <!-- Spring用于管理真正的ConnectionFactoryConnectionFactory -->

    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory" >

        <!-- 目标ConnectionFactory对应真实的可以产生JMS ConnectionConnectionFactory -->

        <property name="targetConnectionFactory" ref="targetConnectionFactory" />

    </bean>

    <!-- 这个是队列目的地,虽然不太明白,但是有topicqueue两种,当前是topic模式-->

    <bean id="registerTopic" class="org.apache.activemq.command.ActiveMQTopic" >

        <!-- 主题的地址,这里的地址就是topic://registerTopic,此地址需根据实际情况修改 -->

        <constructor-arg>

        <value>taikang.retirement.customerprofile11</value>    

        </constructor-arg>

    </bean>

    <!-- 消息监听器 -->

    <bean id="registerListener" class="com.taikang.healthcare.cust.listener.RegisterListener" >

        <!-- 监听的目标 -->

        <property name="destination" ref="registerTopic" />

    </bean>

    <!-- 消息监听容器 -->

    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">

        <property name="connectionFactory" ref="connectionFactory" />

        <property name="destination" ref="registerTopic" />

        <property name="messageListener" ref="registerListener" />

    </bean>

</beans>

 

3、在applicationContext.xml装载activitymq.xml

JMS消息服务器ActiveMQ项目应用

4、接收消息代码

public void onMessage(Message message) throws JMSException {

       TextMessage message1=(TextMessage)message;

       JSONObject jo=JSONObject.fromObject(message1.getText());

       logInfo.put("msgId", message1.getStringProperty("id"));

       logInfo.put("event", message1.getStringProperty("event"));

       logInfo.put("source", message1.getStringProperty("source"));

       logInfo.put("consumer","HCUST");

       info("已成功接收到消息");

       try {

           handle(jo);

       } catch (Exception e) {

           warn("消息处理失败:"+e.getMessage());

       }

    }

 

 

二、MQ发送配置以及发送消息代码书写

    @Override

    public void send(String destination, JSONObject obj) {

       jmsTemplate.convertAndSend(new ActiveMQTopic(destination), obj);

    }

 

 

 

 

 

 

转载于:https://my.oschina.net/Seaside20151225/blog/717409

相关文章: