Search in sources :

Example 6 with AbstractMessageListenerContainer

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

use of org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer 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 8 with AbstractMessageListenerContainer

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

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

the class AmqpChannelFactoryBean method createContainer.

private AbstractMessageListenerContainer createContainer() throws Exception {
    AbstractMessageListenerContainer container;
    if (this.consumersPerQueue == null) {
        SimpleMessageListenerContainer smlc = new SimpleMessageListenerContainer();
        if (this.concurrentConsumers != null) {
            smlc.setConcurrentConsumers(this.concurrentConsumers);
        }
        if (this.receiveTimeout != null) {
            smlc.setReceiveTimeout(this.receiveTimeout);
        }
        if (this.txSize != null) {
            smlc.setTxSize(this.txSize);
        }
        container = smlc;
    } else {
        DirectMessageListenerContainer dmlc = new DirectMessageListenerContainer();
        dmlc.setConsumersPerQueue(this.consumersPerQueue);
        container = dmlc;
    }
    if (this.acknowledgeMode != null) {
        container.setAcknowledgeMode(this.acknowledgeMode);
    }
    if (!ObjectUtils.isEmpty(this.adviceChain)) {
        container.setAdviceChain(this.adviceChain);
    }
    container.setAutoStartup(this.autoStartup);
    container.setChannelTransacted(this.channelTransacted);
    container.setConnectionFactory(this.connectionFactory);
    if (this.errorHandler != null) {
        container.setErrorHandler(this.errorHandler);
    }
    if (this.exposeListenerChannel != null) {
        container.setExposeListenerChannel(this.exposeListenerChannel);
    }
    if (this.messagePropertiesConverter != null) {
        container.setMessagePropertiesConverter(this.messagePropertiesConverter);
    }
    if (this.phase != null) {
        container.setPhase(this.phase);
    }
    if (this.prefetchCount != null) {
        container.setPrefetchCount(this.prefetchCount);
    }
    if (this.recoveryInterval != null) {
        container.setRecoveryInterval(this.recoveryInterval);
    }
    if (this.shutdownTimeout != null) {
        container.setShutdownTimeout(this.shutdownTimeout);
    }
    if (this.taskExecutor != null) {
        container.setTaskExecutor(this.taskExecutor);
    }
    if (this.transactionAttribute != null) {
        container.setTransactionAttribute(this.transactionAttribute);
    }
    if (this.transactionManager != null) {
        container.setTransactionManager(this.transactionManager);
    }
    if (this.missingQueuesFatal != null) {
        container.setMissingQueuesFatal(this.missingQueuesFatal);
    }
    return container;
}
Also used : DirectMessageListenerContainer(org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)

Aggregations

AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)9 Test (org.junit.Test)7 MessageProperties (org.springframework.amqp.core.MessageProperties)7 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)7 QueueChannel (org.springframework.integration.channel.QueueChannel)6 Message (org.springframework.amqp.core.Message)5 AmqpInboundChannelAdapter (org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter)4 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)3 DirectChannel (org.springframework.integration.channel.DirectChannel)3 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)2 AmqpMessageHeaderErrorMessageStrategy (org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy)2 ErrorMessageSendingRecoverer (org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer)2 MessagingException (org.springframework.messaging.MessagingException)2 RetryTemplate (org.springframework.retry.support.RetryTemplate)2 Field (java.lang.reflect.Field)1 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)1 DirectMessageListenerContainer (org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 PointToPointSubscribableAmqpChannel (org.springframework.integration.amqp.channel.PointToPointSubscribableAmqpChannel)1 PollableAmqpChannel (org.springframework.integration.amqp.channel.PollableAmqpChannel)1