Search in sources :

Example 16 with SimpleMessageListenerContainer

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

the class InboundEndpointTests method testGatewayConversionError.

@Test
public void testGatewayConversionError() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpInboundGateway adapter = new AmqpInboundGateway(container);
    QueueChannel outputChannel = new QueueChannel();
    adapter.setRequestChannel(outputChannel);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);
    adapter.setMessageConverter(new MessageConverter() {

        @Override
        public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
            throw new MessageConversionException("intended");
        }

        @Override
        public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException {
            return null;
        }
    });
    adapter.afterPropertiesSet();
    ((ChannelAwareMessageListener) container.getMessageListener()).onMessage(null, null);
    assertNull(outputChannel.receive(0));
    assertNotNull(errorChannel.receive(0));
}
Also used : MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Example 17 with SimpleMessageListenerContainer

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

the class InboundEndpointTests method testInt2809JavaTypePropertiesToAmqp.

@Test
public void testInt2809JavaTypePropertiesToAmqp() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    adapter.setMessageConverter(new Jackson2JsonMessageConverter());
    PollableChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    Object payload = new Foo("bar1");
    Transformer objectToJsonTransformer = new ObjectToJsonTransformer();
    Message<?> jsonMessage = objectToJsonTransformer.transform(new GenericMessage<Object>(payload));
    MessageProperties amqpMessageProperties = new MessageProperties();
    amqpMessageProperties.setDeliveryTag(123L);
    org.springframework.amqp.core.Message amqpMessage = new SimpleMessageConverter().toMessage(jsonMessage.getPayload(), amqpMessageProperties);
    DefaultAmqpHeaderMapper.inboundMapper().fromHeadersToRequest(jsonMessage.getHeaders(), amqpMessageProperties);
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    Channel rabbitChannel = mock(Channel.class);
    listener.onMessage(amqpMessage, rabbitChannel);
    Message<?> result = channel.receive(1000);
    assertEquals(payload, result.getPayload());
    assertSame(rabbitChannel, result.getHeaders().get(AmqpHeaders.CHANNEL));
    assertEquals(123L, result.getHeaders().get(AmqpHeaders.DELIVERY_TAG));
}
Also used : ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) Transformer(org.springframework.integration.transformer.Transformer) JsonToObjectTransformer(org.springframework.integration.json.JsonToObjectTransformer) ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) Test(org.junit.Test)

Example 18 with SimpleMessageListenerContainer

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

the class InboundEndpointTests method testInt2809JavaTypePropertiesFromAmqp.

@Test
public void testInt2809JavaTypePropertiesFromAmqp() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    PollableChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    Object payload = new Foo("bar1");
    MessageProperties amqpMessageProperties = new MessageProperties();
    org.springframework.amqp.core.Message amqpMessage = new Jackson2JsonMessageConverter().toMessage(payload, amqpMessageProperties);
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    listener.onMessage(amqpMessage, null);
    Message<?> receive = channel.receive(1000);
    Message<?> result = new JsonToObjectTransformer().transform(receive);
    assertEquals(payload, result.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) JsonToObjectTransformer(org.springframework.integration.json.JsonToObjectTransformer) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 19 with SimpleMessageListenerContainer

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

the class InboundEndpointTests method testMessageConverterJsonHeadersHavePrecedenceOverMessageHeaders.

