Search in sources :

Example 1 with CorrelationData

use of org.springframework.amqp.rabbit.connection.CorrelationData in project java-study by q920447939.

the class RabbitmqConfig method rabbitTemplate.

@Bean
public RabbitTemplate rabbitTemplate() {
    connectionFactory.setPublisherConfirms(true);
    connectionFactory.setPublisherReturns(true);
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    rabbitTemplate.setMandatory(true);
    rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {

        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause);
        }
    });
    rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {

        @Override
        public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
            log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey, replyCode, replyText, message);
        }
    });
    return rabbitTemplate;
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) CorrelationData(org.springframework.amqp.rabbit.connection.CorrelationData) Message(org.springframework.amqp.core.Message) Bean(org.springframework.context.annotation.Bean)

Example 2 with CorrelationData

use of org.springframework.amqp.rabbit.connection.CorrelationData in project spring-parent by mingyang66.

the class RabbitLazySender method sendMsg.

/**
 * 发送消息
 *
 * @param exchange   交换器
 * @param route      路由键
 * @param message    消息
 * @param properties
 */
public void sendMsg(String exchange, String routingKey, String message, MessageProperties properties) {
    /**
     * 设置生产者消息publish-confirm回调函数
     */
    this.rabbitTemplate.setConfirmCallback(confirmCallback);
    /**
     * 设置消息退回回调函数
     */
    this.rabbitTemplate.setReturnCallback(returnCallback);
    /**
     * 新增消息转换完成后、发送之前的扩展点
     */
    this.rabbitTemplate.setBeforePublishPostProcessors(messagePostProcessor);
    try {
        if (null == properties) {
            properties = new MessageProperties();
        }
        /**
         * 设置消息唯一标识
         */
        properties.setMessageId(UUID.randomUUID().toString());
        /**
         * 创建消息包装对象
         */
        Message msg = MessageBuilder.withBody(message.getBytes()).andProperties(properties).build();
        /**
         * 将消息主题和属性封装在Message类中
         */
        Message returnedMessage = MessageBuilder.withBody(message.getBytes()).build();
        /**
         * 相关数据
         */
        CorrelationData correlationData = new CorrelationData();
        /**
         * 消息ID,全局唯一
         */
        correlationData.setId(msg.getMessageProperties().getMessageId());
        /**
         * 设置此相关数据的返回消息
         */
        correlationData.setReturnedMessage(returnedMessage);
        /**
         * 如果msg是org.springframework.amqp.core.Message对象的实例,则直接返回,否则转化为Message对象
         */
        this.rabbitTemplate.convertAndSend(exchange, routingKey, msg, correlationData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CorrelationData(org.springframework.amqp.rabbit.connection.CorrelationData) AmqpException(org.springframework.amqp.AmqpException)

Example 3 with CorrelationData

use of org.springframework.amqp.rabbit.connection.CorrelationData in project spring-parent by mingyang66.

the class RabbitSender method sendMsg.

/**
 * 发送消息
 *
 * @param exchange   交换器
 * @param route      路由键
 * @param message    消息
 * @param properties
 */
public void sendMsg(String exchange, String routingKey, String message, MessageProperties properties) {
    /**
     * 设置生产者消息publish-confirm回调函数
     */
    this.rabbitTemplate.setConfirmCallback(confirmCallback);
    /**
     * 设置消息退回回调函数
     */
    this.rabbitTemplate.setReturnCallback(returnCallback);
    /**
     * 新增消息转换完成后、发送之前的扩展点
     */
    this.rabbitTemplate.setBeforePublishPostProcessors(messagePostProcessor);
    try {
        if (null == properties) {
            properties = new MessageProperties();
        }
        /**
         * 设置消息唯一标识
         */
        properties.setMessageId(UUID.randomUUID().toString());
        /**
         * 创建消息包装对象
         */
        Message msg = MessageBuilder.withBody(message.getBytes()).andProperties(properties).build();
        /**
         * 将消息主题和属性封装在Message类中
         */
        Message returnedMessage = MessageBuilder.withBody(message.getBytes()).build();
        /**
         * 相关数据
         */
        CorrelationData correlationData = new CorrelationData();
        /**
         * 消息ID,全局唯一
         */
        correlationData.setId(msg.getMessageProperties().getMessageId());
        /**
         * 设置此相关数据的返回消息
         */
        correlationData.setReturnedMessage(returnedMessage);
        /**
         * 如果msg是org.springframework.amqp.core.Message对象的实例,则直接返回,否则转化为Message对象
         */
        this.rabbitTemplate.convertAndSend(exchange, routingKey, msg, correlationData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CorrelationData(org.springframework.amqp.rabbit.connection.CorrelationData) AmqpException(org.springframework.amqp.AmqpException)

Example 4 with CorrelationData

use of org.springframework.amqp.rabbit.connection.CorrelationData in project demo-SpringBoot by Max-Qiu.

the class IndexController method publisherConfirms.

/**
 * 发布确认生产者
 */
@GetMapping("publisherConfirms")
public void publisherConfirms() {
    rabbitTemplate.convertAndSend("publisher.confirms.exchange", "key1", "message", new CorrelationData("1"));
    rabbitTemplate.convertAndSend("publisher.confirms.exchange1", "key1", "message", new CorrelationData("2"));
    rabbitTemplate.convertAndSend("publisher.confirms.exchange", "key2", "message", new CorrelationData("3"));
}
Also used : CorrelationData(org.springframework.amqp.rabbit.connection.CorrelationData) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with CorrelationData

use of org.springframework.amqp.rabbit.connection.CorrelationData in project demo-SpringBoot by Max-Qiu.

the class IndexController method backupExchange.

/**
 * 备份交换机生产者
 */
@GetMapping("backupExchange")
public Integer backupExchange() {
    // 让消息绑定一个 id 值
    CorrelationData correlationData1 = new CorrelationData(UUID.randomUUID().toString());
    rabbitTemplate.convertAndSend("confirm.exchange", "key1", i, correlationData1);
    System.out.println("发送消息 id 为:" + correlationData1.getId() + "\t内容为:" + i);
    CorrelationData correlationData2 = new CorrelationData(UUID.randomUUID().toString());
    rabbitTemplate.convertAndSend("confirm.exchange", "key2", i, correlationData2);
    System.out.println("发送消息 id 为:" + correlationData2.getId() + "\t内容为:" + i);
    return i++;
}
Also used : CorrelationData(org.springframework.amqp.rabbit.connection.CorrelationData) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

CorrelationData (org.springframework.amqp.rabbit.connection.CorrelationData)74 Test (org.junit.jupiter.api.Test)34 CountDownLatch (java.util.concurrent.CountDownLatch)24 CachingConnectionFactory (org.springframework.amqp.rabbit.connection.CachingConnectionFactory)19 Channel (com.rabbitmq.client.Channel)15 Message (org.springframework.amqp.core.Message)14 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)12 ExecutorService (java.util.concurrent.ExecutorService)11 Connection (com.rabbitmq.client.Connection)9 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 PublisherCallbackChannelImpl (org.springframework.amqp.rabbit.connection.PublisherCallbackChannelImpl)8 ArrayList (java.util.ArrayList)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 AmqpException (org.springframework.amqp.AmqpException)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 MessagePostProcessor (org.springframework.amqp.core.MessagePostProcessor)6 MessageProperties (org.springframework.amqp.core.MessageProperties)6