天天看點

RabbitMQ:實作消息确認與消息傳回

不了解​

​RabbitMQ​

​的消息确認與消息傳回可以參考下面兩篇部落格:

  • ​​RabbitMQ:消息确認機制​​
  • ​​RabbitMQ:Return消息機制​​

​pom.xml​

​:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
    </parent>

    <packaging>jar</packaging>

    <groupId>com.kaven</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>springboot</name>
    <description>springboot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>      

​application.properties​

​:

spring.rabbitmq.host=192.168.1.9
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtualHost=/
spring.rabbitmq.exchange=""
spring.rabbitmq.routingKey=kaven
spring.rabbitmq.queue=kaven      

​RabbitMQProperties​

​​類(​

​RabbitMQ​

​的參數類):

package com.kaven.springboot.rabbitmq;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.rabbitmq")
@Setter
@Getter
public class RabbitMQProperties {

    private String host;
    private int port;
    private String username;
    private String password;
    private String exchange;
    private String queue;
    private String routingKey;
    private String virtualHost;
}      

​RabbitMQConfirmCallbackAndReturnsCallback​

​類(用于消息确認與消息傳回的回調):

package com.kaven.springboot.rabbitmq;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQConfirmCallbackAndReturnsCallback implements RabbitTemplate.ConfirmCallback,
        RabbitTemplate.ReturnsCallback {

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            System.out.printf("%s 消息成功發送到RabbitMQ\n", correlationData.getId());
            ReturnedMessage returned = correlationData.getReturned();
            if(returned != null) {
                returnedMessage(returned);
            }

        } else {
            System.out.printf("%s 消息發送到RabbitMQ失敗, %s\n", correlationData.getId(), cause);
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returned) {
        System.out.printf("%s %s %s %s %d\n",
                returned.getExchange(),
                returned.getRoutingKey(),
                returned.getMessage(),
                returned.getReplyText(),
                returned.getReplyCode()
        );
    }
}      

​RabbitMQConfig​

​​類(定義​

​RabbitMQ​

​元件的配置類):

package com.kaven.springboot.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import javax.annotation.Resource;

@Configuration
public class RabbitMQConfig {

    @Resource
    private RabbitMQProperties properties;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(properties.getHost(), properties.getPort());
        connectionFactory.setUsername(properties.getUsername());
        connectionFactory.setPassword(properties.getPassword());
        connectionFactory.setVirtualHost(properties.getVirtualHost());
        // 連接配接工廠開啟消息确認和消息傳回機制
        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
        connectionFactory.setPublisherReturns(true);
        return connectionFactory;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    //必須是prototype類型
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory,
                                         RabbitMQConfirmCallbackAndReturnsCallback confirmCallbackAndReturnsCallback) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        // RabbitTemplate設定消息确認和消息傳回的回調執行個體
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(confirmCallbackAndReturnsCallback);
        rabbitTemplate.setReturnsCallback(confirmCallbackAndReturnsCallback);
        return rabbitTemplate;
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(properties.getExchange());
    }

    @Bean
    public Queue queue() {
        //隊列持久
        return new Queue(properties.getQueue(), true);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with(properties.getRoutingKey());
    }
}      

​Producer​

​類(用于釋出消息):

package com.kaven.springboot.rabbitmq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Component
public class Producer {

    private final RabbitTemplate rabbitTemplate;

    @Resource
    private RabbitMQProperties properties;

    @Autowired
    public Producer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMsg(String msg, boolean returned) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration("10000");
        Message message = new Message(msg.getBytes(StandardCharsets.UTF_8), messageProperties);
        // 如果returned為true
        // 發送的消息不會路由到隊列
        // 因為路由鍵properties.getRoutingKey() + "-returned"沒有綁定的隊列
        // 是以會觸發消息傳回
        if(returned) {
            rabbitTemplate.send(properties.getExchange(),
                    properties.getRoutingKey() + "-returned",
                    message, correlationId);
        }
        else {
            rabbitTemplate.send(properties.getExchange(), properties.getRoutingKey(), message, correlationId);
        }
    }
}      

​Consumer​

​類(用于消費消息):

package com.kaven.springboot.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    @RabbitListener(queues = {"${spring.rabbitmq.queue}"})
    public void process(String msg) {
        System.out.println("接收消息: " + msg);
    }
}      

​ProducerController​

​類(用于釋出消息的接口):

package com.kaven.springboot.rabbitmq;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ProducerController {

    @Resource
    private Producer producer;

    @GetMapping("/send")
    public String send(String msg, boolean returned) {
        producer.sendMsg(msg, returned);
        return "發送消息成功";
    }
}      

啟動類:

package com.kaven.springboot;

import com.kaven.springboot.rabbitmq.RabbitMQProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

@SpringBootApplication
@ConfigurationPropertiesScan(basePackageClasses = {RabbitMQProperties.class})
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(SpringbootApplication.class);
        application.run(args);
    }
}      

啟動應用,​

​Spring Boot​

​​會與​

​RabbitMQ​

​建立連接配接。

RabbitMQ:實作消息确認與消息傳回
RabbitMQ:實作消息确認與消息傳回

請求接口。

RabbitMQ:實作消息确認與消息傳回

控制台輸出:

// 消息确認
a9f3afef-72c5-406a-8e75-e9af46a89381 消息成功發送到RabbitMQ
接收消息: "我不會觸發消息傳回,但可以觸發消息确認"      

消息成功發送到​

​RabbitMQ​

​觸發了消息确認,并且可路由,是以不會觸發消息傳回。

RabbitMQ:實作消息确認與消息傳回

再次請求接口。

RabbitMQ:實作消息确認與消息傳回

控制台輸出:

// 消息傳回
"" kaven-returned (Body:'[B@6d598368(byte[53])' MessageProperties [headers={spring_returned_message_correlation=34c045fb-7706-4aca-b775-7befa6c9be01}, contentType=application/octet-stream, contentLength=0, receivedDeliveryMode=PERSISTENT, expiration=10000, priority=0, deliveryTag=0]) NO_ROUTE 312

// 消息确認
34c045fb-7706-4aca-b775-7befa6c9be01 消息成功發送到RabbitMQ
"" kaven-returned (Body:'[B@6d598368(byte[53])' MessageProperties [headers={spring_listener_return_correlation=2facdfdd-f4ec-4625-ae0b-6f913798eb29, spring_returned_message_correlation=34c045fb-7706-4aca-b775-7befa6c9be01}, contentType=application/octet-stream, contentLength=0, receivedDeliveryMode=PERSISTENT, expiration=10000, priority=0, redelivered=false, receivedExchange="", receivedRoutingKey=kaven-returned, deliveryTag=0]) NO_ROUTE 312      

消息成功發送到​

​RabbitMQ​

​觸發了消息确認。

RabbitMQ:實作消息确認與消息傳回

但不可路由,是以也會觸發消息傳回。

RabbitMQ:實作消息确認與消息傳回