Example usage for org.springframework.integration.aggregator BarrierMessageHandler BarrierMessageHandler

List of usage examples for org.springframework.integration.aggregator BarrierMessageHandler BarrierMessageHandler

Introduction

In this page you can find the example usage for org.springframework.integration.aggregator BarrierMessageHandler BarrierMessageHandler.

Prototype

public BarrierMessageHandler(long timeout) 

Source Link

Document

Construct an instance with the provided timeout and default correlation and output strategies.

Usage

From source file:org.springframework.integration.aggregator.BarrierMessageHandlerTests.java

@Test
public void testRequestBeforeReply() throws Exception {
    final BarrierMessageHandler handler = new BarrierMessageHandler(10000);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();//ww  w  .  ja v  a  2 s .  c om
    final AtomicReference<Exception> dupCorrelation = new AtomicReference<Exception>();
    final CountDownLatch latch = new CountDownLatch(1);
    Runnable runnable = () -> {
        try {
            handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
        } catch (MessagingException e) {
            dupCorrelation.set(e);
        }
        latch.countDown();
    };
    ExecutorService exec = Executors.newCachedThreadPool();
    exec.execute(runnable);
    exec.execute(runnable);
    Map<?, ?> suspensions = TestUtils.getPropertyValue(handler, "suspensions", Map.class);
    int n = 0;
    while (n++ < 100 && suspensions.size() == 0) {
        Thread.sleep(100);
    }
    Map<?, ?> inProcess = TestUtils.getPropertyValue(handler, "inProcess", Map.class);
    assertEquals(1, inProcess.size());
    assertTrue("suspension did not appear in time", n < 100);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertNotNull(dupCorrelation.get());
    assertThat(dupCorrelation.get().getMessage(), startsWith("Correlation key (foo) is already in use by"));
    handler.trigger(MessageBuilder.withPayload("bar").setCorrelationId("foo").build());
    Message<?> received = outputChannel.receive(10000);
    assertNotNull(received);
    List<?> result = (List<?>) received.getPayload();
    assertEquals("foo", result.get(0));
    assertEquals("bar", result.get(1));
    assertEquals(0, suspensions.size());
    assertEquals(0, inProcess.size());
}

From source file:org.springframework.integration.aggregator.BarrierMessageHandlerTests.java

@Test
public void testReplyBeforeRequest() throws Exception {
    final BarrierMessageHandler handler = new BarrierMessageHandler(10000);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();/*from  www.j a  v  a  2  s  .  co  m*/
    Executors.newSingleThreadExecutor()
            .execute(() -> handler.trigger(MessageBuilder.withPayload("bar").setCorrelationId("foo").build()));
    Map<?, ?> suspensions = TestUtils.getPropertyValue(handler, "suspensions", Map.class);
    int n = 0;
    while (n++ < 100 && suspensions.size() == 0) {
        Thread.sleep(100);
    }
    assertTrue("suspension did not appear in time", n < 100);
    handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    Message<?> received = outputChannel.receive(10000);
    assertNotNull(received);
    List<?> result = (ArrayList<?>) received.getPayload();
    assertEquals("foo", result.get(0));
    assertEquals("bar", result.get(1));
    assertEquals(0, suspensions.size());
}

From source file:org.springframework.integration.aggregator.BarrierMessageHandlerTests.java

@Test
public void testLateReply() throws Exception {
    final BarrierMessageHandler handler = new BarrierMessageHandler(0);
    QueueChannel outputChannel = new QueueChannel();
    QueueChannel discardChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.setDiscardChannelName("discards");
    handler.setChannelResolver(s -> discardChannel);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();//  ww w  . j  a  va 2s. c  o m
    final CountDownLatch latch = new CountDownLatch(1);
    Executors.newSingleThreadExecutor().execute(() -> {
        handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
        latch.countDown();
    });
    Map<?, ?> suspensions = TestUtils.getPropertyValue(handler, "suspensions", Map.class);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals("suspension not removed", 0, suspensions.size());
    Log logger = spy(TestUtils.getPropertyValue(handler, "logger", Log.class));
    new DirectFieldAccessor(handler).setPropertyValue("logger", logger);
    final Message<String> triggerMessage = MessageBuilder.withPayload("bar").setCorrelationId("foo").build();
    handler.trigger(triggerMessage);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger).error(captor.capture());
    assertThat(captor.getValue(),
            allOf(containsString("Suspending thread timed out or did not arrive within timeout for:"),
                    containsString("payload=bar")));
    assertEquals(0, suspensions.size());
    Message<?> discard = discardChannel.receive(0);
    assertSame(discard, triggerMessage);
    handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    assertEquals(0, suspensions.size());
}

From source file:org.springframework.integration.aggregator.BarrierMessageHandlerTests.java

@Test
public void testRequiresReply() throws Exception {
    final BarrierMessageHandler handler = new BarrierMessageHandler(0);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.setRequiresReply(true);//from   w w  w.  j  a va 2 s  .  c om
    handler.afterPropertiesSet();
    try {
        handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
        fail("exception expected");
    } catch (Exception e) {
        assertThat(e, Matchers.instanceOf(ReplyRequiredException.class));
    }
}

From source file:org.springframework.integration.aggregator.BarrierMessageHandlerTests.java

@Test
public void testExceptionReply() throws Exception {
    final BarrierMessageHandler handler = new BarrierMessageHandler(10000);
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();/*  www. jav  a  2 s . c  o m*/
    final AtomicReference<Exception> exception = new AtomicReference<Exception>();
    final CountDownLatch latch = new CountDownLatch(1);
    Executors.newSingleThreadExecutor().execute(() -> {
        try {
            handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
        } catch (Exception e) {
            exception.set(e);
            latch.countDown();
        }
    });
    Map<?, ?> suspensions = TestUtils.getPropertyValue(handler, "suspensions", Map.class);
    int n = 0;
    while (n++ < 100 && suspensions.size() == 0) {
        Thread.sleep(100);
    }
    assertTrue("suspension did not appear in time", n < 100);
    Exception exc = new RuntimeException();
    handler.trigger(MessageBuilder.withPayload(exc).setCorrelationId("foo").build());
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertSame(exc, exception.get().getCause());
    assertEquals(0, suspensions.size());
}