Java Code Examples for com.rabbitmq.client.Channel#basicRecover()

The following examples show how to use com.rabbitmq.client.Channel#basicRecover() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BookHandler.java    From springboot-learning-experience with Apache License 2.0 6 votes vote down vote up
/**
 * <p>TODO 该方案是 spring-boot-data-amqp 默认的方式,不太推荐。具体推荐使用  listenerManualAck()</p>
 * 默认情况下,如果没有配置手动ACK, 那么Spring Data AMQP 会在消息消费完毕后自动帮我们去ACK
 * 存在问题:如果报错了,消息不会丢失,但是会无限循环消费,一直报错,如果开启了错误日志很容易就吧磁盘空间耗完
 * 解决方案:手动ACK,或者try-catch 然后在 catch 里面讲错误的消息转移到其它的系列中去
 * spring.rabbitmq.listener.simple.acknowledge-mode=manual
 * <p>
 *
 * @param book 监听的内容
 */
@RabbitListener(queues = {RabbitConfig.DEFAULT_BOOK_QUEUE})
public void listenerAutoAck(Book book, Message message, Channel channel) {
    // TODO 如果手动ACK,消息会被监听消费,但是消息在队列中依旧存在,如果 未配置 acknowledge-mode 默认是会在消费完毕后自动ACK掉
    final long deliveryTag = message.getMessageProperties().getDeliveryTag();
    try {
        log.info("[listenerAutoAck 监听的消息] - [{}]", book.toString());
        // TODO 通知 MQ 消息已被成功消费,可以ACK了
        channel.basicAck(deliveryTag, false);
    } catch (IOException e) {
        try {
            // TODO 处理失败,重新压入MQ
            channel.basicRecover();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
 
Example 2
Source File: QueueTwoHandler.java    From spring-boot-demo with MIT License 6 votes vote down vote up
@RabbitHandler
public void directHandlerManualAck(MessageStruct messageStruct, Message message, Channel channel) {
    //  如果手动ACK,消息会被监听消费,但是消息在队列中依旧存在,如果 未配置 acknowledge-mode 默认是会在消费完毕后自动ACK掉
    final long deliveryTag = message.getMessageProperties().getDeliveryTag();
    try {
        log.info("队列2,手动ACK,接收消息:{}", JSONUtil.toJsonStr(messageStruct));
        // 通知 MQ 消息已被成功消费,可以ACK了
        channel.basicAck(deliveryTag, false);
    } catch (IOException e) {
        try {
            // 处理失败,重新压入MQ
            channel.basicRecover();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
 
Example 3
Source File: DelayQueueHandler.java    From spring-boot-demo with MIT License 6 votes vote down vote up
@RabbitHandler
public void directHandlerManualAck(MessageStruct messageStruct, Message message, Channel channel) {
    //  如果手动ACK,消息会被监听消费,但是消息在队列中依旧存在,如果 未配置 acknowledge-mode 默认是会在消费完毕后自动ACK掉
    final long deliveryTag = message.getMessageProperties().getDeliveryTag();
    try {
        log.info("延迟队列,手动ACK,接收消息:{}", JSONUtil.toJsonStr(messageStruct));
        // 通知 MQ 消息已被成功消费,可以ACK了
        channel.basicAck(deliveryTag, false);
    } catch (IOException e) {
        try {
            // 处理失败,重新压入MQ
            channel.basicRecover();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
 
Example 4
Source File: QueueThreeHandler.java    From spring-boot-demo with MIT License 6 votes vote down vote up
@RabbitHandler
public void directHandlerManualAck(MessageStruct messageStruct, Message message, Channel channel) {
    //  如果手动ACK,消息会被监听消费,但是消息在队列中依旧存在,如果 未配置 acknowledge-mode 默认是会在消费完毕后自动ACK掉
    final long deliveryTag = message.getMessageProperties().getDeliveryTag();
    try {
        log.info("队列3,手动ACK,接收消息:{}", JSONUtil.toJsonStr(messageStruct));
        // 通知 MQ 消息已被成功消费,可以ACK了
        channel.basicAck(deliveryTag, false);
    } catch (IOException e) {
        try {
            // 处理失败,重新压入MQ
            channel.basicRecover();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
 
Example 5
Source File: DirectQueueOneHandler.java    From spring-boot-demo with MIT License 6 votes vote down vote up
@RabbitHandler
public void directHandlerManualAck(MessageStruct messageStruct, Message message, Channel channel) {
    //  如果手动ACK,消息会被监听消费,但是消息在队列中依旧存在,如果 未配置 acknowledge-mode 默认是会在消费完毕后自动ACK掉
    final long deliveryTag = message.getMessageProperties().getDeliveryTag();
    try {
        log.info("直接队列1,手动ACK,接收消息:{}", JSONUtil.toJsonStr(messageStruct));
        // 通知 MQ 消息已被成功消费,可以ACK了
        channel.basicAck(deliveryTag, false);
    } catch (IOException e) {
        try {
            // 处理失败,重新压入MQ
            channel.basicRecover();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}