Search in sources :

Example 1 with AbstractMessageListenerContainer

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

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

the class InboundEndpointTests method testRetryWithinOnMessageGateway.

@Test
public void testRetryWithinOnMessageGateway() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    AmqpInboundGateway adapter = new AmqpInboundGateway(container);
    adapter.setRequestChannel(new DirectChannel());
    adapter.setRetryTemplate(new RetryTemplate());
    QueueChannel errors = new QueueChannel();
    ErrorMessageSendingRecoverer recoveryCallback = new ErrorMessageSendingRecoverer(errors);
    recoveryCallback.setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
    adapter.setRecoveryCallback(recoveryCallback);
    adapter.afterPropertiesSet();
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    listener.onMessage(org.springframework.amqp.core.MessageBuilder.withBody("foo".getBytes()).andProperties(new MessageProperties()).build(), null);
    Message<?> errorMessage = errors.receive(0);
    assertNotNull(errorMessage);
    assertThat(errorMessage.getPayload(), instanceOf(MessagingException.class));
    MessagingException payload = (MessagingException) errorMessage.getPayload();
    assertThat(payload.getMessage(), containsString("Dispatcher has no"));
    assertThat(StaticMessageHeaderAccessor.getDeliveryAttempt(payload.getFailedMessage()).get(), equalTo(3));
    org.springframework.amqp.core.Message amqpMessage = errorMessage.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, org.springframework.amqp.core.Message.class);
    assertThat(amqpMessage, notNullValue());
    assertNull(errors.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageProperties(org.springframework.amqp.core.MessageProperties) ErrorMessageSendingRecoverer(org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer) AmqpMessageHeaderErrorMessageStrategy(org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 3 with AbstractMessageListenerContainer

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

the class InboundEndpointTests method testRetryWithinOnMessageAdapter.

@Test
public void testRetryWithinOnMessageAdapter() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    adapter.setOutputChannel(new DirectChannel());
    adapter.setRetryTemplate(new RetryTemplate());
    QueueChannel errors = new QueueChannel();
    ErrorMessageSendingRecoverer recoveryCallback = new ErrorMessageSendingRecoverer(errors);
    recoveryCallback.setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
    adapter.setRecoveryCallback(recoveryCallback);
    adapter.afterPropertiesSet();
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    listener.onMessage(org.springframework.amqp.core.MessageBuilder.withBody("foo".getBytes()).andProperties(new MessageProperties()).build(), null);
    Message<?> errorMessage = errors.receive(0);
    assertNotNull(errorMessage);
    assertThat(errorMessage.getPayload(), instanceOf(MessagingException.class));
    MessagingException payload = (MessagingException) errorMessage.getPayload();
    assertThat(payload.getMessage(), containsString("Dispatcher has no"));
    assertThat(StaticMessageHeaderAccessor.getDeliveryAttempt(payload.getFailedMessage()).get(), equalTo(3));
    org.springframework.amqp.core.Message amqpMessage = errorMessage.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, org.springframework.amqp.core.Message.class);
    assertThat(amqpMessage, notNullValue());
    assertNull(errors.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageProperties(org.springframework.amqp.core.MessageProperties) ErrorMessageSendingRecoverer(org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer) AmqpMessageHeaderErrorMessageStrategy(org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 4 with AbstractMessageListenerContainer

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

the class AmqpChannelFactoryBean method createInstance.

@Override
protected AbstractAmqpChannel createInstance() throws Exception {
    if (this.messageDriven) {
        AbstractMessageListenerContainer container = this.createContainer();
        if (this.amqpTemplate instanceof InitializingBean) {
            ((InitializingBean) this.amqpTemplate).afterPropertiesSet();
        }
        if (this.isPubSub) {
            PublishSubscribeAmqpChannel pubsub = new PublishSubscribeAmqpChannel(this.beanName, container, this.amqpTemplate, this.outboundHeaderMapper, this.inboundHeaderMapper);
            if (this.exchange != null) {
                pubsub.setExchange(this.exchange);
            }
            if (this.maxSubscribers != null) {
                pubsub.setMaxSubscribers(this.maxSubscribers);
            }
            this.channel = pubsub;
        } else {
            PointToPointSubscribableAmqpChannel p2p = new PointToPointSubscribableAmqpChannel(this.beanName, container, this.amqpTemplate, this.outboundHeaderMapper, this.inboundHeaderMapper);
            if (StringUtils.hasText(this.queueName)) {
                p2p.setQueueName(this.queueName);
            }
            if (this.maxSubscribers != null) {
                p2p.setMaxSubscribers(this.maxSubscribers);
            }
            this.channel = p2p;
        }
    } else {
        Assert.isTrue(!this.isPubSub, "An AMQP 'publish-subscribe-channel' must be message-driven.");
        PollableAmqpChannel pollable = new PollableAmqpChannel(this.beanName, this.amqpTemplate, this.outboundHeaderMapper, this.inboundHeaderMapper);
        if (this.amqpAdmin != null) {
            pollable.setAmqpAdmin(this.amqpAdmin);
        }
        if (StringUtils.hasText(this.queueName)) {
            pollable.setQueueName(this.queueName);
        }
        this.channel = pollable;
    }
    if (!CollectionUtils.isEmpty(this.interceptors)) {
        this.channel.setInterceptors(this.interceptors);
    }
    this.channel.setBeanName(this.beanName);
    if (this.getBeanFactory() != null) {
        this.channel.setBeanFactory(this.getBeanFactory());
    }
    if (this.defaultDeliveryMode != null) {
        this.channel.setDefaultDeliveryMode(this.defaultDeliveryMode);
    }
    if (this.extractPayload != null) {
        this.channel.setExtractPayload(this.extractPayload);
    }
    this.channel.setHeadersMappedLast(this.headersLast);
    this.channel.afterPropertiesSet();
    return this.channel;
}
Also used : PointToPointSubscribableAmqpChannel(org.springframework.integration.amqp.channel.PointToPointSubscribableAmqpChannel) PollableAmqpChannel(org.springframework.integration.amqp.channel.PollableAmqpChannel) InitializingBean(org.springframework.beans.factory.InitializingBean) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) PublishSubscribeAmqpChannel(org.springframework.integration.amqp.channel.PublishSubscribeAmqpChannel)

Example 5 with AbstractMessageListenerContainer

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

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