Spring Boot集成RabbitMQ

订阅程序

  • 源码
@Bean
public SimpleMessageListenerContainer customerMessageContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(orderConnectionFactory());
    container.setQueues(recoveryQueue());
    container.setExposeListenerChannel(true);
    container.setPrefetchCount(1);// 每次只消费1条
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL); // 设置手动应答
    container.setConcurrentConsumers(1); // Specify the number of concurrent consumers to create. Default is 1.
    container.setMessageListener(new ChannelAwareMessageListener() {
        public void onMessage(Message message, Channel channel) throws Exception {
            byte[] body = message.getBody();
            String bodyStr = new String(body);
            try {
                LOGGER.info("正在推送99单号回收汇总信息");
                dailyRecoveryGatherService.pushDailyRecoveryGather(bodyStr);
                // 确认应答(是否批量.true:将一次性ack所有小于deliveryTag的消息)
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                LOGGER.info("99单号回收汇总推送完成,消息内容:" + bodyStr);
            } catch (Exception e) {
                LOGGER.error("99单号回收汇总推送失败,消息内容:" + bodyStr, e);
                /**
                 * 拒绝应答(true表示重新入队列,
                 * false表示直接从队列中删除,此时和basicAck(long deliveryTag, false)的效果一样)
                 */
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            }
        }
    });
    return container;
}

创建管道和队列日志


org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:360) 2019-01-04 09:02:25 [INFO] Created new connection: orderConnectionFactory#368a8022:0/SimpleConnection@ef99e17 [delegate=amqp://guest@192.168.0.204:5672/, localPort= 50346]
org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:360) 2019-01-04 09:02:25 [INFO] Created new connection: SpringAMQP#38d84b3:0/SimpleConnection@19a04ff3 [delegate=amqp://guest@192.168.0.204:5672/, localPort= 50347]
org.springframework.boot.StartupInfoLogger.logStarted(StartupInfoLogger.java:57) 2019-01-04 09:02:25 [INFO] Started Application in 3.334 seconds (JVM running for 3.948)

RabbitMQ 可靠投递

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值