ActiveMQ를 봄 부트 통합

저작권 : https://blog.csdn.net/weixin_41716049/article/details/90550197

수신 구성 파일 :  

상기 1. 오기 프로필

 

2. 가이드 좌표

    <!-- activemq  start -->

        <dependency>

          <groupId>org.apache.activemq</groupId>

          <artifactId>activemq-all</artifactId>

          <version>5.2.0</version>

        </dependency>

        

        <!-- activemq  end -->

        

        <!-- spring 与 mq整合  start -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-jms</artifactId>

            <version>4.2.4.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.apache.xbean</groupId>

            <artifactId>xbean-spring</artifactId>

            <version>3.7</version>

        </dependency>

        <!-- spring 与 mq整合  end -->

  3. 구성 파일의 ApplicationContext-mq.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:aop="http://www.springframework.org/schema/aop"

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

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

    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"

    xmlns:amq="http://activemq.apache.org/schema/core"

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

    xsi:schemaLocation="

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

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd

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

http://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc.xsd

http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/data/jpa

http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

http://www.springframework.org/schema/jms

http://www.springframework.org/schema/jms/spring-jms.xsd

http://activemq.apache.org/schema/core

http://activemq.apache.org/schema/core/activemq-core.xsd">

 <! - 스캔 패키지 ->   

 <!-- <context:component-scan base-package="cn.itcast.jms" /> -->

    

    <!-- ActiveMQ 连接工厂 -->

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

    <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->

    <amq:connectionFactory id="amqConnectionFactory"

        brokerURL="tcp://localhost:61616" userName="admin" password="admin"  />



    <!-- Spring Caching连接工厂 -->

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

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

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

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

        <!-- Session缓存数量和链接数有关 -->

        <property name="sessionCacheSize" value="100" />

    </bean>

    

     <! -이 JmsTemplate의 대기열 유형 정의 ->

    <콩 ID = "jmsQueueTemplate"클래스 = "org.springframework.jms.core.JmsTemplate">

        <! - 봄에서 제공하는 ConnectionFactory에 객체를 해당이 ConnectionFactory를 우리가 정의 ->  

        <생성자 아규먼트 REF = "ConnectionFactory에"/>

        <! - 비 술집 / 하위 모델 (공개 / 등록), 그 큐 모드 ->

        <속성 명 = "인 PubSubDomain"값 = "FALSE"/>

    </ 콩>

 

    <! - 유형 정의 JmsTemplate의의 주제 ->

    <콩 ID = "jmsTopicTemplate"클래스 = "org.springframework.jms.core.JmsTemplate">

         <! - 봄에서 제공하는 ConnectionFactory에 객체를 해당이 ConnectionFactory를 우리가 정의 ->  

        <생성자 아규먼트 REF = "ConnectionFactory에"/>

        <! - 펍 / 하위 모델 (게시 / 가입) ->

        <속성 명 = "인 PubSubDomain"값 = "참"/>

    </ 콩>

   

</ 콩>

3.1 쓰기 큐는 메시지를 보내고 메시지를받을 ,,

//1.手动架子啊xml配置文件

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext-mq.xml")

public class SpringQueueTest {

//2.自动注入一个对象 ,但是在这里直接使用Autowired 自动注入不了。需要把他在配置文件的ID名 加上

    @Autowired

    @Qualifier(value="jmsQueueTemplate")

    private JmsTemplate queueTemplate;

    

    @Test

    public void testQueueSend(){

//通过queueTemplate.send("数据名",new MessageCreator())给queue发送数据

//实现他的匿名方法

        queueTemplate.send("itcast297_queue——数据名", new MessageCreator() {

            

            @Override

            public Message createMessage(Session session) throws JMSException {

               

//session.自己想要传的数据的类型。。text  map 类型等,下面是text类型的

                TextMessage message = session.createTextMessage("spring发送的消息");

                return message;

            }

        });

    }

    

    @Test

    public void testQueueReceive() throws JMSException{

//调用queueTemplate接收数据  .receive("数据名"):接收            手动接收数据一般不用了

        TextMessage message = (TextMessage) queueTemplate.receive("itcast297_queue");

        System.out.println(message.getText());

    }

}

3.2 주제 메시지를 전송하고 메시지를받을 준비 ,,

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext-mq.xml")

public class SpringTopicTest {



    @Autowired

    @Qualifier(value="jmsTopicTemplate")

    private JmsTemplate topicTemplate;

    

    @Test

    public void testTopicSend(){

        topicTemplate.send("itcast297_topic", new MessageCreator() {

            

            @Override

            public Message createMessage(Session session) throws JMSException {

                // TODO Auto-generated method stub

                //TextMessage message = session.createTextMessage("spring发送的消息");

                MapMessage map = session.createMapMessage();

                map.setString("username", "cgx");

                return map;

            }

        });

    }

    

    /**

     * 手动调用的方式

     * @throws JMSException

     */

    @Test

    public void testTopicReceive() throws JMSException{

        MapMessage message = (MapMessage) topicTemplate.receive("itcast297_topic");

        System.out.println(message.getString("username"));

        System.out.println("执行结束");

    }

    

    

}

 

추천

출처blog.csdn.net/weixin_41716049/article/details/90550197