Example usage for org.springframework.integration.channel QueueChannel receive

List of usage examples for org.springframework.integration.channel QueueChannel receive

Introduction

In this page you can find the example usage for org.springframework.integration.channel QueueChannel receive.

Prototype

@Override 
@Nullable
public Message<?> receive(long timeout) 

Source Link

Document

Receive the first available message from this channel.

Usage

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testBlockingReceiveWithTimeout() throws Exception {
    final QueueChannel channel = new QueueChannel();
    final AtomicBoolean receiveInterrupted = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {
        @Override//from  ww  w.  j a v  a  2s . c o  m
        public void run() {
            Message<?> message = channel.receive(10000);
            receiveInterrupted.set(true);
            assertTrue(message == null);
            latch.countDown();
        }
    });
    t.start();
    assertFalse(receiveInterrupted.get());
    t.interrupt();
    latch.await();
    assertTrue(receiveInterrupted.get());
}

From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java

@Test
public void testAdviceChain() throws Exception {
    SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
    QueueChannel outputChannel = new QueueChannel();
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    factoryBean.setBeanFactory(context.getBeanFactory());
    factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    factoryBean.setOutputChannel(outputChannel);
    factoryBean.setSource(() -> new GenericMessage<>("test"));
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicBoolean adviceApplied = new AtomicBoolean(false);
    adviceChain.add((MethodInterceptor) invocation -> {
        adviceApplied.set(true);/* w  w  w. ja  v a 2  s . c om*/
        return invocation.proceed();
    });
    pollerMetadata.setTrigger(new PeriodicTrigger(5000));
    pollerMetadata.setMaxMessagesPerPoll(1);
    pollerMetadata.setAdviceChain(adviceChain);
    factoryBean.setPollerMetadata(pollerMetadata);
    factoryBean.setAutoStartup(true);
    factoryBean.afterPropertiesSet();
    context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
    context.refresh();
    Message<?> message = outputChannel.receive(5000);
    assertEquals("test", message.getPayload());
    assertTrue("adviceChain was not applied", adviceApplied.get());
}

From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java

@Test
public void testTransactionalAdviceChain() throws Throwable {
    SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
    QueueChannel outputChannel = new QueueChannel();
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    factoryBean.setBeanFactory(context.getBeanFactory());
    factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    factoryBean.setOutputChannel(outputChannel);
    factoryBean.setSource(() -> new GenericMessage<>("test"));
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicBoolean adviceApplied = new AtomicBoolean(false);
    adviceChain.add((MethodInterceptor) invocation -> {
        adviceApplied.set(true);/*from   w  w  w  . j a  va  2s.  co m*/
        return invocation.proceed();
    });
    pollerMetadata.setTrigger(new PeriodicTrigger(5000));
    pollerMetadata.setMaxMessagesPerPoll(1);
    final AtomicInteger count = new AtomicInteger();
    final MethodInterceptor txAdvice = mock(MethodInterceptor.class);
    adviceChain.add((MethodInterceptor) invocation -> {
        count.incrementAndGet();
        return invocation.proceed();
    });
    when(txAdvice.invoke(any(MethodInvocation.class))).thenAnswer(invocation -> {
        count.incrementAndGet();
        return ((MethodInvocation) invocation.getArgument(0)).proceed();
    });

    pollerMetadata.setAdviceChain(adviceChain);
    factoryBean.setPollerMetadata(pollerMetadata);
    factoryBean.setAutoStartup(true);
    factoryBean.afterPropertiesSet();
    context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
    context.refresh();
    Message<?> message = outputChannel.receive(5000);
    assertEquals("test", message.getPayload());
    assertEquals(1, count.get());
    assertTrue("adviceChain was not applied", adviceApplied.get());
}

From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java

@Test
public void testStartSourceBeforeRunPollingTask() {
    TaskScheduler taskScheduler = mock(TaskScheduler.class);

    willAnswer(invocation -> {//from ww w .j a va2  s  . co m
        Runnable task = invocation.getArgument(0);
        task.run();
        return null;
    }).given(taskScheduler).schedule(any(Runnable.class), any(Trigger.class));

    SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
    pollingChannelAdapter.setTaskScheduler(taskScheduler);
    pollingChannelAdapter.setSource(new LifecycleMessageSource());
    pollingChannelAdapter.setMaxMessagesPerPoll(1);
    QueueChannel outputChannel = new QueueChannel();
    pollingChannelAdapter.setOutputChannel(outputChannel);
    pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
    pollingChannelAdapter.afterPropertiesSet();
    pollingChannelAdapter.start();

    Message<?> receive = outputChannel.receive(10_000);
    assertNotNull(receive);
    assertEquals(true, receive.getPayload());
    pollingChannelAdapter.stop();
}

