springboot集成rabbitMq+消息确认

mq的使用推荐一篇文章
mq介绍和使用

  1. 项目结构 在这里插入图片描述

(2)添加maven或者gradle坐标(看自己的项目是什么工程)
maven

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>

gradle

// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp
compile group: 'org.springframework.boot', name: 'spring-boot-starter-amqp', version: '2.1.0.RELEASE'

(2)application.yml 配置开启ACK

server.port=8888
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
# 开启发送确认
spring.rabbitmq.publisher-confirms=true
# 开启发送失败退回
spring.rabbitmq.publisher-returns=true
# 开启ACK
spring.rabbitmq.listener.direct.acknowledge-mode=manual
spring.rabbitmq.listener.simple.acknowledge-mode=manual

(3)MQconfig 配置topicExchange

package com.example.demo.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {



  //EXCHANGE
  public static final String TEST_EXCHANGE = "COM.TEST.EXCHANGE";
  //queue
  public static final String TEST_QUEUE = "COM.TEST.QUEUE";



  /**
   * 配置交换机.
   */
  @Bean
  public TopicExchange getExchange() {
    return new TopicExchange(TEST_EXCHANGE);
  }

  /**
   * 配置队列.
   */
  @Bean
  public Queue getQueue() {
    return new Queue(TEST_QUEUE);
  }

  /**
   * 绑定exchange queue routekey.
   *
   * @param queueMessage 队列
   * @param exchange     交换机
   * @param routekey     路由
   * @return
   */
  public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) {
    return BindingBuilder.bind(queueMessage).to(exchange).with(routekey);
  }


  /**
   * 绑定队列与exchange.
   *
   * @return
   */
  @Bean
  public Binding agencyChangeBinding() {
    return bindingExchange(
        getQueue(),
        getExchange(),
        RabbitConfig.TEST_QUEUE);
  }

}

(4)producer(消息发送者)

package com.example.demo.controller;

import com.example.demo.config.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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;

@RestController
@RequestMapping("/v1/api/company")
public class Producer implements RabbitTemplate.ReturnCallback {
  @Autowired
  private RabbitTemplate rabbitTemplate;

  @GetMapping("test")
  public void testSendMq() {
    Logger log = LoggerFactory.getLogger(RabbitTemplate.class);

    rabbitTemplate.setReturnCallback(this);
    rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
      if (!ack) {
        System.out.println("HelloSender消息发送失败" + cause + correlationData.toString());
      } else {
        System.out.println("HelloSender 消息发送成功 ");
      }
    });
  
    rabbitTemplate.convertAndSend(
        RabbitConfig.TEST_EXCHANGE, RabbitConfig.TEST_QUEUE, "45678998767909878987");
  }

  @Override
  public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
    System.out.println("sender return success" + message.toString() + "===" + replyCode + "===" + replyText + "===" + exchange);

  }
}


(5)comsumer(消息消费)

package com.example.demo.controller;

import com.example.demo.config.RabbitConfig;
import java.io.IOException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;

@Component
public class Comsumer {


  @RabbitHandler
  @RabbitListener(queues = RabbitConfig.TEST_QUEUE)
  public void aa(String nnnn,Channel channel, Message message){
    try {
      channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    } catch (IOException e) {
      e.printStackTrace();
      //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);

    }

    System.out.println(nnnn);
  }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值