/** * the basic.return is sent to the client before basic.ack */ @PostConstruct public void postConstruct() { // return rabbitTemplate.setReturnCallback(new RabbitReturnCallback()); // ack rabbitTemplate.setConfirmCallback(new RabbitConfirmCallback()); }
@Override protected void endpointInit() { if (getConfirmCorrelationExpression() != null) { Assert.isInstanceOf(RabbitTemplate.class, this.amqpTemplate, "RabbitTemplate implementation is required for publisher confirms"); ((RabbitTemplate) this.amqpTemplate).setConfirmCallback(this); } if (getReturnChannel() != null) { Assert.isInstanceOf(RabbitTemplate.class, this.amqpTemplate, "RabbitTemplate implementation is required for publisher confirms"); ((RabbitTemplate) this.amqpTemplate).setReturnCallback(this); } }
/** * @param mandatoryExpression a SpEL {@link Expression} to evaluate against each request * message. The result of the evaluation must be a {@code boolean} value. * @since 2.0 */ public void setMandatoryExpressionString(String mandatoryExpression) { this.template.setReturnCallback(this); this.template.setMandatoryExpressionString(mandatoryExpression); }
/** * Set to true to enable the receipt of returned messages that cannot be delivered * in the form of a {@link AmqpMessageReturnedException}. * @param mandatory true to enable returns. */ public void setMandatory(boolean mandatory) { this.template.setReturnCallback(this); this.template.setMandatory(mandatory); }
/** * @param mandatoryExpression a SpEL {@link Expression} to evaluate against each request * message. The result of the evaluation must be a {@code boolean} value. * @since 2.0 */ public void setMandatoryExpression(Expression mandatoryExpression) { this.template.setReturnCallback(this); this.template.setMandatoryExpression(mandatoryExpression); }
/** * Set to true to enable the receipt of returned messages that cannot be delivered * in the form of a {@link AmqpMessageReturnedException}. * @param mandatory true to enable returns. */ public void setMandatory(boolean mandatory) { this.template.setReturnCallback(this); this.template.setMandatory(mandatory); }
/** * @param mandatoryExpression a SpEL {@link Expression} to evaluate against each request * message. The result of the evaluation must be a {@code boolean} value. * @since 2.0 */ public void setMandatoryExpression(Expression mandatoryExpression) { this.template.setReturnCallback(this); this.template.setMandatoryExpression(mandatoryExpression); }
/** * @param mandatoryExpression a SpEL {@link Expression} to evaluate against each request * message. The result of the evaluation must be a {@code boolean} value. * @since 2.0 */ public void setMandatoryExpressionString(String mandatoryExpression) { this.template.setReturnCallback(this); this.template.setMandatoryExpressionString(mandatoryExpression); }
@Override protected void endpointInit() { if (getConfirmCorrelationExpression() != null) { Assert.isInstanceOf(RabbitTemplate.class, this.amqpTemplate, "RabbitTemplate implementation is required for publisher confirms"); ((RabbitTemplate) this.amqpTemplate).setConfirmCallback(this); } if (getReturnChannel() != null) { Assert.isInstanceOf(RabbitTemplate.class, this.amqpTemplate, "RabbitTemplate implementation is required for publisher confirms"); ((RabbitTemplate) this.amqpTemplate).setReturnCallback(this); } }
public void send() { String context = "你好现在是 " + new Date() +""; System.out.println("HelloSender发送内容 : " + context); // this.rabbitTemplate.setConfirmCallback(this); this.rabbitTemplate.setReturnCallback(this); //为保证数据一致性,应先将数据保存到数据库当中 this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> { if (!ack) { //先尝试重发机制 System.out.println("HelloSender消息发送失败" + cause + correlationData.toString()); } else { System.out.println("HelloSender 消息发送成功 "); } }); this.rabbitTemplate.convertAndSend("hello", context); }
@Bean public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) { connectionFactory.setPublisherConfirms(true); connectionFactory.setPublisherReturns(true); RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); rabbitTemplate.setMandatory(true); rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause)); rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey, replyCode, replyText, message)); return rabbitTemplate; }
@Bean public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) { connectionFactory.setPublisherConfirms(true); connectionFactory.setPublisherReturns(true); RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); rabbitTemplate.setMandatory(true); rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause)); rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey, replyCode, replyText, message)); return rabbitTemplate; }
@Bean public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory){ connectionFactory.setPublisherConfirms(true); connectionFactory.setPublisherReturns(true); RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); rabbitTemplate.setMandatory(true); rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> log.info("消息发送成功:correlationData({}),ack({}),cause({})",correlationData,ack,cause)); rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> log.info("消息丢失: message({}),replyCode({}),replytext({}),exchange({}),routingKey({})",message,replyCode,replyText,exchange,routingKey)); return rabbitTemplate; }
private void setupCallbacks() { /* * Confirms/returns enabled in application.properties - add the callbacks here. */ this.rabbitTemplate.setConfirmCallback((correlation, ack, reason) -> { if (correlation != null) { System.out.println("Received " + (ack ? " ack " : " nack ") + "for correlation: " + correlation); } this.confirmLatch.countDown(); }); this.rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> { System.out.println("Returned: " + message + "\nreplyCode: " + replyCode + "\nreplyText: " + replyText + "\nexchange/rk: " + exchange + "/" + routingKey); this.returnLatch.countDown(); }); /* * Replace the correlation data with one containing the converted message in case * we want to resend it after a nack. */ this.rabbitTemplate.setCorrelationDataPostProcessor((message, correlationData) -> new CompleteMessageCorrelationData(correlationData != null ? correlationData.getId() : null, message)); }
public void sendMessage(String context) { // 消息发送失败返回到队列中, application.properties 配置 spring.rabbitmq.publisher-returns=true rabbitTemplate.setMandatory(true); this.rabbitTemplate.setReturnCallback(this); this.rabbitTemplate.setConfirmCallback(this); this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> { if (!ack) { logger.info("HelloSender 发送失败:" + cause + correlationData.toString()); } else { logger.info("HelloSender 发送成功"); } }); logger.info("HelloSender 发送的消息内容:{}", context); this.rabbitTemplate.convertAndSend("hello", context); }
public void sendSimpleMessage(Map<String, Object> headers, Object message, String messageId, String exchangeName, String key) { // 自定义消息头 MessageHeaders messageHeaders = new MessageHeaders(headers); // 创建消息 Message<Object> msg = MessageBuilder.createMessage(message, messageHeaders); /* 确认的回调 确认消息是否到达 Broker 服务器 其实就是是否到达交换器 如果发送时候指定的交换器不存在 ack就是false 代表消息不可达 */ rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> { log.info("correlationData:{} , ack:{}", correlationData.getId(), ack); if (!ack) { System.out.println("进行对应的消息补偿机制"); } }); /* 消息失败的回调 * 例如消息已经到达交换器上,但路由键匹配任何绑定到该交换器的队列,会触发这个回调,此时 replyText: NO_ROUTE */ rabbitTemplate.setReturnCallback((message1, replyCode, replyText, exchange, routingKey) -> { log.info("message:{}; replyCode: {}; replyText: {} ; exchange:{} ; routingKey:{}", message1, replyCode, replyText, exchange, routingKey); }); // 在实际中ID 应该是全局唯一 能够唯一标识消息 消息不可达的时候触发ConfirmCallback回调方法时可以获取该值,进行对应的错误处理 CorrelationData correlationData = new CorrelationData(messageId); rabbitTemplate.convertAndSend(exchangeName, key, msg, correlationData); } }
@Test public void testPublisherReturns() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final List<Message> returns = new ArrayList<Message>(); templateWithReturnsEnabled.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> { returns.add(message); latch.countDown(); }); templateWithReturnsEnabled.setMandatory(true); templateWithReturnsEnabled.convertAndSend(ROUTE + "junk", (Object) "message", new CorrelationData("abc")); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); assertEquals(1, returns.size()); Message message = returns.get(0); assertEquals("message", new String(message.getBody(), "utf-8")); }
@Test public void testPublisherReturnsWithMandatoryExpression() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final List<Message> returns = new ArrayList<Message>(); templateWithReturnsEnabled.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> { returns.add(message); latch.countDown(); }); Expression mandatoryExpression = new SpelExpressionParser().parseExpression("'message'.bytes == body"); templateWithReturnsEnabled.setMandatoryExpression(mandatoryExpression); templateWithReturnsEnabled.convertAndSend(ROUTE + "junk", (Object) "message", new CorrelationData("abc")); templateWithReturnsEnabled.convertAndSend(ROUTE + "junk", (Object) "foo", new CorrelationData("abc")); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); assertEquals(1, returns.size()); Message message = returns.get(0); assertEquals("message", new String(message.getBody(), "utf-8")); }
confirmLatch.countDown(); }); template.setReturnCallback((m, r, rt, e, rk) -> { returnLatch.countDown(); });
CorrelationData cd4 = new CorrelationData("42"); AtomicBoolean resent = new AtomicBoolean(); this.templateWithConfirmsAndReturnsEnabled.setReturnCallback((m, r, rt, e, rk) -> { this.templateWithConfirmsEnabled.send(ROUTE, m); resent.set(true);