public void setEncoding(String encoding) { if (this.amqpTemplate instanceof RabbitTemplate) { ((RabbitTemplate) this.amqpTemplate).setEncoding(encoding); } else if (logger.isInfoEnabled()) { logger.info("AmqpTemplate is not a RabbitTemplate, so configured 'encoding' value will be ignored."); } }
public void setEncoding(String encoding) { if (this.amqpTemplate instanceof RabbitTemplate) { ((RabbitTemplate) this.amqpTemplate).setEncoding(encoding); } else if (logger.isInfoEnabled()) { logger.info("AmqpTemplate is not a RabbitTemplate, so configured 'encoding' value will be ignored."); } }
/** * 因为要设置回调类,所以应是prototype类型,如果是singleton类型,则回调类为最后一次设置 * ConfirmCallback接口用于实现消息发送到RabbitMQ交换器后接收ack回调 即消息发送到exchange ack * ReturnCallback接口用于实现消息发送到RabbitMQ 交换器,但无相应队列与交换器绑定时的回调 即消息发送不到任何一个队列中 ack */ @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Bean public RabbitTemplate rabbitTemplate() { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory()); rabbitTemplate.setEncoding("UTF-8"); rabbitTemplate.setMandatory(true); //相应交换机接收后异步回调 rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> { if (ack) { log.info("交换机接收信息成功,id:{}", correlationData.getId()); } else { log.error("交换机接收信息失败:{}", cause); } }); //无相应队列与交换机绑定异步回调 rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> { String msg = new String(message.getBody()); log.error("消息:{} 发送失败, 应答码:{} 原因:{} 交换机:{} 路由键:{}", msg, replyCode, replyText, exchange, routingKey); }); return rabbitTemplate; } }
rabbitTemplate.setEncoding("UTF-8");
rabbitTemplate.setEncoding("UTF-8");