Search in sources :

Example 21 with Message

use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.

the class DispatcherHasNoSubscribersTests method testPtP.

@SuppressWarnings("unchecked")
@Test
public void testPtP() throws Exception {
    final Channel channel = mock(Channel.class);
    DeclareOk declareOk = mock(DeclareOk.class);
    when(declareOk.getQueue()).thenReturn("noSubscribersChannel");
    when(channel.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), isNull())).thenReturn(declareOk);
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
    PointToPointSubscribableAmqpChannel amqpChannel = new PointToPointSubscribableAmqpChannel("noSubscribersChannel", container, amqpTemplate);
    amqpChannel.setBeanName("noSubscribersChannel");
    amqpChannel.setBeanFactory(mock(BeanFactory.class));
    amqpChannel.afterPropertiesSet();
    MessageListener listener = (MessageListener) container.getMessageListener();
    try {
        listener.onMessage(new Message("Hello world!".getBytes(), null));
        fail("Exception expected");
    } catch (MessageDeliveryException e) {
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
    }
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Queue.DeclareOk) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Example 22 with Message

use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.

the class AmqpInboundChannelAdapterParserTests method withHeaderMapperStandardAndCustomHeaders.

@Test
public void withHeaderMapperStandardAndCustomHeaders() throws Exception {
    AmqpInboundChannelAdapter adapter = context.getBean("withHeaderMapperStandardAndCustomHeaders", 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);
    assertEquals("foo", siMessage.getHeaders().get("foo"));
    assertNull(siMessage.getHeaders().get("bar"));
    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 23 with Message

use of org.springframework.amqp.core.Message in project spring-integration by spring-projects.

the class AmqpInboundChannelAdapterParserTests method withHeaderMapperNothingToMap.

@Test
public void withHeaderMapperNothingToMap() throws Exception {
    AmqpInboundChannelAdapter adapter = context.getBean("withHeaderMapperNothingToMap", 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);
    assertNull(siMessage.getHeaders().get("foo"));
    assertNull(siMessage.getHeaders().get("bar"));
    assertNull(siMessage.getHeaders().get(AmqpHeaders.CONTENT_ENCODING));
    assertNull(siMessage.getHeaders().get(AmqpHeaders.CLUSTER_ID));
    assertNull(siMessage.getHeaders().get(AmqpHeaders.APP_ID));
    assertNull(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 24 with Message

use of org.springframework.amqp.core.Message 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 25 with Message

use of org.springframework.amqp.core.Message 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)

Aggregations

Message (org.springframework.amqp.core.Message)43 Test (org.junit.Test)38 MessageProperties (org.springframework.amqp.core.MessageProperties)15 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)6 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)6 AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)5 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)5 QueueChannel (org.springframework.integration.channel.QueueChannel)5 BeanFactory (org.springframework.beans.factory.BeanFactory)4 AmqpInboundChannelAdapter (org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter)4 GenericMessage (org.springframework.messaging.support.GenericMessage)4 AsyncRabbitTemplate (org.springframework.amqp.rabbit.AsyncRabbitTemplate)3 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)3 Channel (com.rabbitmq.client.Channel)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Advice (org.aopalliance.aop.Advice)2 AmqpTemplate (org.springframework.amqp.core.AmqpTemplate)2 MessageListener (org.springframework.amqp.core.MessageListener)2 Connection (org.springframework.amqp.rabbit.connection.Connection)2 MessageRecoverer (org.springframework.amqp.rabbit.retry.MessageRecoverer)2