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 关系型数据库
Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
|
4月前
|
消息中间件 Java Kafka
SpringBoot实用开发篇第六章(整合第三方技术,ActiveMQ,RabbitMQ,RocketMQ,Kafka)
SpringBoot实用开发篇第六章(整合第三方技术,ActiveMQ,RabbitMQ,RocketMQ,Kafka)
|
4月前
|
消息中间件 监控 Java
使用Spring Boot结合ActiveMQ和MQTT实现消息的发送和接收
使用Spring Boot结合ActiveMQ和MQTT实现消息的发送和接收
341 3
|
3月前
|
消息中间件 Java Apache
使用Spring Boot实现与ActiveMQ的消息队列集成
使用Spring Boot实现与ActiveMQ的消息队列集成
|
5月前
|
XML Java 数据格式
SpringBoot文件下载(Zip & Xml)
SpringBoot文件下载(Zip & Xml)
263 0
|
5月前
|
XML Java 测试技术
【SpringBoot】基于 Maven 的 pom.xml 配置详解
【SpringBoot】基于 Maven 的 pom.xml 配置详解
742 0
【SpringBoot】基于 Maven 的 pom.xml 配置详解
|
5月前
|
XML Java 数据库连接
Spring Boot整合Mybatis(注解版+XML版)
Spring Boot整合Mybatis(注解版+XML版)
88 0
|
5月前
|
消息中间件 XML Java
SpringBoot2.0整合ActiveMQ
SpringBoot2.0整合ActiveMQ
71 2
|
5月前
|
消息中间件 Java
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
37 0
|
5月前
|
消息中间件 Java Kafka
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
57 0
下一篇
无影云桌面