spring jms topic

来源:互联网 发布:圣艾尔摩之火 知乎 编辑:程序博客网 时间:2024/05/16 07:48

客户端配置
<?xml version="1.0" encoding="UTF-8"?>  
<!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ -->
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:aop="http://www.springframework.org/schema/aop"   
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"   
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
  
   <!-- 启用MVC注解 -->
    <mvc:annotation-driven />
    
    <!-- 静态资源文件,不会被Spring MVC拦截 -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>
    
    <!-- 指定Sping组件扫描的基本包路径 -->
    <context:component-scan base-package="com.mq" >
     <!-- 这里只扫描Controller,不可重复加载Service -->
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

   <!-- JSP视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/" />  
        <property name="suffix" value=".jsp" />
<!--  定义其解析视图的order顺序为1 -->
        <property name="order" value="1" />
    </bean>
    
    
    <!-- 配置JMS连接工厂 -->
    <bean id="providerConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="failover:(tcp://localhost:61616)" />
        <property name="useAsyncSend" value="true" />
        <property name="clientID" value="providerClienctConnect" />
    </bean>


    <!-- 定义消息Destination -->
    <bean id="topicDestination"  class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="testSpringTopic"/>
    </bean>


    <!-- 消息发送者客户端 -->
    <bean id="providerJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="providerConnectionFactory" />
        <property name="defaultDestination" ref="topicDestination" />
        <!-- 开启订阅模式 -->
        <property name="pubSubDomain" value="true"/>
        <property name="receiveTimeout" value="10000" />
        <!-- deliveryMode, priority, timeToLive 的开关要生效,必须配置为true,默认false-->
        <property name="explicitQosEnabled" value="true"/>
        <!-- 发送模式
             DeliveryMode.NON_PERSISTENT=1:非持久 ;
             DeliveryMode.PERSISTENT=2:持久
        -->
        <property name="deliveryMode" value="1"/>
    </bean>
    <bean id="sender" class="com.mq.spring.topic.TopicSender">
<property name="jmsTemplate" ref="providerJmsTemplate"></property>
</bean>
</beans>  
客户端代码

package com.mq.spring.topic;


import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;



//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations={"classpath:spring-mvc.xml"})
public class TopicSender {


    @Resource
    private JmsTemplate jmsTemplate;


    @Test
    public void send(){
        sendMqMessage(null,"spring activemq topic type message[with listener] !");
    }


    /**
     * 说明:发送的时候如果这里没有显示的指定destination.将用spring xml中配置的destination
     * @param destination
     * @param message
     */
    public void sendMqMessage(Destination destination, final String message){
        if(null == destination){
            destination = jmsTemplate.getDefaultDestination();
        }
        jmsTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(message);
            }
        });
        System.out.println("spring send text message...");
    }




    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
}

服务端配置
<?xml version="1.0" encoding="UTF-8"?>  
<!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ -->
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:aop="http://www.springframework.org/schema/aop"   
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"   
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
  
   <!-- 启用MVC注解 -->
    <mvc:annotation-driven />
    
    <!-- 静态资源文件,不会被Spring MVC拦截 -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>
    
    <!-- 指定Sping组件扫描的基本包路径 -->
    <context:component-scan base-package="com.mq" >
     <!-- 这里只扫描Controller,不可重复加载Service -->
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

   <!-- JSP视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/" />  
        <property name="suffix" value=".jsp" />
<!--  定义其解析视图的order顺序为1 -->
        <property name="order" value="1" />
    </bean>
    
    
 <!-- 配置JMS连接工厂 -->
    <bean id="consumerConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="failover:(tcp://localhost:61616)" />
        <property name="useAsyncSend" value="true" />
        <property name="clientID" value="consumerClienctConnect" />
    </bean>


    <!-- 定义消息Destination -->
    <bean id="topic1Destination"  class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="testSpringTopic"/>
    </bean>


    <!-- 配置消息消费监听者 -->
    <bean id="consumerMessageListener" class="com.mq.spring.topic.ConsumerMessageListener" />


    <!-- 消息订阅客户端1 -->
    <bean id="consumerListenerClient1" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="consumerConnectionFactory" />
        <!-- 开启订阅模式 -->
        <property name="pubSubDomain" value="true"/>
        <property name="destination" ref="topic1Destination" />
        <property name="subscriptionDurable" value="true"/>
        <!---这里是设置接收客户端的ID,在持久化时,但这个客户端不在线时,消息就存在数据库里,直到被这个ID的客户端消费掉-->
        <property name="clientId" value="consumerClient1"/>
        <property name="messageListener" ref="consumerMessageListener" />
        <!-- 消息应答方式
             Session.AUTO_ACKNOWLEDGE  消息自动签收
             Session.CLIENT_ACKNOWLEDGE  客户端调用acknowledge方法手动签收
             Session.DUPS_OK_ACKNOWLEDGE 不必必须签收,消息可能会重复发送
        -->
        <property name="sessionAcknowledgeMode" value="1"/>
    </bean>


    <!-- 消息订阅客户端2 -->
    
     <bean id="consumerMessageListener2" class="com.mq.spring.topic.ConsumerMessageListener2" />
    
    <bean id="consumerListenerClient2" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="consumerConnectionFactory" />
        <!-- 开启订阅模式 -->
        <property name="pubSubDomain" value="true"/>
        <property name="destination" ref="topic1Destination" />
        <property name="subscriptionDurable" value="true"/>
        <!---这里是设置接收客户端的ID,在持久化时,但这个客户端不在线时,消息就存在数据库里,直到被这个ID的客户端消费掉-->
        <property name="clientId" value="consumerClient2"/>
        <property name="messageListener" ref="consumerMessageListener2" />
        <!-- 消息应答方式
             Session.AUTO_ACKNOWLEDGE  消息自动签收
             Session.CLIENT_ACKNOWLEDGE  客户端调用acknowledge方法手动签收
             Session.DUPS_OK_ACKNOWLEDGE 不必必须签收,消息可能会重复发送
        -->
        <property name="sessionAcknowledgeMode" value="1"/>
    </bean>




<!-- <bean id="sender" class="com.mq.spring.topic.TopicSender">
<property name="jmsTemplate" ref="consumerConnectionFactory"></property>
</bean> -->
</beans>  
服务端代码

package com.mq.spring.topic;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;



public class ConsumerMessageListener implements MessageListener{
    @Override
    public void onMessage(Message message) {
        TextMessage tm = (TextMessage) message;
        try {
            System.out.println("---------消息消费---------");
            System.out.println("消息内容:\t" + tm.getText());
            System.out.println("消息ID:\t" + tm.getJMSMessageID());
            System.out.println("消息Destination:\t" + tm.getJMSDestination());
            System.out.println("---------更多信息---------");
            //System.out.println(ToStringBuilder.reflectionToString(tm));
            System.out.println("-------------------------");
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
package com.mq.spring.topic;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;



public class ConsumerMessageListener2 implements MessageListener{
    @Override
    public void onMessage(Message message) {
        TextMessage tm = (TextMessage) message;
        try {
            System.out.println("---------消息消费222---------");
            System.out.println("消息内容22:\t" + tm.getText());
            System.out.println("消息ID:\t" + tm.getJMSMessageID());
            System.out.println("消息Destination:\t" + tm.getJMSDestination());
            System.out.println("---------更多信息---------");
            //System.out.println(ToStringBuilder.reflectionToString(tm));
            System.out.println("-------------------------");
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
放在两个web工程中启动就可以发送和处理消息了。

0 0