Search in sources :

Example 6 with ChannelAwareMessageListener

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

the class AmqpInboundChannelAdapterParserTests method withHeaderMapperDefaultMapping.

@Test
public void withHeaderMapperDefaultMapping() throws Exception {
    AmqpInboundChannelAdapter adapter = context.getBean("withHeaderMapperDefaultMapping", AmqpInboundChannelAdapter.class);
    AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue(adapter, "messageListenerContainer", AbstractMessageListenerContainer.class);
    ChannelAwareMessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", ChannelAwareMessageListener.class);
    MessageProperties amqpProperties = new MessageProperties();
    amqpProperties.setAppId("test.appId");
    amqpProperties.setClusterId("test.clusterId");
    amqpProperties.setContentEncoding("test.contentEncoding");
    amqpProperties.setContentLength(99L);
    amqpProperties.setContentType("test.contentType");
    amqpProperties.setHeader("foo", "foo");
    amqpProperties.setHeader("bar", "bar");
    Message amqpMessage = new Message("hello".getBytes(), amqpProperties);
    listener.onMessage(amqpMessage, null);
    QueueChannel requestChannel = context.getBean("requestChannel", QueueChannel.class);
    org.springframework.messaging.Message<?> siMessage = requestChannel.receive(0);
    assertNotNull(siMessage.getHeaders().get("bar"));
    assertNotNull(siMessage.getHeaders().get("foo"));
    assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_ENCODING));
    assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CLUSTER_ID));
    assertNotNull(siMessage.getHeaders().get(AmqpHeaders.APP_ID));
    assertNotNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
}
Also used : AmqpInboundChannelAdapter(org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter) Message(org.springframework.amqp.core.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 7 with ChannelAwareMessageListener

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

the class AmqpInboundGatewayParserTests method verifyUsageWithHeaderMapper.

@Test
public void verifyUsageWithHeaderMapper() throws Exception {
    DirectChannel requestChannel = context.getBean("requestChannel", DirectChannel.class);
    requestChannel.subscribe(siMessage -> {
        org.springframework.messaging.Message<?> replyMessage = MessageBuilder.fromMessage(siMessage).setHeader("bar", "bar").build();
        MessageChannel replyChannel = (MessageChannel) siMessage.getHeaders().getReplyChannel();
        replyChannel.send(replyMessage);
    });
    final AmqpInboundGateway gateway = context.getBean("withHeaderMapper", AmqpInboundGateway.class);
    Field amqpTemplateField = ReflectionUtils.findField(AmqpInboundGateway.class, "amqpTemplate");
    amqpTemplateField.setAccessible(true);
    RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(gateway, "amqpTemplate", RabbitTemplate.class);
    amqpTemplate = Mockito.spy(amqpTemplate);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        Message amqpReplyMessage = (Message) args[2];
        MessageProperties properties = amqpReplyMessage.getMessageProperties();
        assertEquals("bar", properties.getHeaders().get("bar"));
        return null;
    }).when(amqpTemplate).send(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Message.class), isNull());
    ReflectionUtils.setField(amqpTemplateField, gateway, amqpTemplate);
    AbstractMessageListenerContainer mlc = TestUtils.getPropertyValue(gateway, "messageListenerContainer", AbstractMessageListenerContainer.class);
    ChannelAwareMessageListener listener = TestUtils.getPropertyValue(mlc, "messageListener", ChannelAwareMessageListener.class);
    MessageProperties amqpProperties = new MessageProperties();
    amqpProperties.setAppId("test.appId");
    amqpProperties.setClusterId("test.clusterId");
    amqpProperties.setContentEncoding("test.contentEncoding");
    amqpProperties.setContentLength(99L);
    amqpProperties.setReplyTo("oleg");
    amqpProperties.setContentType("test.contentType");
    amqpProperties.setHeader("foo", "foo");
    amqpProperties.setHeader("bar", "bar");
    Message amqpMessage = new Message("hello".getBytes(), amqpProperties);
    listener.onMessage(amqpMessage, null);
    Mockito.verify(amqpTemplate, Mockito.times(1)).send(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Message.class), isNull());
}
Also used : AmqpInboundGateway(org.springframework.integration.amqp.inbound.AmqpInboundGateway) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.amqp.core.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) Field(java.lang.reflect.Field) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 8 with ChannelAwareMessageListener

use of org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener 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 9 with ChannelAwareMessageListener

use of org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener 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 10 with ChannelAwareMessageListener

use of org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener 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)

Aggregations

Test (org.junit.Test)12 MessageProperties (org.springframework.amqp.core.MessageProperties)12 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)12 QueueChannel (org.springframework.integration.channel.QueueChannel)11 AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)8 DirectChannel (org.springframework.integration.channel.DirectChannel)8 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)7 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)7 Channel (com.rabbitmq.client.Channel)5 Message (org.springframework.amqp.core.Message)5 Connection (org.springframework.amqp.rabbit.connection.Connection)5 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)5 PollableChannel (org.springframework.messaging.PollableChannel)5 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)4 AmqpInboundChannelAdapter (org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter)4 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)3 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 JsonToObjectTransformer (org.springframework.integration.json.JsonToObjectTransformer)3 Message (org.springframework.messaging.Message)3