介绍SpringBoot集成ActiveMQ的使用。

文章目录

  • 配置文件
  • pom.xml
  • application.yml
  • 源代码
  • 测试结果


配置文件

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 依赖于starter, 以使用自动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

        <!-- 依赖于连接池, 以启用JMS 连接池 -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>

    </dependencies>

application.yml

spring:
  # activemq的配置
  activemq:
    # 服务器地址
    broker-url: tcp://localhost:61616
    # 账户名、密码
    user: admin
    password: admin
    pool:
      # 连接池最大连接数
      max-connections: 50
      # 是否启动连接池
      enabled: true
  # jms的规范
  jms:
    # false为队列模式(默认)、true是发布订阅模式
    pub-sub-domain: false

# 自定义队列
tohandle-queue: tohandle-queue
handled_queue: handled_queue

源代码

消费者代码

package com.jsy.demoactivemqconsumer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageConsumer {

    // 注入有Springboot生成的 jmsTemplate
    @Autowired
    JmsTemplate jmsTemplate;

    @Value("${handled_queue}")
    String queue;

    @JmsListener(destination = "${tohandle-queue}")
    public void receive(String message) {
        System.out.println("消费者受到消息,现在是 ");
        System.out.println("消息内容是" + message);
        System.out.println();
        // 再发送给消费者
        sendMessage("来自消费者的回声 "+message);
    }

    public void sendMessage(String message) {
        jmsTemplate.convertAndSend(queue,message);
    }


}

生产者代码

package com.jsy.demoactivemq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageProducer {
    @Autowired
    JmsTemplate jmsTemplate;

    @Value("${tohandle-queue}")
    String queue;

    public void send(String message){
        jmsTemplate.convertAndSend(queue,message);
    }


    @JmsListener(destination = "${handled_queue}")
    public void receive(String message) {
        System.out.println(message);
    }

}

定时任务(启动类上加上@EnableScheduling注解)

package com.jsy.demoactivemq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;


@Service
public class ScheduledService {

    int cnt = 0;

    @Autowired
    MessageProducer messageProducer;

//    开启定时任务
    @Async
    @Scheduled(fixedRate = 1000)
    public void job() {
        System.out.println("我是生产者:  我要给消息队列发送一条消息");
        messageProducer.send("来自生产者的消息  id:" + (++cnt));
    }
}

执行效果就是一个生产者轮流给三个消费者发消息,然后,消费者在返回给生产者。

测试结果

消费者的输出:

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:51

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:54

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:57

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:60

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:63
.......

生产者的输出

我是生产者:  我要给消息队列发送一条消息
来自消费者的回声 来自生产者的消息  id:46
我是生产者:  我要给消息队列发送一条消息
来自消费者的回声 来自生产者的消息  id:47
我是生产者:  我要给消息队列发送一条消息
来自消费者的回声 来自生产者的消息  id:48
.....

有一点是值得关注的。
由于有三个消费者,所以,生产者生产的消息将会被按模3循环分发给三个消费者。

Web管理端。

springboot 队列启动 springboot任务队列_spring