Search in sources :

Example 1 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer in project spring-integration by spring-projects.

the class DispatcherHasNoSubscribersTests method insertMockLoggerInListener.

private List<String> insertMockLoggerInListener(PublishSubscribeAmqpChannel channel) {
    SimpleMessageListenerContainer container = TestUtils.getPropertyValue(channel, "container", SimpleMessageListenerContainer.class);
    Log logger = mock(Log.class);
    final ArrayList<String> logList = new ArrayList<String>();
    doAnswer(invocation -> {
        String message = invocation.getArgument(0);
        if (message.startsWith("Dispatcher has no subscribers")) {
            logList.add(message);
        }
        return null;
    }).when(logger).warn(anyString(), any(Exception.class));
    when(logger.isWarnEnabled()).thenReturn(true);
    Object listener = container.getMessageListener();
    DirectFieldAccessor dfa = new DirectFieldAccessor(listener);
    dfa.setPropertyValue("logger", logger);
    return logList;
}
Also used : Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 2 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer in project spring-integration by spring-projects.

the class InboundEndpointTests method testRetryWithinOnMessageGateway.

@Test
public void testRetryWithinOnMessageGateway() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    AmqpInboundGateway adapter = new AmqpInboundGateway(container);
    adapter.setRequestChannel(new DirectChannel());
    adapter.setRetryTemplate(new RetryTemplate());
    QueueChannel errors = new QueueChannel();
    ErrorMessageSendingRecoverer recoveryCallback = new ErrorMessageSendingRecoverer(errors);
    recoveryCallback.setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
    adapter.setRecoveryCallback(recoveryCallback);
    adapter.afterPropertiesSet();
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    listener.onMessage(org.springframework.amqp.core.MessageBuilder.withBody("foo".getBytes()).andProperties(new MessageProperties()).build(), null);
    Message<?> errorMessage = errors.receive(0);
    assertNotNull(errorMessage);
    assertThat(errorMessage.getPayload(), instanceOf(MessagingException.class));
    MessagingException payload = (MessagingException) errorMessage.getPayload();
    assertThat(payload.getMessage(), containsString("Dispatcher has no"));
    assertThat(StaticMessageHeaderAccessor.getDeliveryAttempt(payload.getFailedMessage()).get(), equalTo(3));
    org.springframework.amqp.core.Message amqpMessage = errorMessage.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, org.springframework.amqp.core.Message.class);
    assertThat(amqpMessage, notNullValue());
    assertNull(errors.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageProperties(org.springframework.amqp.core.MessageProperties) ErrorMessageSendingRecoverer(org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer) AmqpMessageHeaderErrorMessageStrategy(org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 3 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer in project spring-integration by spring-projects.

the class InboundEndpointTests method testRetryWithinOnMessageAdapter.

@Test
public void testRetryWithinOnMessageAdapter() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    adapter.setOutputChannel(new DirectChannel());
    adapter.setRetryTemplate(new RetryTemplate());
    QueueChannel errors = new QueueChannel();
    ErrorMessageSendingRecoverer recoveryCallback = new ErrorMessageSendingRecoverer(errors);
    recoveryCallback.setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
    adapter.setRecoveryCallback(recoveryCallback);
    adapter.afterPropertiesSet();
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    listener.onMessage(org.springframework.amqp.core.MessageBuilder.withBody("foo".getBytes()).andProperties(new MessageProperties()).build(), null);
    Message<?> errorMessage = errors.receive(0);
    assertNotNull(errorMessage);
    assertThat(errorMessage.getPayload(), instanceOf(MessagingException.class));
    MessagingException payload = (MessagingException) errorMessage.getPayload();
    assertThat(payload.getMessage(), containsString("Dispatcher has no"));
    assertThat(StaticMessageHeaderAccessor.getDeliveryAttempt(payload.getFailedMessage()).get(), equalTo(3));
    org.springframework.amqp.core.Message amqpMessage = errorMessage.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, org.springframework.amqp.core.Message.class);
    assertThat(amqpMessage, notNullValue());
    assertNull(errors.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageProperties(org.springframework.amqp.core.MessageProperties) ErrorMessageSendingRecoverer(org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer) AmqpMessageHeaderErrorMessageStrategy(org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 4 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer in project spring-integration by spring-projects.

the class ChannelTests method messageConversionTests.

@Test
public void messageConversionTests() throws Exception {
    RabbitTemplate amqpTemplate = new RabbitTemplate(this.connectionFactory);
    MessageConverter messageConverter = mock(MessageConverter.class);
    amqpTemplate.setMessageConverter(messageConverter);
    PointToPointSubscribableAmqpChannel channel = new PointToPointSubscribableAmqpChannel("testConvertFail", new SimpleMessageListenerContainer(this.connectionFactory), amqpTemplate);
    channel.afterPropertiesSet();
    MessageListener listener = TestUtils.getPropertyValue(channel, "container.messageListener", MessageListener.class);
    willThrow(new MessageConversionException("foo", new IllegalStateException("bar"))).given(messageConverter).fromMessage(any(org.springframework.amqp.core.Message.class));
    this.exception.expect(MessageConversionException.class);
    this.exception.expectCause(instanceOf(IllegalStateException.class));
    listener.onMessage(mock(org.springframework.amqp.core.Message.class));
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) MessageListener(org.springframework.amqp.core.MessageListener) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) Test(org.junit.Test)

Example 5 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer in project spring-integration by spring-projects.

the class Amqp method inboundGateway.

/**
 * Create an initial {@link AmqpInboundGatewaySpec}.
 * @param connectionFactory the connectionFactory.
 * @param queueNames the queueNames.
 * @return the AmqpInboundGatewaySpec.
 */
public static AmqpInboundGatewaySMLCSpec inboundGateway(ConnectionFactory connectionFactory, String... queueNames) {
    SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer(connectionFactory);
    listenerContainer.setQueueNames(queueNames);
    return inboundGateway(listenerContainer);
}
Also used : SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)

Aggregations

SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)29 Test (org.junit.Test)13 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)10 BeanFactory (org.springframework.beans.factory.BeanFactory)8 DirectChannel (org.springframework.integration.channel.DirectChannel)8 QueueChannel (org.springframework.integration.channel.QueueChannel)8 Channel (com.rabbitmq.client.Channel)7 MessageProperties (org.springframework.amqp.core.MessageProperties)7 Connection (org.springframework.amqp.rabbit.connection.Connection)7 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)7 Bean (org.springframework.context.annotation.Bean)6 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)5 Message (org.springframework.messaging.Message)5 PollableChannel (org.springframework.messaging.PollableChannel)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)4 AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)4 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)4 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)4