Search in sources :

Example 61 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class MockHandlerTests method configOk.

@Test
public void configOk() throws Exception {
    QueueChannel output = new QueueChannel();
    Mockito.when(mock.test("foo")).thenReturn("bar");
    input.send(MessageBuilder.withPayload("foo").setReplyChannel(output).build());
    Message<?> result = output.receive(0);
    assertEquals("bar", result.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 62 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class OutboundEndpointTests method testHeaderMapperWinsGateway.

@Test
public void testHeaderMapperWinsGateway() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    TestRabbitTemplate amqpTemplate = spy(new TestRabbitTemplate(connectionFactory));
    AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(amqpTemplate);
    endpoint.setHeadersMappedLast(true);
    endpoint.setExpectReply(true);
    DefaultAmqpHeaderMapper mapper = DefaultAmqpHeaderMapper.inboundMapper();
    mapper.setRequestHeaderNames("*");
    endpoint.setHeaderMapper(mapper);
    final AtomicReference<Message> amqpMessage = new AtomicReference<Message>();
    willAnswer(invocation -> {
        amqpMessage.set(invocation.getArgument(2));
        return null;
    }).given(amqpTemplate).doSendAndReceiveWithTemporary(isNull(), isNull(), any(Message.class), isNull());
    org.springframework.messaging.Message<?> message = MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, "bar").setReplyChannel(new QueueChannel()).build();
    endpoint.handleMessage(message);
    assertNotNull(amqpMessage.get());
    assertEquals("bar", amqpMessage.get().getMessageProperties().getContentType());
    assertNull(amqpMessage.get().getMessageProperties().getHeaders().get(MessageHeaders.REPLY_CHANNEL));
}
Also used : ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) DefaultAmqpHeaderMapper(org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 63 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AmqpInboundChannelAdapterParserTests method withHeaderMapperOnlyCustomHeaders.

@Test
public void withHeaderMapperOnlyCustomHeaders() throws Exception {
    AmqpInboundChannelAdapter adapter = context.getBean("withHeaderMapperOnlyCustomHeaders", 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"));
    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 64 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AmqpOutboundGatewayParserTests method withHeaderMapperNothingToMap.

@Test
public void withHeaderMapperNothingToMap() {
    Object eventDrivenConsumer = this.context.getBean("withHeaderMapperNothingToMap");
    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler", AmqpOutboundEndpoint.class);
    Field amqpTemplateField = ReflectionUtils.findField(AmqpOutboundEndpoint.class, "amqpTemplate");
    amqpTemplateField.setAccessible(true);
    RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(endpoint, "amqpTemplate", RabbitTemplate.class);
    amqpTemplate = Mockito.spy(amqpTemplate);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        org.springframework.amqp.core.Message amqpRequestMessage = (org.springframework.amqp.core.Message) args[2];
        MessageProperties properties = amqpRequestMessage.getMessageProperties();
        assertNull(properties.getHeaders().get("foo"));
        // mock reply AMQP message
        MessageProperties amqpProperties = new MessageProperties();
        amqpProperties.setAppId("test.appId");
        amqpProperties.setHeader("foobar", "foobar");
        amqpProperties.setHeader("bar", "bar");
        return new org.springframework.amqp.core.Message("hello".getBytes(), amqpProperties);
    }).when(amqpTemplate).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    ReflectionUtils.setField(amqpTemplateField, endpoint, amqpTemplate);
    MessageChannel requestChannel = this.context.getBean("toRabbit3", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    // verify reply
    QueueChannel queueChannel = context.getBean("fromRabbit", QueueChannel.class);
    Message<?> replyMessage = queueChannel.receive(0);
    assertNull(replyMessage.getHeaders().get("bar"));
    // copied from request Message
    assertEquals("foo", replyMessage.getHeaders().get("foo"));
    assertNull(replyMessage.getHeaders().get("foobar"));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.DELIVERY_MODE));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
    assertNull(replyMessage.getHeaders().get(AmqpHeaders.APP_ID));
    assertEquals(1, adviceCalled);
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.messaging.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) Field(java.lang.reflect.Field) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) Test(org.junit.Test)

