1.RabbitMQRely on

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

2. The configurationRabbitMQ

2.1 configurationRabbitMQThe basic information

spring:
  rabbitmq:
    host: 192.16879.132.
    username: admin
    password: admin
    port: 5672
    virtual-host: /
    connection-timeout: 15000
    # Send confirmation
    publisherConfirmType: CORRELATED
    # Enable route failure callback
    publisher-returns: true
    template:
      # must be set to true to notify listeners of message routing failure rather than discarding messages
      mandatory: true
      retry:
        #enabled: Enables failed retry
        enabled: true
        # The interval between first retries
        initial-interval: 10000ms
        # Maximum retry interval after which no more retries will be performed
        max-interval: 30000ms
        # Multiple of the next retry interval. In this case, 2 means the next retry interval is twice as long as the last retry interval
        multiplier: 2
    listener:
      simple:
        # Set manual sign-in messages
        acknowledge-mode: manual
Copy the code

2.2 configurationRabbitMQMessage converter and callback listener

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** * com.avatar.rabbit.config * Description: * RabbitMQ configuration **@author jack
 * @date2021/08/01 19:10 * /
@Configuration
public class RabbitConfig {

    @Bean
    public MessageConverter messageConverter(a) {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        /* Mandatory is true to listen for messages that are not routable. If false is set, messages that are not routable are deleted. * /
        rabbitTemplate.setMandatory(true);
        /* * ConfirmCallback (); ReturnCallback (); ReturnCallback (); This is not triggered when exchange does not exist. 2. When the Exchange is present, the routingKey does not match. If exchange does not exist, ConfirmCallback () is triggered * 2. If exchange does exist, routingKey does not match, ReturnCallback () is triggered first. ConfirmCallback () * 3. If the Exchange is present and the routingKey matches, only ConfirmCallback () */ will be triggered
        rabbitTemplate.setConfirmCallback(new RabbitConfirmCallback());
        rabbitTemplate.setReturnCallback(new RabbitReturnCallback());
        returnrabbitTemplate; }}Copy the code

2.3 Listening for Messages sent toBroker

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/ * * * com. Avatar. Rabbit. Callback * Description: * to monitor whether the message sent to the broker * *@author jack
 * @date2021/08/01 outer * /
@Slf4j
public class RabbitConfirmCallback implements RabbitTemplate.ConfirmCallback {

    / * * *@param correlationData null
     * @paramWhether the ACK is sent to the broker *@paramCause Cause of failure */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("***************confirmCallback begin***************");
        if (ack) {
            log.info(ConfirmCallback: message sent successfully: ack:{}", ack);
        } else {
            log.error("ConfirmCallback: failed to send message: ack:{}", ack);
            log.error("ConfirmCallback: failure cause:{}", cause);
        }
        log.info("***************confirmCallback end***************"); }}Copy the code

2.4 Listening for messages to be routed to queues

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/ * * * com. Avatar. Rabbit. Callback * Description: * * * whether listening news routing@author jack
 * @date2020/11/21 wentest * /
@Slf4j
public class RabbitReturnCallback implements RabbitTemplate.ReturnCallback {
    /** * to view the description: {@linkplain RabbitConfig#createRabbitTemplate(ConnectionFactory connectionFactory)}
     */
    @SuppressWarnings("NullableProblems")
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.error("***************returnCallback begin***************");
        log.error("ReturnCallback: response code: {}", replyCode);
        log.error("ReturnCallback: response message: {}", replyText);
        log.error("ReturnCallback: message: {}", JSON.toJSONString(message));
        log.error("ReturnCallback:     交换机:{}", exchange);
        log.error("ReturnCallback: routing key: {}", routingKey);
        log.error("***************returnCallback end***************"); }}Copy the code

3. Use RabbitTemplate to send messages

@SpringBootTest
public class RabbitSendTest {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Test
    public void send(a) {
        rabbitTemplate.convertAndSend("test-directExchange"."test.send"."Hello"); }}Copy the code