/** * 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); }
/** * 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); }
@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; }
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); }
/** * 因为要设置回调类,所以应是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.setMandatory(true); rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> { String correlationId = message.getMessageProperties().getCorrelationIdString();
rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter()); rabbitTemplate.setMandatory(true);
rabbitTemplate.setEncoding("UTF-8"); rabbitTemplate.setMandatory(true); rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> { String correlationId = message.getMessageProperties().getCorrelationIdString();
@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")); }
rabbitTemplate.setMandatory(true); rabbitTemplate.setExchange(exchange); rabbitTemplate.setRoutingKey(routingKey);
returnLatch.countDown(); }); template.setMandatory(true); Connection conn = cf.createConnection(); Channel channel1 = conn.createChannel(false);
@Test public void testDeferredChannelCacheAck() throws Exception { final CachingConnectionFactory cf = new CachingConnectionFactory( RabbitAvailableCondition.getBrokerRunning().getConnectionFactory()); cf.setPublisherConfirms(true); final RabbitTemplate template = new RabbitTemplate(cf); final CountDownLatch confirmLatch = new CountDownLatch(1); final AtomicInteger cacheCount = new AtomicInteger(); template.setConfirmCallback((cd, a, c) -> { cacheCount.set(TestUtils.getPropertyValue(cf, "cachedChannelsNonTransactional", List.class).size()); confirmLatch.countDown(); }); template.setMandatory(true); Connection conn = cf.createConnection(); Channel channel1 = conn.createChannel(false); Channel channel2 = conn.createChannel(false); channel1.close(); channel2.close(); conn.close(); assertThat(TestUtils.getPropertyValue(cf, "cachedChannelsNonTransactional", List.class).size()).isEqualTo(2); template.convertAndSend("", QUEUE2, "foo", new MyCD("foo")); assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(cacheCount.get()).isEqualTo(1); cf.destroy(); }
@Test public void testSendAndReceiveUndeliverable() throws Exception { this.connectionFactory.setBeanName("testSendAndReceiveUndeliverable"); template.setMandatory(true); try { template.convertSendAndReceive(ROUTE + "xxxxxxxx", "undeliverable"); fail("expected exception"); } catch (AmqpMessageReturnedException e) { assertEquals("undeliverable", new String(e.getReturnedMessage().getBody())); assertEquals("NO_ROUTE", e.getReplyText()); } assertEquals(0, TestUtils.getPropertyValue(template, "replyHolder", Map.class).size()); }
rabbitTemplate.setBeforePublishPostProcessors(this.compressingPostProcessor); rabbitTemplate.setMandatory(mandatory); // returned messages if (rabbitProperties != null && rabbitProperties.getTemplate().getRetry().isEnabled()) { Retry retry = rabbitProperties.getTemplate().getRetry();
rabbitTemplate.setBeforePublishPostProcessors(this.compressingPostProcessor); rabbitTemplate.setMandatory(mandatory); // returned messages if (rabbitProperties != null && rabbitProperties.getTemplate().getRetry().isEnabled()) { Retry retry = rabbitProperties.getTemplate().getRetry();
@Before public void create() { connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost("localhost"); connectionFactory.setChannelCacheSize(10); connectionFactory.setPort(BrokerTestUtils.getPort()); connectionFactoryWithConfirmsEnabled = new CachingConnectionFactory(); connectionFactoryWithConfirmsEnabled.setHost("localhost"); connectionFactoryWithConfirmsEnabled.setChannelCacheSize(100); connectionFactoryWithConfirmsEnabled.setPort(BrokerTestUtils.getPort()); connectionFactoryWithConfirmsEnabled.setPublisherConfirms(true); templateWithConfirmsEnabled = new RabbitTemplate(connectionFactoryWithConfirmsEnabled); connectionFactoryWithReturnsEnabled = new CachingConnectionFactory(); connectionFactoryWithReturnsEnabled.setHost("localhost"); connectionFactoryWithReturnsEnabled.setChannelCacheSize(1); connectionFactoryWithReturnsEnabled.setPort(BrokerTestUtils.getPort()); connectionFactoryWithReturnsEnabled.setPublisherReturns(true); templateWithReturnsEnabled = new RabbitTemplate(connectionFactoryWithReturnsEnabled); connectionFactoryWithConfirmsAndReturnsEnabled = new CachingConnectionFactory(); connectionFactoryWithConfirmsAndReturnsEnabled.setHost("localhost"); connectionFactoryWithConfirmsAndReturnsEnabled.setChannelCacheSize(100); connectionFactoryWithConfirmsAndReturnsEnabled.setPort(BrokerTestUtils.getPort()); connectionFactoryWithConfirmsAndReturnsEnabled.setPublisherConfirms(true); connectionFactoryWithConfirmsAndReturnsEnabled.setPublisherReturns(true); templateWithConfirmsAndReturnsEnabled = new RabbitTemplate(connectionFactoryWithConfirmsAndReturnsEnabled); templateWithConfirmsAndReturnsEnabled.setMandatory(true); }