Search in sources :

Example 21 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method exactlyOneConsumerReceivesPointToPointMessage.

@Test
public void exactlyOneConsumerReceivesPointToPointMessage() {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    QueueChannel inputChannel = new QueueChannel();
    QueueChannel outputChannel1 = new QueueChannel();
    QueueChannel outputChannel2 = new QueueChannel();
    AbstractReplyProducingMessageHandler handler1 = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            return message;
        }
    };
    AbstractReplyProducingMessageHandler handler2 = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            return message;
        }
    };
    context.registerChannel("input", inputChannel);
    context.registerChannel("output1", outputChannel1);
    context.registerChannel("output2", outputChannel2);
    handler1.setOutputChannel(outputChannel1);
    handler2.setOutputChannel(outputChannel2);
    PollingConsumer endpoint1 = new PollingConsumer(inputChannel, handler1);
    endpoint1.setBeanFactory(mock(BeanFactory.class));
    PollingConsumer endpoint2 = new PollingConsumer(inputChannel, handler2);
    endpoint2.setBeanFactory(mock(BeanFactory.class));
    context.registerEndpoint("testEndpoint1", endpoint1);
    context.registerEndpoint("testEndpoint2", endpoint2);
    context.refresh();
    inputChannel.send(new GenericMessage<String>("testing"));
    Message<?> message1 = outputChannel1.receive(10000);
    Message<?> message2 = outputChannel2.receive(0);
    context.stop();
    assertTrue("exactly one message should be null", message1 == null ^ message2 == null);
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 22 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method endpointRegistrationWithInputChannelReference.

@Test
public void endpointRegistrationWithInputChannelReference() {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    QueueChannel sourceChannel = new QueueChannel();
    QueueChannel targetChannel = new QueueChannel();
    context.registerChannel("sourceChannel", sourceChannel);
    context.registerChannel("targetChannel", targetChannel);
    Message<String> message = MessageBuilder.withPayload("test").setReplyChannelName("targetChannel").build();
    sourceChannel.send(message);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            return message;
        }
    };
    handler.setBeanFactory(context);
    handler.afterPropertiesSet();
    PollingConsumer endpoint = new PollingConsumer(sourceChannel, handler);
    endpoint.setBeanFactory(mock(BeanFactory.class));
    context.registerEndpoint("testEndpoint", endpoint);
    context.refresh();
    Message<?> result = targetChannel.receive(10000);
    assertEquals("test", result.getPayload());
    context.stop();
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 23 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class ChannelInterceptorTests method testPollingConsumerWithExecutorInterceptor.

@Test
public void testPollingConsumerWithExecutorInterceptor() throws InterruptedException {
    TestUtils.TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
    QueueChannel channel = new QueueChannel();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    final List<Message<?>> messages = new ArrayList<>();
    PollingConsumer consumer = new PollingConsumer(channel, message -> {
        messages.add(message);
        latch1.countDown();
        latch2.countDown();
    });
    testApplicationContext.registerBean("consumer", consumer);
    testApplicationContext.refresh();
    channel.send(new GenericMessage<>("foo"));
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    channel.addInterceptor(new TestExecutorInterceptor());
    channel.send(new GenericMessage<>("foo"));
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertEquals(2, messages.size());
    assertEquals("foo", messages.get(0).getPayload());
    assertEquals("FOO", messages.get(1).getPayload());
    testApplicationContext.close();
}
Also used : TestUtils(org.springframework.integration.test.util.TestUtils) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 24 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class JmsOutboundGatewayParserTests method testDefault.

@Test
public void testDefault() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("jmsOutboundGatewayWithConverter.xml", this.getClass());
    PollingConsumer endpoint = (PollingConsumer) context.getBean("jmsGateway");
    DirectFieldAccessor accessor = new DirectFieldAccessor(endpoint);
    JmsOutboundGateway gateway = (JmsOutboundGateway) accessor.getPropertyValue("handler");
    accessor = new DirectFieldAccessor(gateway);
    MessageConverter converter = (MessageConverter) accessor.getPropertyValue("messageConverter");
    assertTrue("Wrong message converter", converter instanceof StubMessageConverter);
    context.close();
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) StubMessageConverter(org.springframework.integration.jms.StubMessageConverter) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JmsOutboundGateway(org.springframework.integration.jms.JmsOutboundGateway) MessageConverter(org.springframework.jms.support.converter.MessageConverter) StubMessageConverter(org.springframework.integration.jms.StubMessageConverter) Test(org.junit.Test)

Example 25 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class MailOutboundChannelAdapterParserTests method adapterWithPollableChannel.

@Test
public void adapterWithPollableChannel() {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("mailOutboundChannelAdapterParserTests.xml", this.getClass());
    PollingConsumer pc = context.getBean("adapterWithPollableChannel", PollingConsumer.class);
    QueueChannel pollableChannel = TestUtils.getPropertyValue(pc, "inputChannel", QueueChannel.class);
    assertEquals("pollableChannel", pollableChannel.getComponentName());
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Aggregations

PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)25 Test (org.junit.Test)17 QueueChannel (org.springframework.integration.channel.QueueChannel)12 Message (org.springframework.messaging.Message)9 BeanFactory (org.springframework.beans.factory.BeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)6 MessageChannel (org.springframework.messaging.MessageChannel)6 PollableChannel (org.springframework.messaging.PollableChannel)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)5 ErrorMessage (org.springframework.messaging.support.ErrorMessage)5 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)4 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)4 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)4 SubscribableChannel (org.springframework.messaging.SubscribableChannel)4 ReactiveStreamsConsumer (org.springframework.integration.endpoint.ReactiveStreamsConsumer)3 MessagingException (org.springframework.messaging.MessagingException)3 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2