Example 65 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AmqpOutboundGatewayParserTests method withHeaderMapperCustomAndStandardResponse.

@Test
public void withHeaderMapperCustomAndStandardResponse() {
    Object eventDrivenConsumer = this.context.getBean("withHeaderMapperCustomAndStandardResponse");
    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler", AmqpOutboundEndpoint.class);
    assertNull(TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode"));
    assertFalse(TestUtils.getPropertyValue(endpoint, "headersMappedLast", Boolean.class));
    Field amqpTemplateField = ReflectionUtils.findField(AmqpOutboundEndpoint.class, "amqpTemplate");
    amqpTemplateField.setAccessible(true);
    RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(endpoint, "amqpTemplate", RabbitTemplate.class);
    amqpTemplate = Mockito.spy(amqpTemplate);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        org.springframework.amqp.core.Message amqpRequestMessage = (org.springframework.amqp.core.Message) args[2];
        MessageProperties properties = amqpRequestMessage.getMessageProperties();
        assertEquals("foo", properties.getHeaders().get("foo"));
        // mock reply AMQP message
        MessageProperties amqpProperties = new MessageProperties();
        amqpProperties.setAppId("test.appId");
        amqpProperties.setHeader("foobar", "foobar");
        amqpProperties.setHeader("bar", "bar");
        assertEquals(MessageDeliveryMode.PERSISTENT, properties.getDeliveryMode());
        amqpProperties.setReceivedDeliveryMode(properties.getDeliveryMode());
        return new org.springframework.amqp.core.Message("hello".getBytes(), amqpProperties);
    }).when(amqpTemplate).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    ReflectionUtils.setField(amqpTemplateField, endpoint, amqpTemplate);
    MessageChannel requestChannel = this.context.getBean("toRabbit2", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).sendAndReceive(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(org.springframework.amqp.core.Message.class), isNull());
    // verify reply
    QueueChannel queueChannel = this.context.getBean("fromRabbit", QueueChannel.class);
    Message<?> replyMessage = queueChannel.receive(0);
    assertEquals("bar", replyMessage.getHeaders().get("bar"));
    // copied from request Message
    assertEquals("foo", replyMessage.getHeaders().get("foo"));
    assertNull(replyMessage.getHeaders().get("foobar"));
    assertNotNull(replyMessage.getHeaders().get(AmqpHeaders.RECEIVED_DELIVERY_MODE));
    assertNotNull(replyMessage.getHeaders().get(AmqpHeaders.CONTENT_TYPE));
    assertNotNull(replyMessage.getHeaders().get(AmqpHeaders.APP_ID));
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Message(org.springframework.messaging.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) Field(java.lang.reflect.Field) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) MessageProperties(org.springframework.amqp.core.MessageProperties) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Aggregations

QueueChannel (org.springframework.integration.channel.QueueChannel)709 Test (org.junit.Test)669 GenericMessage (org.springframework.messaging.support.GenericMessage)186 Message (org.springframework.messaging.Message)173 BeanFactory (org.springframework.beans.factory.BeanFactory)162 MessageChannel (org.springframework.messaging.MessageChannel)100 Matchers.containsString (org.hamcrest.Matchers.containsString)66 CountDownLatch (java.util.concurrent.CountDownLatch)59 DirectChannel (org.springframework.integration.channel.DirectChannel)57 ArrayList (java.util.ArrayList)55 PollableChannel (org.springframework.messaging.PollableChannel)55 MessagingException (org.springframework.messaging.MessagingException)53 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 Socket (java.net.Socket)44 ErrorMessage (org.springframework.messaging.support.ErrorMessage)42 ServerSocket (java.net.ServerSocket)41 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)39 IOException (java.io.IOException)35 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)35