SpringBoot整合RabbitMQ---Simple Queue

1、配置文件 application.yml

#基本配置
server:
  port: 8080

spring:
  #mysql基本配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybase?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    username: root
    password: 123456
  #rabbitmq配置
  rabbitmq:
    host: 192.168.42.131
    port: 5672
    username: dev
    password: dev
    virtual-host: /vhost_dev

2、代码实践:

a、配置消息队列
package com.atzhangwl.sboot.rabbitMqConf;

import com.atzhangwl.sboot.BaseConst.MQConst;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMqConf
 * @Description RabbitMq配置
 * @Author zhangwl
 * @Date 2020/4/15 23:02
 * @Version 1.0
 **/
@Configuration
public class RabbitMqConf {

   /**
    * 简单队列
    *
    * @return
    */
   @Bean(name = "simple_queue")
   public Queue defQueue() {
      return new Queue(MQConst.TEST_SIMPLE_QUEUE);
   }

}

b、创建消息生产者

package com.atzhangwl.sboot.web;

import com.atzhangwl.sboot.BaseConst.MQConst;
import com.atzhangwl.sboot.req.MessageReq;
import com.atzhangwl.sboot.res.Result;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @ClassName RabbitMqControllor
 * @Description
 * @Author zhangwl
 * @Date 2020/4/17 21:52
 * @Version 1.0
 **/
@Controller
public class RabbitMqControllor {

   private final Logger LOG = LoggerFactory.getLogger(RabbitMqControllor.class);

   @Autowired
   private RabbitTemplate rabbitTemplate;

   /**
    * 简单队列演示
    *
    * @param messageReq
    * @return
    */
   @ResponseBody
   @RequestMapping(value = "/send/simple/message", method = RequestMethod.POST, consumes = "application/json")
   public Result sendSimpleMessage(@RequestBody MessageReq messageReq) {
      Result result = new Result();
      if (StringUtils.isEmpty(messageReq.getMessage())) {
         result.setResultCode("0001");
         result.setResultMessage("请求参数不能为空");
         return result;
      }
      rabbitTemplate.convertAndSend(MQConst.TEST_SIMPLE_QUEUE, messageReq.getMessage());
      result.setResultCode("0000");
      result.setResultMessage("发送消息成功 :" + messageReq.getMessage());
      return result;
   }

// @RabbitListener(queues = MQConst.TEST_SIMPLE_QUEUE)
// public void recvSimpleMessage(String message) {
//    System.out.println("recv :" + message);
// }
}

c、创建消息消费者

package com.atzhangwl.sboot.rabbitmq;

import com.atzhangwl.sboot.BaseConst.MQConst;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @ClassName SimpleQueueRecver
 * @Description 默认值非持久化,自动删除
 * @Author zhangwl
 * @Date 2020/4/27 21:56
 * @Version 1.0
 **/
@Component
@RabbitListener(queuesToDeclare = @Queue(value = MQConst.TEST_SIMPLE_QUEUE, durable = "true", autoDelete = "true"))
public class SimpleQueueRecver {

   @RabbitHandler
   public void recv(String message) {
      System.out.println("Simple Queue recv :" + message);
   }
}

运行结果:

Simple Queue recv :Hi Simple Message...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值