Search in sources :

Example 1 with ListenerExecutionFailedException

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();
}
Also used : ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MessageProperties(org.springframework.amqp.core.MessageProperties) AtomicReference(java.util.concurrent.atomic.AtomicReference) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtils(org.springframework.amqp.utils.test.TestUtils) ErrorHandler(org.springframework.util.ErrorHandler) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory) ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) Map(java.util.Map) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) ExecutorService(java.util.concurrent.ExecutorService) AmqpRejectAndDontRequeueException(org.springframework.amqp.AmqpRejectAndDontRequeueException) Mockito.mock(org.mockito.Mockito.mock) ErrorHandler(org.springframework.util.ErrorHandler) Message(org.springframework.amqp.core.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MessageProperties(org.springframework.amqp.core.MessageProperties) AmqpRejectAndDontRequeueException(org.springframework.amqp.AmqpRejectAndDontRequeueException) CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory) ExecutorService(java.util.concurrent.ExecutorService) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with ListenerExecutionFailedException

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());
    }
}
Also used : ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Channel(com.rabbitmq.client.Channel) ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Test(org.junit.jupiter.api.Test)

Example 3 with ListenerExecutionFailedException

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());
    }
}
Also used : ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) Channel(com.rabbitmq.client.Channel) ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Test(org.junit.jupiter.api.Test)

Example 4 with ListenerExecutionFailedException

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();
}
Also used : ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Message(org.springframework.messaging.Message) Channel(com.rabbitmq.client.Channel) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageProperties(org.springframework.amqp.core.MessageProperties) RabbitListenerErrorHandler(org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler) Test(org.junit.jupiter.api.Test)

Example 5 with ListenerExecutionFailedException

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));
}
Also used : ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) MessageProperties(org.springframework.amqp.core.MessageProperties) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) Channel(com.rabbitmq.client.Channel) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Test(org.junit.jupiter.api.Test)

Aggregations

ListenerExecutionFailedException (org.springframework.amqp.rabbit.support.ListenerExecutionFailedException)21 Test (org.junit.jupiter.api.Test)13 MessageProperties (org.springframework.amqp.core.MessageProperties)12 Message (org.springframework.amqp.core.Message)11 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)11 Channel (com.rabbitmq.client.Channel)10 AmqpRejectAndDontRequeueException (org.springframework.amqp.AmqpRejectAndDontRequeueException)7 CachingConnectionFactory (org.springframework.amqp.rabbit.connection.CachingConnectionFactory)6 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)5 ArrayList (java.util.ArrayList)4 ImmediateAcknowledgeAmqpException (org.springframework.amqp.ImmediateAcknowledgeAmqpException)4 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)4 Message (org.springframework.messaging.Message)4 List (java.util.List)3 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeUnit (java.util.concurrent.TimeUnit)3 AmqpIOException (org.springframework.amqp.AmqpIOException)3 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)2 IOException (java.io.IOException)2