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

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

Introduction

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

Prototype

public QueueChannel() 

Source Link

Document

Create a channel with "unbounded" queue capacity.

Usage

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

@Test
@Ignore//from   w ww. ja  v a  2 s  .c  o  m
// dropped backwards compatibility for setting capacity limit (it's always Integer.MAX_VALUE)
public void testTrackedCorrelationIdsCapacityAtLimit() {
    QueueChannel replyChannel = new QueueChannel();
    QueueChannel discardChannel = new QueueChannel();

    this.aggregator.setDiscardChannel(discardChannel);
    this.aggregator.handleMessage(createMessage(1, 1, 1, 1, replyChannel, null));
    assertEquals(1, replyChannel.receive(1000).getPayload());
    this.aggregator.handleMessage(createMessage(3, 2, 1, 1, replyChannel, null));
    assertEquals(3, replyChannel.receive(1000).getPayload());
    this.aggregator.handleMessage(createMessage(4, 3, 1, 1, replyChannel, null));
    assertEquals(4, replyChannel.receive(1000).getPayload());
    // next message with same correllation ID is discarded
    this.aggregator.handleMessage(createMessage(2, 1, 1, 1, replyChannel, null));
    assertEquals(2, discardChannel.receive(1000).getPayload());
}

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

@Test
@Ignore/*from ww w.ja v a 2  s  . c  o m*/
// dropped backwards compatibility for setting capacity limit (it's always Integer.MAX_VALUE)
public void testTrackedCorrelationIdsCapacityPassesLimit() {
    QueueChannel replyChannel = new QueueChannel();
    QueueChannel discardChannel = new QueueChannel();

    this.aggregator.setDiscardChannel(discardChannel);
    this.aggregator.handleMessage(createMessage(1, 1, 1, 1, replyChannel, null));
    assertEquals(1, replyChannel.receive(1000).getPayload());
    this.aggregator.handleMessage(createMessage(2, 2, 1, 1, replyChannel, null));
    assertEquals(2, replyChannel.receive(1000).getPayload());
    this.aggregator.handleMessage(createMessage(3, 3, 1, 1, replyChannel, null));
    assertEquals(3, replyChannel.receive(1000).getPayload());
    this.aggregator.handleMessage(createMessage(4, 4, 1, 1, replyChannel, null));
    assertEquals(4, replyChannel.receive(1000).getPayload());
    this.aggregator.handleMessage(createMessage(5, 1, 1, 1, replyChannel, null));
    assertEquals(5, replyChannel.receive(1000).getPayload());
    assertNull(discardChannel.receive(0));
}

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

@Test(expected = MessageHandlingException.class)
public void testExceptionThrownIfNoCorrelationId() throws InterruptedException {
    Message<?> message = createMessage(3, null, 2, 1, new QueueChannel(), null);
    this.aggregator.handleMessage(message);
}

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

@Test
public void testAdditionalMessageAfterCompletion() throws InterruptedException {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    Message<?> message3 = createMessage(7, "ABC", 3, 3, replyChannel, null);
    Message<?> message4 = createMessage(7, "ABC", 3, 3, replyChannel, null);

    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message2);
    this.aggregator.handleMessage(message3);
    this.aggregator.handleMessage(message4);

    Message<?> reply = replyChannel.receive(10000);
    assertNotNull("A message should be aggregated", reply);
    assertThat(((Integer) reply.getPayload()), is(105));
}

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

@Test
public void shouldRejectDuplicatedSequenceNumbers() throws InterruptedException {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    Message<?> message3 = createMessage(7, "ABC", 3, 3, replyChannel, null);
    Message<?> message4 = createMessage(7, "ABC", 3, 3, replyChannel, null);
    this.aggregator.setReleaseStrategy(new SequenceSizeReleaseStrategy());

    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message3);
    // duplicated sequence number, either message3 or message4 should be rejected
    this.aggregator.handleMessage(message4);
    this.aggregator.handleMessage(message2);

    Message<?> reply = replyChannel.receive(10000);
    assertNotNull("A message should be aggregated", reply);
    assertThat(((Integer) reply.getPayload()), is(105));
}

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  2s . c  o  m*/
    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();/* w w  w . j av a  2 s.c o 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();//from   w w w . ja  v  a2s  . 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  v  a 2s.  co m*/
    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();/*from w  ww . ja v a2s.co  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());
}