From source file:org.springframework.integration.configuration.EnableIntegrationTests.java

@Test
public void testSourcePollingChannelAdapterOutputChannelLateBinding() {
    QueueChannel testChannel = new QueueChannel();
    ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) this.context
            .getAutowireCapableBeanFactory();
    beanFactory.registerSingleton("lateBindingChannel", testChannel);
    beanFactory.initializeBean(testChannel, "lateBindingChannel");

    this.autoCreatedChannelMessageSourceAdapter.start();
    Message<?> receive = testChannel.receive(10000);
    assertNotNull(receive);/*from ww  w  .  ja va  2s .  c  o m*/
    assertEquals("bar", receive.getPayload());
    this.autoCreatedChannelMessageSourceAdapter.stop();
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testDirectFlow() {
    assertTrue(this.beanFactory.containsBean("filter"));
    assertTrue(this.beanFactory.containsBean("filter.handler"));
    QueueChannel replyChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("100").setReplyChannel(replyChannel).build();
    try {/*from   ww w  .j  a  va  2s  .c  o m*/
        this.inputChannel.send(message);
        fail("Expected MessageDispatchingException");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(MessageDispatchingException.class));
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers"));
    }
    this.controlBus.send("@payloadSerializingTransformer.start()");

    final AtomicBoolean used = new AtomicBoolean();

    this.foo.subscribe(m -> used.set(true));

    this.inputChannel.send(message);
    Message<?> reply = replyChannel.receive(5000);
    assertNotNull(reply);
    assertEquals(200, reply.getPayload());

    Message<?> successMessage = this.successChannel.receive(5000);
    assertNotNull(successMessage);
    assertEquals(100, successMessage.getPayload());

    assertTrue(used.get());
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testMethodInvokingMessageHandler() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("world")
            .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build();
    this.methodInvokingInput.send(message);
    Message<?> receive = replyChannel.receive(5000);
    assertNotNull(receive);/*from  ww  w  .  j  a  v  a  2 s  .  co  m*/
    assertEquals("Hello World and world", receive.getPayload());
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testLambdas() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("World")
            .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build();
    this.lamdasInput.send(message);
    Message<?> receive = replyChannel.receive(5000);
    assertNotNull(receive);//from ww  w.  j ava 2  s  .c o m
    assertEquals("Hello World", receive.getPayload());

    message = MessageBuilder.withPayload("Spring").setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel)
            .build();

    this.lamdasInput.send(message);
    assertNull(replyChannel.receive(10));

}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testContentEnricher() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload(new TestPojo("Bar"))
            .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build();
    this.enricherInput.send(message);
    Message<?> receive = replyChannel.receive(5000);
    assertNotNull(receive);/* w w w .j a v a2s.c  o  m*/
    assertEquals("Bar Bar", receive.getHeaders().get("foo"));
    Object payload = receive.getPayload();
    assertThat(payload, instanceOf(TestPojo.class));
    TestPojo result = (TestPojo) payload;
    assertEquals("Bar Bar", result.getName());
    assertNotNull(result.getDate());
    assertThat(new Date(), Matchers.greaterThanOrEqualTo(result.getDate()));
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testContentEnricher2() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload(new TestPojo("Bar"))
            .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build();
    this.enricherInput2.send(message);
    Message<?> receive = replyChannel.receive(5000);
    assertNotNull(receive);/*from   ww  w.  ja  va 2  s .c  om*/
    assertNull(receive.getHeaders().get("foo"));
    Object payload = receive.getPayload();
    assertThat(payload, instanceOf(TestPojo.class));
    TestPojo result = (TestPojo) payload;
    assertEquals("Bar Bar", result.getName());
    assertNotNull(result.getDate());
    assertThat(new Date(), Matchers.greaterThanOrEqualTo(result.getDate()));
}