Spring整合JMS发布/订阅入门小Demo

Spring整合JMS发布/订阅入门小Demo

  • 消息生产者

1.创建工程Spring-Jms-producer-Demo,在POM文件中引入SpringJms 、activeMQ以及单元测试相关依赖
参考点对点依赖:
http://blog.csdn.net/qq_41455420/article/details/79019062

2.在src/main/resources下创建spring配置文件applicationContext-activemq-producer.xml


<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">


    <context:component-scan base-package="cn.itcast.demo">context:component-scan>     


      
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
        <property name="brokerURL" value="tcp://192.168.52.128:61616"/>  
    bean>

      
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
      
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
    bean>  

      
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
          
        <property name="connectionFactory" ref="connectionFactory"/>  
    bean>      

      
    <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">  
        <constructor-arg value="topic_text"/>  
    bean>  

beans>

3.创建生产者类TopProducer

package cn.itcast.demo;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

@Component
public class TopProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Destination topicTextDestination;

    public void sendTextMessage(final String text){
        jmsTemplate.send(topicTextDestination, new MessageCreator() {

            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(text);
            }
        });
    }
}

这里写图片描述

4.编写测试类TestTopic

package cn.itcast.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-activemq-producer.xml")
public class TestTopic {

    @Autowired
    private TopProducer topicProducer;

    @Test
    public void sendTextQueue(){
        topicProducer.sendTextMessage("Spring-jms-发布/订阅");
    }
}
  • 消息消费者

1.创建工程Spring-Jms-consumer-Demo,在POM文件中引入依赖 (同上一个工程)

2.创建配置文件 applicationContext-activemq-consumer-topic.xml


<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">

      
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
        <property name="brokerURL" value="tcp://192.168.52.128:61616"/>  
    bean>

      
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
      
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
    bean>  

      
    <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">  
        <constructor-arg value="topic_text"/>  
    bean>    

    
    <bean id="myMessageListener" class="cn.itcast.demo.MyMessageListener">bean>
    
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicTextDestination" />
        <property name="messageListener" ref="myMessageListener" />
    bean>

beans>

3.编写测试类TestTopic

package cn.itcast.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-activemq-consumer-topic.xml")
public class TestTopic {

    @Test
    public void testTop(){
        try {
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4.测试

同时运行三个消费者工程,在运行生产者工程,查看三个消费者工程的控制台输出。

这里写图片描述

Spring整合JMS发布/订阅入门小Demo_第1张图片

Spring整合JMS发布/订阅入门小Demo_第2张图片

这里写图片描述

喜欢就点赞评论+关注吧

Spring整合JMS发布/订阅入门小Demo_第3张图片

感谢阅读,希望能帮助到大家,谢谢大家的支持!

你可能感兴趣的:(消息中间件)