RabbitMessageFuture future = asyncTemplate.new RabbitMessageFuture(null, null); willReturn(future).given(asyncTemplate).sendAndReceive(anyString(), anyString(), any(org.springframework.amqp.core.Message.class));
@Override public void onSuccess(Boolean result) { try { handleConfirm(this.correlationData, result, this.replyFuture.getNackCause()); } catch (Exception e) { logger.error("Failed to send publisher confirm"); } }
((RabbitMessageFuture) future).set(message);
((RabbitMessageFuture) future).set(message);
@Override public RabbitMessageFuture sendAndReceive(String exchange, String routingKey, Message message) { String correlationId = getOrSetCorrelationIdAndSetReplyTo(message); RabbitMessageFuture future = new RabbitMessageFuture(correlationId, message); CorrelationData correlationData = null; if (this.enableConfirms) { correlationData = new CorrelationData(correlationId); future.setConfirm(new SettableListenableFuture<>()); } this.pending.put(correlationId, future); if (this.container != null) { this.template.send(exchange, routingKey, message, correlationData); } else { ChannelHolder channelHolder = this.directReplyToContainer.getChannelHolder(); future.setChannelHolder(channelHolder); sendDirect(channelHolder.getChannel(), exchange, routingKey, message, correlationData); } future.startTimer(); return future; }
@Override public RabbitMessageFuture sendAndReceive(String exchange, String routingKey, Message message) { String correlationId = getOrSetCorrelationIdAndSetReplyTo(message); RabbitMessageFuture future = new RabbitMessageFuture(correlationId, message); CorrelationData correlationData = null; if (this.enableConfirms) { correlationData = new CorrelationData(correlationId); future.setConfirm(new SettableListenableFuture<>()); } this.pending.put(correlationId, future); if (this.container != null) { this.template.send(exchange, routingKey, message, correlationData); } else { ChannelHolder channelHolder = this.directReplyToContainer.getChannelHolder(); future.setChannelHolder(channelHolder); sendDirect(channelHolder.getChannel(), exchange, routingKey, message, correlationData); } future.startTimer(); return future; }
@Override protected Object handleRequestMessage(Message<?> requestMessage) { org.springframework.amqp.core.Message amqpMessage = MappingUtils.mapMessage(requestMessage, this.messageConverter, getHeaderMapper(), getDefaultDeliveryMode(), isHeadersMappedLast()); addDelayProperty(requestMessage, amqpMessage); RabbitMessageFuture future = this.template.sendAndReceive(generateExchangeName(requestMessage), generateRoutingKey(requestMessage), amqpMessage); future.addCallback(new FutureCallback(requestMessage)); CorrelationData correlationData = generateCorrelationData(requestMessage); if (correlationData != null && future.getConfirm() != null) { future.getConfirm().addCallback(new CorrelationCallback(correlationData, future)); } return null; }
@Override public void onSuccess(Boolean result) { try { handleConfirm(this.correlationData, result, this.replyFuture.getNackCause()); } catch (Exception e) { logger.error("Failed to send publisher confirm"); } }
@Test @DirtiesContext public void testMessageWithConfirm() throws Exception { this.asyncTemplate.setEnableConfirms(true); RabbitMessageFuture future = this.asyncTemplate .sendAndReceive(new SimpleMessageConverter().toMessage("sleep", new MessageProperties())); ListenableFuture<Boolean> confirm = future.getConfirm(); assertNotNull(confirm); assertTrue(confirm.get(10, TimeUnit.SECONDS)); checkMessageResult(future, "SLEEP"); }
@Test @DirtiesContext public void testMessageWithConfirmDirect() throws Exception { this.asyncDirectTemplate.setEnableConfirms(true); RabbitMessageFuture future = this.asyncDirectTemplate .sendAndReceive(new SimpleMessageConverter().toMessage("sleep", new MessageProperties())); ListenableFuture<Boolean> confirm = future.getConfirm(); assertNotNull(confirm); assertTrue(confirm.get(10, TimeUnit.SECONDS)); checkMessageResult(future, "SLEEP"); }
@Test public void testAsyncDelayExpression() { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); AsyncRabbitTemplate amqpTemplate = spy(new AsyncRabbitTemplate(new RabbitTemplate(connectionFactory), new SimpleMessageListenerContainer(connectionFactory), "replyTo")); amqpTemplate.setTaskScheduler(mock(TaskScheduler.class)); AsyncAmqpOutboundGateway gateway = new AsyncAmqpOutboundGateway(amqpTemplate); willAnswer( invocation -> amqpTemplate.new RabbitMessageFuture("foo", invocation.getArgument(2))) .given(amqpTemplate).sendAndReceive(anyString(), anyString(), any(Message.class)); gateway.setExchangeName("foo"); gateway.setRoutingKey("bar"); gateway.setDelayExpressionString("42"); gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setOutputChannel(new NullChannel()); gateway.afterPropertiesSet(); gateway.start(); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); gateway.handleMessage(new GenericMessage<>("foo")); verify(amqpTemplate).sendAndReceive(eq("foo"), eq("bar"), captor.capture()); assertThat(captor.getValue().getMessageProperties().getDelay(), equalTo(42)); }
@Override protected Object handleRequestMessage(Message<?> requestMessage) { org.springframework.amqp.core.Message amqpMessage = MappingUtils.mapMessage(requestMessage, this.messageConverter, getHeaderMapper(), getDefaultDeliveryMode(), isHeadersMappedLast()); addDelayProperty(requestMessage, amqpMessage); RabbitMessageFuture future = this.template.sendAndReceive(generateExchangeName(requestMessage), generateRoutingKey(requestMessage), amqpMessage); future.addCallback(new FutureCallback(requestMessage)); CorrelationData correlationData = generateCorrelationData(requestMessage); if (correlationData != null && future.getConfirm() != null) { future.getConfirm().addCallback(new CorrelationCallback(correlationData, future)); } return null; }