weixin_42251775
2022-06-24 10:48
采纳率: 0%
浏览 14

RabbitTemplate回调出错

使用 RabbitTemplate 回调时出现错误,


java.lang.IllegalStateException: Only one ConfirmCallback is supported by each RabbitTemplate
    at org.springframework.util.Assert.state(Assert.java:73)
    at org.springframework.amqp.rabbit.core.RabbitTemplate.setConfirmCallback(RabbitTemplate.java:447)
    at com.itheima.pinda.goods.service.impl.ShippingServiceImpl.failMsgToRabbitmq(ShippingServiceImpl.java:676)
    at com.itheima.pinda.goods.service.impl.ShippingServiceImpl.submitToWMS(ShippingServiceImpl.java:661)
    at com.itheima.pinda.goods.service.impl.ShippingServiceImpl.submitToWMS(ShippingServiceImpl.java:543)
    at com.itheima.pinda.goods.service.impl.ShippingServiceImpl$$FastClassBySpringCGLIB$$a7f9348c.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685)
    at com.itheima.pinda.goods.service.impl.ShippingServiceImpl$$EnhancerBySpringCGLIB$$26aaa855.submitToWMS(<generated>)
    at com.itheima.pinda.goods.controller.ShippingController.submitToWMS(ShippingController.java:128)

将单例改成多例模式


 @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory)
    {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        //设置Json转换器
        rabbitTemplate.setMessageConverter(jsonMessageConverter());

        return rabbitTemplate;
    }

不起作用,然后 添加

 @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,**proxyMode = ScopedProxyMode.TARGET_CLASS)**
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory)
    {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
       
        //设置Json转换器
        rabbitTemplate.setMessageConverter(jsonMessageConverter());

        return rabbitTemplate;
    }

发现不报错了,但是回调不执行了,


  rabbitTemplate.setConfirmCallback((correlationData, b, s) -> {
                        if (b){
                            log.info("failMsgToRabbitmq消息成功发送:key= {} and value= {}",key,result.get(key));
                        }else {
                            log.info("failMsgToRabbitmq消息失败发送:key= {} and value= {},{}", key, result.get(key), s);
                        }
                    });
                    rabbitTemplate.convertAndSend(RabbitMqConfig.DIRECT_EXCHANGE_REMIND, RabbitMqConfig.REMIND_ROUTING_KEY, result);

抓狂,求指点迷津


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题