SpringBoot整合ActiveMQ:实现生产者-消费者

整合ActiveMQ

基础

  1. 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

<dependency>
    <groupId>org.messaginghub</groupId>
    <artifactId>pooled-jms</artifactId>
</dependency>
  1. 基础配置
#ActiveMQ
##基本配置
spring.activemq.broker-url=tcp://192.168.1.122:61616
spring.activemq.user=admin
spring.activemq.password=admin
## 集群配置
#spring.activemq.broker-url=failover:(tcp://192.168.1.122:61616,tcp://192.168.1.122:61617)
## 线程池配置
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=50

生产者

  1. 配置默认队列
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.jms.Queue;

/**
 *  @title 配置默认队列
 *  @Desc  描述
 *  @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
 *  @Date 2020-03-27 16:11
 *
 */
@Configuration
public class ActiveMQConfig {

    public static final String DEFAULT_QUEUE = "avaos.queue";

    @Bean
    public Queue queue(){
        return new ActiveMQQueue(DEFAULT_QUEUE);
    }
}
  1. 生产服务

import javax.jms.Destination;

/**
 *  @title 消息生产者
 *  @Desc  描述
 *  @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
 *  @Date 2020-03-27 02:42
 *
 */
public interface ProducerService {

    /**
     * @title  发送消息
     * @desc  指定消息队列发送消息
     * @param destination:
     * @param message:
     * @return
     * @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
     * @date 2020-03-27 02:44
     */
    void send(Destination destination, final String message);

    /**
     * @title 发送消息
     * @desc  使用默认队列发送消息
     * @param message:
     * @return
     * @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
     * @date 2020-03-27 02:45
     */
    void send(final String message);
}


import cn.avaos.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import javax.jms.Queue;

@Service
public class ProducerServiceImpl implements ProducerService {

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @Override
    public void send(Destination destination, String message) {
        jmsTemplate.convertAndSend(destination, message);
    }

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

  1. 对外接口API
import cn.avaos.service.ProducerService;
import cn.avaos.web.form.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;

/**
 *  @title 模拟下订单
 *  @Desc  描述
 *  @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
 *  @Date 2020-03-27 03:04
 *  
 */
@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private ProducerService producerService;

    @GetMapping
    public Object order(String msg) {
        Destination destination = new ActiveMQQueue("order.queue");
        producerService.send(destination, msg);
        log.info("producer: 发送消息-{}", msg);
        return JsonData.ok();
    }

    @GetMapping(path = "/common")
    public Object common(String msg){
        producerService.send(msg);
        log.info("producer: 发送消息-{}", msg);
        return JsonData.ok();
    }
}

消费者

  1. 消费订单队列的消息
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 *  @title 消费order队列中的消息
 *  @Desc  描述
 *  @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
 *  @Date 2020-03-27 16:15
 *
 */
@Slf4j
@Component
public class OrderConsumer {

    @JmsListener(destination = "order.queue")
    public void receiveQueue(String text) {
      log.info("consumer: 接收的消息-{}", text);
    }
}
  1. 消费默认队列的消息
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 *  @title 消费默认队列中的消息
 *  @Desc  描述
 *  @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
 *  @Date 2020-03-27 16:16
 *
 */
@Slf4j
@Component
public class CommonConsumer {

    @JmsListener(destination = "avaos.queue")
    public void receiveQueue(String text) {
        log.info("consumer: 接收的消息-{}", text);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一鹿由妳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值