Springboot2模块系列:RabbitMQ

1 配置信息

1.0 pom.xml

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

1.2 application-dev.xml

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin
    publisher-confirms: true 
    publisher-returns: true
    listener:
      direct:
        acknowledge-mode: manual
      simple: 
        acknowledge-mode: manual

2 配置队列信息

package com.company.web.config;

import org.springframework.context.annotation.Configuration;

// import com.rabbitmq.client.AMQP.Queue;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;

/**
 * @uthor xindaqi
 * @description 创建RabbitMQ消息队列
 * @since 2020-06-29
 */
@Configuration
public class RabbitMQConfig {
    @Bean
    public Queue queueTest(){
        return new Queue("testqueue");
    }
}

3 生产者与消费者

3.1 生产者

package com.company.web.service;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
/**
 * @author xindaqi
 * @description 消息生产者
 * @since 2020-06-29
 */
@Component
public class MQSendService {
    @Autowired 
    private AmqpTemplate template;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(Map params){
        System.out.println("开始发送消息");
        rabbitTemplate.convertAndSend("testqueue", JSON.toJSONString(params));
    }
}

3.2 消费者

package com.company.web.service;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties.Cache.Channel;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

/**
 * @author xindaqi
 * @description 消息消费者
 * @since 2020-06-29
 */
@Component
public class MQReceiveService {
    @Autowired
    private AmqpTemplate template;
    @Autowired 
    private RabbitTemplate rabbitTempalte;

    @RabbitListener(queues="testqueue")
    public String receive(String msg){
        System.out.println("msg:"+msg);
        //TODO 写入数据库等其他操作
        return msg;
    } 
}

4 生产者接口

package com.company.web.controller;

import java.util.Map;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.web.service.MQSendService;

/**
 * @author xindaqi
 * @description 生产者接口,调用接口,生成数据
 * @since 2020-06-29
 */
@CrossOrigin(origins="*", maxAge=3600)
@RestController
@RequestMapping("/api/mq")
public class RabbitMQController {
    @Autowired 
    private MQSendService mqSendService;

    @RequestMapping(value="/send", method=RequestMethod.POST)
    public String sendMsg(@RequestBody Map params){
        mqSendService.send(params);
        return "OK";
    }
}

【参考文献】
[1]https://my.oschina.net/u/2364788/blog/2875902
[2]https://www.cnblogs.com/kingsonfu/p/10599608.html
[3]https://www.jianshu.com/p/3411feeb115f
[4]https://www.cnblogs.com/chengxy-nds/p/13217828.html
[5]https://blog.csdn.net/Weixiaohuai/article/details/82790785
[]https://blog.csdn.net/ztx114/article/details/78410727

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天然玩家

坚持才能做到极致

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

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

打赏作者

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

抵扣说明:

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

余额充值