@Test
public void testMessageConverterJsonHeadersHavePrecedenceOverMessageHeaders() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    DirectChannel channel = new DirectChannel();
    final Channel rabbitChannel = mock(Channel.class);
    channel.subscribe(new MessageTransformingHandler(message -> {
        assertSame(rabbitChannel, message.getHeaders().get(AmqpHeaders.CHANNEL));
        assertEquals(123L, message.getHeaders().get(AmqpHeaders.DELIVERY_TAG));
        return MessageBuilder.fromMessage(message).setHeader(JsonHeaders.TYPE_ID, "foo").setHeader(JsonHeaders.CONTENT_TYPE_ID, "bar").setHeader(JsonHeaders.KEY_TYPE_ID, "baz").build();
    }));
    RabbitTemplate rabbitTemplate = spy(new RabbitTemplate());
    rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    CountDownLatch sendLatch = new CountDownLatch(1);
    Mockito.doAnswer(invocation -> {
        org.springframework.amqp.core.Message message = invocation.getArgument(2);
        Map<String, Object> headers = message.getMessageProperties().getHeaders();
        assertTrue(headers.containsKey(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
        assertNotEquals("foo", headers.get(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
        assertFalse(headers.containsKey(JsonHeaders.CONTENT_TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
        assertFalse(headers.containsKey(JsonHeaders.KEY_TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
        assertFalse(headers.containsKey(JsonHeaders.TYPE_ID));
        assertFalse(headers.containsKey(JsonHeaders.KEY_TYPE_ID));
        assertFalse(headers.containsKey(JsonHeaders.CONTENT_TYPE_ID));
        sendLatch.countDown();
        return null;
    }).when(rabbitTemplate).send(anyString(), anyString(), any(org.springframework.amqp.core.Message.class), isNull());
    AmqpInboundGateway gateway = new AmqpInboundGateway(container, rabbitTemplate);
    gateway.setMessageConverter(new Jackson2JsonMessageConverter());
    gateway.setRequestChannel(channel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setDefaultReplyTo("foo");
    gateway.afterPropertiesSet();
    Object payload = new Foo("bar1");
    MessageProperties amqpMessageProperties = new MessageProperties();
    amqpMessageProperties.setDeliveryTag(123L);
    org.springframework.amqp.core.Message amqpMessage = new Jackson2JsonMessageConverter().toMessage(payload, amqpMessageProperties);
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    listener.onMessage(amqpMessage, rabbitChannel);
    assertTrue(sendLatch.await(10, TimeUnit.SECONDS));
}
Also used : SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ErrorMessageSendingRecoverer(org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer) MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) Transformer(org.springframework.integration.transformer.Transformer) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) DefaultAmqpHeaderMapper(org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) PollableChannel(org.springframework.messaging.PollableChannel) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Connection(org.springframework.amqp.rabbit.connection.Connection) AmqpMessageHeaderErrorMessageStrategy(org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertFalse(org.junit.Assert.assertFalse) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Matchers.equalTo(org.hamcrest.Matchers.equalTo) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) JsonToObjectTransformer(org.springframework.integration.json.JsonToObjectTransformer) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) JsonHeaders(org.springframework.integration.mapping.support.JsonHeaders) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) Assert.assertSame(org.junit.Assert.assertSame) MessageBuilder(org.springframework.integration.support.MessageBuilder) Message(org.springframework.messaging.Message) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) StaticMessageHeaderAccessor(org.springframework.integration.StaticMessageHeaderAccessor) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ObjectToJsonTransformer(org.springframework.integration.json.ObjectToJsonTransformer) Mockito.when(org.mockito.Mockito.when) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Assert.assertNull(org.junit.Assert.assertNull) BeanFactory(org.springframework.beans.factory.BeanFactory) AmqpHeaders(org.springframework.amqp.support.AmqpHeaders) GenericMessage(org.springframework.messaging.support.GenericMessage) RetryTemplate(org.springframework.retry.support.RetryTemplate) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AcknowledgeMode(org.springframework.amqp.core.AcknowledgeMode) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) MessageProperties(org.springframework.amqp.core.MessageProperties) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 20 with SimpleMessageListenerContainer

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

the class InboundEndpointTests method testAdapterConversionError.

@Test
public void testAdapterConversionError() throws Exception {
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    QueueChannel outputChannel = new QueueChannel();
    adapter.setOutputChannel(outputChannel);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);
    adapter.setMessageConverter(new MessageConverter() {

        @Override
        public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
            throw new MessageConversionException("intended");
        }

        @Override
        public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException {
            return null;
        }
    });
    adapter.afterPropertiesSet();
    ((ChannelAwareMessageListener) container.getMessageListener()).onMessage(null, null);
    assertNull(outputChannel.receive(0));
    assertNotNull(errorChannel.receive(0));
}
Also used : MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) PollableChannel(org.springframework.messaging.PollableChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Channel(com.rabbitmq.client.Channel) QueueChannel(org.springframework.integration.channel.QueueChannel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) Jackson2JsonMessageConverter(org.springframework.amqp.support.converter.Jackson2JsonMessageConverter) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Aggregations

SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)27 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 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)5 Bean (org.springframework.context.annotation.Bean)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