SpringBoot整合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 ww
 * @Date 2021-3-23  16:55
 * @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 ww
 * @Date 2021/3/23 16:56
 * @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 ww
 * @Date 
 * @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 ww
 * @Date 
 * @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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值