Spring整合JMS点对点入门小Demo

Spring整合JMS点对点入门小Demo

  • 消息生产者

    1. 创建工程Spring-Jms-producer-Demo,在POM文件中引入SpringJms 、activeMQ以及单元测试相关依赖
    <properties>
        <spring.version>4.2.4.RELEASEspring.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.activemqgroupId>
            <artifactId>activemq-allartifactId>
            <version>5.11.2version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
        dependency>
    dependencies>
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="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">  
        <constructor-arg value="queue_text"/>  
    bean>    

beans>

3.创建消息生产者类QueueProducer

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 QueueProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Destination queueTextSedtination;

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

            public Message createMessage(Session session) throws JMSException {

                return session.createTextMessage(text);
            }
        });
    }
}

4.单元测试

创建测试类TestQueue

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 TestQueue {

    @Autowired
    private QueueProducer queueProducer;

    @Test
    public void testSend(){
        queueProducer.sendTextMessage("Spring-jms-点对点");
    }
}

运行结果

这里写图片描述

  • 消息消费者

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

2.创建配置文件 applicationContext-activemq-consumer.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="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">  
        <constructor-arg value="queue_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="queueTextDestination" />
        <property name="messageListener" ref="myMessageListener" />
    bean>

beans>

3.编写监听类MyMessageListener

package cn.itcast.demo;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MyMessageListener implements MessageListener {

    public void onMessage(Message message) {
        TextMessage textMessage=(TextMessage) message;
        try {
            System.out.println("接收消息: "+textMessage.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

5.创建测试类TestQueueConsumer

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.xml")
public class TestQueueConsumer {

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

这里写图片描述

Spring整合JMS点对点入门小Demo_第1张图片

喜欢就点赞评论+关注吧

Spring整合JMS点对点入门小Demo_第2张图片

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

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