ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ

简介: ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ

ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ


前面有介绍与基础部分,有兴趣的可以移步:


初步认识了ActiveMQ:https://blog.csdn.net/qq_26975307/article/details/98875098


结合JavaSE进行初尝试:https://blog.csdn.net/qq_26975307/article/details/98968854


详细讲讲JMS:https://blog.csdn.net/qq_26975307/article/details/99408962,


JMS的可靠性:https://phubing.blog.csdn.net/article/details/99412285,


结合 Spring,基于配置文件的使用 ActiveMQ:https://phubing.blog.csdn.net/article/details/99413883


此篇开始结合 SpringBoot,基于 application.xml 使用ActiveMQ


1、步骤


1、新建maven工程并设置包名类名

2.pom.xml

3.application.yml

4.配置Bean

5.Queue_Produce

6.主启动类

7.测试单元


1.1、pom.xml 文件


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.phubing</groupId>
    <artifactId>bootdemotopicpro</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bootdemotopicpro</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>


1.2、application.yml


server:
  port: 8080
spring:
  activemq:
    #ActiveMQ服务器地址
    broker-url: tcp://192.168.177.130:61616
    user: admin
    password: admin
  jms:
    #false == Queue  ;    true == Topic  ;  默认为false
    pub-sub-domain: false
#自定义的队列名称
myQueue: boot-activemq-queue


1.3、消息生产者


package com.phubing.bootmqdemo.queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
import java.util.UUID;
/**
 * @ClassName Queue_Produce
 * @Description TODO
 * @Author phubing
 * @Date 2019-08-12 22:39
 * @Version 1.0
 *
 * 相当于Service层
 **/
@Component
public class Queue_Produce {
    //可以当做Dao层
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    //在ConfigBean中进行Spring管理后,可以直接注入
    @Autowired
    private Queue queue;
    //业务逻辑方法
    public void produceMsg(){
        jmsMessagingTemplate.convertAndSend(queue, "boot整合ActiveMQ案例:" + UUID.randomUUID().toString().substring(0, 32));
        System.out.println("Queue生产者消息发送完毕");
    }
}

1.4、ConfigBean


package com.phubing.bootmqdemo.config;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
/**
 * @ClassName ConfigBean
 * @Description TODO
 * @Author phubing
 * @Date 2019-08-12 22:35
 * @Version 1.0
 **/
@Component
//开启JMS适配的注解
@EnableJms
public class ConfigBean {
    //1、从配置文件中读取队列名称
    @Value("${myQueue}")
    private String myQueue;
    //相当于  <bean id="" class=""/>
    @Bean
    public Queue queue(){
        //也即按照yml的配置去访问ActiveMQ
        return new ActiveMQQueue(myQueue);
    }
}


1.5、生产者测试类


package com.phubing.bootmqdemo.queue;
import com.phubing.bootmqdemo.BootmqdemoApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
/**
 * @ClassName TestActiceMQQueue
 * @Description TODO
 * @Author phubing
 * @Date 2019-08-12 22:47
 * @Version 1.0
 **/
@SpringBootTest(classes = BootmqdemoApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
//以web的形式启动测试
@WebAppConfiguration
public class TestActiceMQQueue {
    /**
     * @Autowired 与 @Resource 的异同?
     *
     */
    @Resource
    private Queue_Produce queue_produce;
    @Test
    public void test() throws Exception{
        queue_produce.produceMsg();
    }
}


1.6、新建一个boot工程


说明:消费者与生产者分开主要是为了测试方便,pom 与 application 文件直接粘贴即可


1.7、消息消费者


package com.phubing.queue;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.TextMessage;
/**
 * @ClassName Queue_Consumer
 * @Description TODO
 * @Author phubing
 * @Date 2019-08-12 23:06
 * @Version 1.0
 *
 *
 * @Component 和 @Service 的区别
 *
 **/
@Component
public class Queue_Consumer {
    @JmsListener(destination = "${myQueue}")
    public void recive(TextMessage textMessage) throws Exception{
        System.out.println("消费者收到队列消息:"+textMessage.getText());
    }
}


2、新需求:要求每隔3秒钟往 MQ 推送消息以下定时发送


2.1、在消息生产者类中增加(Queue_Produce)


//定时投递(主启动类需开启)
    @Scheduled(fixedDelay = 3000L)
    public void produceMsgScheduled(){
        jmsMessagingTemplate.convertAndSend(queue, "produceMsgScheduled:" + UUID.randomUUID().toString().substring(0, 32));
        System.out.println("Queue生产者定投消息发送完毕");
    }


2.2、在消息生产者工程的启动类中增加


@EnableScheduling


2.3、直接启动两个工程的启动类,看看控制台效果

 

3、主题模式


主题模式与队列模式相类似,这里就不再多赘述,主要修改的是 application.yml 文件中的

pub-sub-domain: true


未完待续......(下篇开始讲讲 ActiveMQ 传输协议以及持久化机制)

目录
相关文章
|
3天前
|
XML Java 测试技术
【SpringBoot】基于 Maven 的 pom.xml 配置详解
【SpringBoot】基于 Maven 的 pom.xml 配置详解
310 0
【SpringBoot】基于 Maven 的 pom.xml 配置详解
|
3天前
|
XML Java 数据库连接
Spring Boot整合Mybatis(注解版+XML版)
Spring Boot整合Mybatis(注解版+XML版)
57 0
|
9月前
|
XML Java 数据库连接
SpringBoot-19-Mybatis的xml配置方式
在上一章节中,我们已经简单介绍mybatis的增删改查的基本操作,基础(单表)的增删改查可以按照,如果稍微复杂一些我们就需要使用mybatis的xml格式去实现。 那么我们开始使用mybatis的xml方式去实现增删改查。
64 0
|
3天前
|
XML Java 数据库连接
SpringBoot - 整合MyBatis配置版(XML)并开启事务
SpringBoot - 整合MyBatis配置版(XML)并开启事务
118 0
|
3天前
|
XML SQL Java
springboot 项目启动报Has been loaded by XML or SqlProvider, ignoring the injection of the SQL的错误的解决方案
springboot 项目启动报Has been loaded by XML or SqlProvider, ignoring the injection of the SQL的错误的解决方案
276 0
|
5月前
|
Java
application.properties模板+application.yml模板+pom模板+mapper.xml模板(springboot)
application.properties模板+application.yml模板+pom模板+mapper.xml模板(springboot)
37 0
|
6月前
|
XML Java 数据库连接
Springboot 中同时使用mybatis注解和springbean-xml配置方式
因为自己新建了一个应用,为了开发的速度,直接选用了springboot,但后来发现大部分读库的代码和同事已有的代码重复, 索性直接拿过来用。但问题是我已有的代码是通过纯注解的方式使用mybatis,同事代码是spring+xml来使用mybatis,经过几天的探索,发现一种两种方式结合使用的方法。
56 0
|
9月前
|
XML Java 数据格式
解决SpringBoot获取mapper.xml路径的问题
当mapper.xml与mapper.class放在同一文件夹下时,是不能够将xml文件打包进项目的,项目构建的时候不会加载到target文件夹中。在pom.xml中加入如下这句:
198 0
|
9月前
|
Java Maven
打通Maven中Pom.xml与SpringBoot多环境配置
通过pom.xml配置profiles节点,以及自定义打包配置文件实现SpringBoot多环境文件配置
364 0
|
9月前
|
XML 人工智能 JSON
SpringBoot实战(二):SpringMvc接收xml请求
SpringBoot实战(二):SpringMvc接收xml请求
343 0