use of org.springframework.amqp.rabbit.support.ListenerExecutionFailedException in project spring-amqp by spring-projects.
the class RabbitTemplateDirectReplyToContainerIntegrationTests method channelReleasedOnTimeout.
@SuppressWarnings("unchecked")
@Test
public void channelReleasedOnTimeout() throws Exception {
final CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
RabbitTemplate rabbitTemplate = createSendAndReceiveRabbitTemplate(connectionFactory);
rabbitTemplate.setReplyTimeout(1);
AtomicReference<Throwable> exception = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
ErrorHandler replyErrorHandler = t -> {
exception.set(t);
latch.countDown();
};
rabbitTemplate.setReplyErrorHandler(replyErrorHandler);
Object reply = rabbitTemplate.convertSendAndReceive(ROUTE, "foo");
assertThat(reply).isNull();
Object container = TestUtils.getPropertyValue(rabbitTemplate, "directReplyToContainers", Map.class).get(rabbitTemplate.isUsePublisherConnection() ? connectionFactory.getPublisherConnectionFactory() : connectionFactory);
assertThat(TestUtils.getPropertyValue(container, "inUseConsumerChannels", Map.class)).hasSize(0);
assertThat(TestUtils.getPropertyValue(container, "errorHandler")).isSameAs(replyErrorHandler);
Message replyMessage = new Message("foo".getBytes(), new MessageProperties());
assertThatThrownBy(() -> rabbitTemplate.onMessage(replyMessage, mock(Channel.class))).isInstanceOf(AmqpRejectAndDontRequeueException.class).hasMessage("No correlation header in reply");
replyMessage.getMessageProperties().setCorrelationId("foo");
assertThatThrownBy(() -> rabbitTemplate.onMessage(replyMessage, mock(Channel.class))).isInstanceOf(AmqpRejectAndDontRequeueException.class).hasMessage("Reply received after timeout");
ExecutorService executor = Executors.newFixedThreadPool(1);
// Set up a consumer to respond to our producer
executor.submit(() -> {
Message message = rabbitTemplate.receive(ROUTE, 10_000);
assertThat(message).as("No message received").isNotNull();
rabbitTemplate.send(message.getMessageProperties().getReplyTo(), replyMessage);
return message;
});
while (rabbitTemplate.receive(ROUTE, 100) != null) {
// empty
}
reply = rabbitTemplate.convertSendAndReceive(ROUTE, "foo");
assertThat(reply).isNull();
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(exception.get()).isInstanceOf(ListenerExecutionFailedException.class);
assertThat(exception.get().getCause().getMessage()).isEqualTo("Reply received after timeout");
assertThat(((ListenerExecutionFailedException) exception.get()).getFailedMessage().getBody()).isEqualTo(replyMessage.getBody());
assertThat(TestUtils.getPropertyValue(container, "inUseConsumerChannels", Map.class)).hasSize(0);
executor.shutdownNow();
rabbitTemplate.stop();
connectionFactory.destroy();
}
use of org.springframework.amqp.rabbit.support.ListenerExecutionFailedException in project spring-amqp by spring-projects.
the class MessagingMessageListenerAdapterTests method exceptionInInvocation.
@Test
public void exceptionInInvocation() {
org.springframework.amqp.core.Message message = MessageTestUtils.createTextMessage("foo");
Channel channel = mock(Channel.class);
MessagingMessageListenerAdapter listener = getSimpleInstance("wrongParam", Integer.class);
try {
listener.onMessage(message, channel);
fail("Should have thrown an exception");
} catch (ListenerExecutionFailedException ex) {
assertThat(ex.getCause().getClass()).isEqualTo(org.springframework.messaging.converter.MessageConversionException.class);
} catch (Exception ex) {
fail("Should not have thrown an " + ex.getClass().getSimpleName());
}
}
use of org.springframework.amqp.rabbit.support.ListenerExecutionFailedException in project spring-amqp by spring-projects.
the class MessagingMessageListenerAdapterTests method exceptionInListener.
@Test
public void exceptionInListener() {
org.springframework.amqp.core.Message message = MessageTestUtils.createTextMessage("foo");
Channel channel = mock(Channel.class);
MessagingMessageListenerAdapter listener = getSimpleInstance("fail", String.class);
try {
listener.onMessage(message, channel);
fail("Should have thrown an exception");
} catch (ListenerExecutionFailedException ex) {
assertThat(ex.getCause().getClass()).isEqualTo(IllegalArgumentException.class);
assertThat(ex.getCause().getMessage()).isEqualTo("Expected test exception");
} catch (Exception ex) {
fail("Should not have thrown an " + ex.getClass().getSimpleName());
}
}
use of org.springframework.amqp.rabbit.support.ListenerExecutionFailedException in project spring-amqp by spring-projects.
the class MessagingMessageListenerAdapterTests method errorHandlerAfterConversionEx.
@Test
void errorHandlerAfterConversionEx() throws Exception {
org.springframework.amqp.core.Message message = MessageTestUtils.createTextMessage("foo");
Channel channel = mock(Channel.class);
AtomicBoolean ehCalled = new AtomicBoolean();
MessagingMessageListenerAdapter listener = getSimpleInstance("fail", new RabbitListenerErrorHandler() {
@Override
public Object handleError(org.springframework.amqp.core.Message amqpMessage, Message<?> message, ListenerExecutionFailedException exception) throws Exception {
ehCalled.set(true);
return null;
}
}, false, String.class);
listener.setMessageConverter(new MessageConverter() {
@Override
public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
return null;
}
@Override
public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException {
throw new MessageConversionException("test");
}
});
listener.onMessage(message, channel);
assertThat(ehCalled.get()).isTrue();
}
use of org.springframework.amqp.rabbit.support.ListenerExecutionFailedException in project spring-amqp by spring-projects.
the class MessagingMessageListenerAdapterTests method exceptionInMultiListenerReturnException.
@Test
public void exceptionInMultiListenerReturnException() throws Exception {
org.springframework.amqp.core.Message message = MessageTestUtils.createTextMessage("foo");
Channel channel = mock(Channel.class);
MessagingMessageListenerAdapter listener = getMultiInstance("fail", "failWithReturn", true, String.class, Integer.class);
try {
listener.onMessage(message, channel);
fail("Should have thrown an exception");
} catch (ListenerExecutionFailedException ex) {
assertThat(ex.getCause().getClass()).isEqualTo(IllegalArgumentException.class);
assertThat(ex.getCause().getMessage()).isEqualTo("Expected test exception");
} catch (Exception ex) {
ex.printStackTrace();
fail("Should not have thrown an " + ex.getClass().getSimpleName());
}
message = new SimpleMessageConverter().toMessage(42, new MessageProperties());
try {
listener.onMessage(message, channel);
fail("Should have thrown an exception");
} catch (ReplyFailureException ex) {
assertThat(ex.getMessage()).contains("Failed to send reply");
} catch (Exception ex) {
fail("Should not have thrown an " + ex.getClass().getSimpleName());
}
message.getMessageProperties().setReplyTo("foo/bar");
listener.onMessage(message, channel);
verify(channel).basicPublish(eq("foo"), eq("bar"), eq(false), any(BasicProperties.class), any(byte[].class));
}
Aggregations