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.cloud.stream.binder.rabbit.RabbitBinderTests.java

@Test
public void testAutoBindDLQPartionedConsumerFirst() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    properties.getExtension().setPrefix("bindertest.");
    properties.getExtension().setAutoBindDlq(true);
    properties.setMaxAttempts(1); // disable retry
    properties.setPartitioned(true);//from  w  w w . j  a v a  2s  . c  om
    properties.setInstanceIndex(0);
    DirectChannel input0 = createBindableChannel("input", createConsumerBindingProperties(properties));
    input0.setBeanName("test.input0DLQ");
    Binding<MessageChannel> input0Binding = binder.bindConsumer("partDLQ.0", "dlqPartGrp", input0, properties);
    Binding<MessageChannel> defaultConsumerBinding1 = binder.bindConsumer("partDLQ.0", "default",
            new QueueChannel(), properties);
    properties.setInstanceIndex(1);
    DirectChannel input1 = createBindableChannel("input1", createConsumerBindingProperties(properties));
    input1.setBeanName("test.input1DLQ");
    Binding<MessageChannel> input1Binding = binder.bindConsumer("partDLQ.0", "dlqPartGrp", input1, properties);
    Binding<MessageChannel> defaultConsumerBinding2 = binder.bindConsumer("partDLQ.0", "default",
            new QueueChannel(), properties);

    ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
    producerProperties.getExtension().setPrefix("bindertest.");
    producerProperties.getExtension().setAutoBindDlq(true);
    producerProperties.setPartitionKeyExtractorClass(PartitionTestSupport.class);
    producerProperties.setPartitionSelectorClass(PartitionTestSupport.class);
    producerProperties.setPartitionCount(2);
    BindingProperties bindingProperties = createProducerBindingProperties(producerProperties);
    DirectChannel output = createBindableChannel("output", bindingProperties);
    output.setBeanName("test.output");
    Binding<MessageChannel> outputBinding = binder.bindProducer("partDLQ.0", output, producerProperties);

    final CountDownLatch latch0 = new CountDownLatch(1);
    input0.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            if (latch0.getCount() <= 0) {
                throw new RuntimeException("dlq");
            }
            latch0.countDown();
        }

    });

    final CountDownLatch latch1 = new CountDownLatch(1);
    input1.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            if (latch1.getCount() <= 0) {
                throw new RuntimeException("dlq");
            }
            latch1.countDown();
        }

    });

    output.send(new GenericMessage<>(1));
    assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue();

    output.send(new GenericMessage<>(0));
    assertThat(latch0.await(10, TimeUnit.SECONDS)).isTrue();

    output.send(new GenericMessage<>(1));

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.setReceiveTimeout(10000);

    String streamDLQName = "bindertest.partDLQ.0.dlqPartGrp.dlq";

    org.springframework.amqp.core.Message received = template.receive(streamDLQName);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getReceivedRoutingKey())
            .isEqualTo("bindertest.partDLQ.0.dlqPartGrp-1");
    assertThat(received.getMessageProperties().getHeaders()).doesNotContainKey(BinderHeaders.PARTITION_HEADER);

    output.send(new GenericMessage<>(0));
    received = template.receive(streamDLQName);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getReceivedRoutingKey())
            .isEqualTo("bindertest.partDLQ.0.dlqPartGrp-0");
    assertThat(received.getMessageProperties().getHeaders()).doesNotContainKey(BinderHeaders.PARTITION_HEADER);

    input0Binding.unbind();
    input1Binding.unbind();
    defaultConsumerBinding1.unbind();
    defaultConsumerBinding2.unbind();
    outputBinding.unbind();
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

private void testAutoBindDLQPartionedConsumerFirstWithRepublishGuts(final boolean withRetry) throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    properties.getExtension().setPrefix("bindertest.");
    properties.getExtension().setAutoBindDlq(true);
    properties.getExtension().setRepublishToDlq(true);
    properties.getExtension().setRepublishDeliveyMode(MessageDeliveryMode.NON_PERSISTENT);
    properties.setMaxAttempts(withRetry ? 2 : 1);
    properties.setPartitioned(true);/*from   w w  w  .j  a va  2 s.  c  o m*/
    properties.setInstanceIndex(0);
    DirectChannel input0 = createBindableChannel("input", createConsumerBindingProperties(properties));
    input0.setBeanName("test.input0DLQ");
    Binding<MessageChannel> input0Binding = binder.bindConsumer("partPubDLQ.0", "dlqPartGrp", input0,
            properties);
    Binding<MessageChannel> defaultConsumerBinding1 = binder.bindConsumer("partPubDLQ.0", "default",
            new QueueChannel(), properties);
    properties.setInstanceIndex(1);
    DirectChannel input1 = createBindableChannel("input1", createConsumerBindingProperties(properties));
    input1.setBeanName("test.input1DLQ");
    Binding<MessageChannel> input1Binding = binder.bindConsumer("partPubDLQ.0", "dlqPartGrp", input1,
            properties);
    Binding<MessageChannel> defaultConsumerBinding2 = binder.bindConsumer("partPubDLQ.0", "default",
            new QueueChannel(), properties);

    ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
    producerProperties.getExtension().setPrefix("bindertest.");
    producerProperties.getExtension().setAutoBindDlq(true);
    producerProperties.setPartitionKeyExtractorClass(PartitionTestSupport.class);
    producerProperties.setPartitionSelectorClass(PartitionTestSupport.class);
    producerProperties.setPartitionCount(2);
    BindingProperties bindingProperties = createProducerBindingProperties(producerProperties);
    DirectChannel output = createBindableChannel("output", bindingProperties);
    output.setBeanName("test.output");
    Binding<MessageChannel> outputBinding = binder.bindProducer("partPubDLQ.0", output, producerProperties);

    final CountDownLatch latch0 = new CountDownLatch(1);
    input0.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            if (latch0.getCount() <= 0) {
                throw new RuntimeException("dlq");
            }
            latch0.countDown();
        }

    });

    final CountDownLatch latch1 = new CountDownLatch(1);
    input1.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            if (latch1.getCount() <= 0) {
                throw new RuntimeException("dlq");
            }
            latch1.countDown();
        }

    });

    ApplicationContext context = TestUtils.getPropertyValue(binder.getBinder(), "applicationContext",
            ApplicationContext.class);
    SubscribableChannel boundErrorChannel = context.getBean("bindertest.partPubDLQ.0.dlqPartGrp-0.errors",
            SubscribableChannel.class);
    SubscribableChannel globalErrorChannel = context.getBean("errorChannel", SubscribableChannel.class);
    final AtomicReference<Message<?>> boundErrorChannelMessage = new AtomicReference<>();
    final AtomicReference<Message<?>> globalErrorChannelMessage = new AtomicReference<>();
    final AtomicBoolean hasRecovererInCallStack = new AtomicBoolean(!withRetry);
    boundErrorChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            boundErrorChannelMessage.set(message);
            String stackTrace = Arrays.toString(new RuntimeException().getStackTrace());
            hasRecovererInCallStack.set(stackTrace.contains("ErrorMessageSendingRecoverer"));
        }

    });
    globalErrorChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            globalErrorChannelMessage.set(message);
        }

    });

    output.send(new GenericMessage<>(1));
    assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue();

    output.send(new GenericMessage<>(0));
    assertThat(latch0.await(10, TimeUnit.SECONDS)).isTrue();

    output.send(new GenericMessage<>(1));

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.setReceiveTimeout(10000);

    String streamDLQName = "bindertest.partPubDLQ.0.dlqPartGrp.dlq";

    org.springframework.amqp.core.Message received = template.receive(streamDLQName);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getHeaders().get("x-original-routingKey"))
            .isEqualTo("partPubDLQ.0-1");
    assertThat(received.getMessageProperties().getHeaders()).doesNotContainKey(BinderHeaders.PARTITION_HEADER);
    assertThat(received.getMessageProperties().getReceivedDeliveryMode())
            .isEqualTo(MessageDeliveryMode.NON_PERSISTENT);

    output.send(new GenericMessage<>(0));
    received = template.receive(streamDLQName);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getHeaders().get("x-original-routingKey"))
            .isEqualTo("partPubDLQ.0-0");
    assertThat(received.getMessageProperties().getHeaders()).doesNotContainKey(BinderHeaders.PARTITION_HEADER);

    // verify we got a message on the dedicated error channel and the global (via bridge)
    assertThat(boundErrorChannelMessage.get()).isNotNull();
    assertThat(globalErrorChannelMessage.get()).isNotNull();
    assertThat(hasRecovererInCallStack.get()).isEqualTo(withRetry);

    input0Binding.unbind();
    input1Binding.unbind();
    defaultConsumerBinding1.unbind();
    defaultConsumerBinding2.unbind();
    outputBinding.unbind();
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@Test
public void testAutoBindDLQPartitionedProducerFirst() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedProducerProperties<RabbitProducerProperties> properties = createProducerProperties();

    properties.getExtension().setPrefix("bindertest.");
    properties.getExtension().setAutoBindDlq(true);
    properties.setRequiredGroups("dlqPartGrp");
    properties.setPartitionKeyExtractorClass(PartitionTestSupport.class);
    properties.setPartitionSelectorClass(PartitionTestSupport.class);
    properties.setPartitionCount(2);//from  w  w  w. j a  va 2s  .  c om
    DirectChannel output = createBindableChannel("output", createProducerBindingProperties(properties));
    output.setBeanName("test.output");
    Binding<MessageChannel> outputBinding = binder.bindProducer("partDLQ.1", output, properties);

    ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
    consumerProperties.getExtension().setPrefix("bindertest.");
    consumerProperties.getExtension().setAutoBindDlq(true);
    consumerProperties.setMaxAttempts(1); // disable retry
    consumerProperties.setPartitioned(true);
    consumerProperties.setInstanceIndex(0);
    DirectChannel input0 = createBindableChannel("input", createConsumerBindingProperties(consumerProperties));
    input0.setBeanName("test.input0DLQ");
    Binding<MessageChannel> input0Binding = binder.bindConsumer("partDLQ.1", "dlqPartGrp", input0,
            consumerProperties);
    Binding<MessageChannel> defaultConsumerBinding1 = binder.bindConsumer("partDLQ.1", "defaultConsumer",
            new QueueChannel(), consumerProperties);
    consumerProperties.setInstanceIndex(1);
    DirectChannel input1 = createBindableChannel("input1", createConsumerBindingProperties(consumerProperties));
    input1.setBeanName("test.input1DLQ");
    Binding<MessageChannel> input1Binding = binder.bindConsumer("partDLQ.1", "dlqPartGrp", input1,
            consumerProperties);
    Binding<MessageChannel> defaultConsumerBinding2 = binder.bindConsumer("partDLQ.1", "defaultConsumer",
            new QueueChannel(), consumerProperties);

    final CountDownLatch latch0 = new CountDownLatch(1);
    input0.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            if (latch0.getCount() <= 0) {
                throw new RuntimeException("dlq");
            }
            latch0.countDown();
        }

    });

    final CountDownLatch latch1 = new CountDownLatch(1);
    input1.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            if (latch1.getCount() <= 0) {
                throw new RuntimeException("dlq");
            }
            latch1.countDown();
        }

    });

    output.send(new GenericMessage<Integer>(1));
    assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue();

    output.send(new GenericMessage<Integer>(0));
    assertThat(latch0.await(10, TimeUnit.SECONDS)).isTrue();

    output.send(new GenericMessage<Integer>(1));

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.setReceiveTimeout(10000);

    String streamDLQName = "bindertest.partDLQ.1.dlqPartGrp.dlq";

    org.springframework.amqp.core.Message received = template.receive(streamDLQName);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getReceivedRoutingKey())
            .isEqualTo("bindertest.partDLQ.1.dlqPartGrp-1");
    assertThat(received.getMessageProperties().getHeaders()).doesNotContainKey(BinderHeaders.PARTITION_HEADER);
    assertThat(received.getMessageProperties().getReceivedDeliveryMode())
            .isEqualTo(MessageDeliveryMode.PERSISTENT);

    output.send(new GenericMessage<Integer>(0));
    received = template.receive(streamDLQName);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getReceivedRoutingKey())
            .isEqualTo("bindertest.partDLQ.1.dlqPartGrp-0");
    assertThat(received.getMessageProperties().getHeaders()).doesNotContainKey(BinderHeaders.PARTITION_HEADER);

    input0Binding.unbind();
    input1Binding.unbind();
    defaultConsumerBinding1.unbind();
    defaultConsumerBinding2.unbind();
    outputBinding.unbind();
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@SuppressWarnings("unchecked")
@Test/* w  ww  .ja v a2  s  .  com*/
public void testBatchingAndCompression() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
    producerProperties.getExtension().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
    producerProperties.getExtension().setBatchingEnabled(true);
    producerProperties.getExtension().setBatchSize(2);
    producerProperties.getExtension().setBatchBufferLimit(100000);
    producerProperties.getExtension().setBatchTimeout(30000);
    producerProperties.getExtension().setCompress(true);
    producerProperties.setRequiredGroups("default");

    DirectChannel output = createBindableChannel("input", createProducerBindingProperties(producerProperties));
    output.setBeanName("batchingProducer");
    Binding<MessageChannel> producerBinding = binder.bindProducer("batching.0", output, producerProperties);

    Log logger = spy(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor.logger", Log.class));
    new DirectFieldAccessor(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor"))
            .setPropertyValue("logger", logger);
    when(logger.isTraceEnabled()).thenReturn(true);

    assertThat(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor.level"))
            .isEqualTo(Deflater.BEST_SPEED);

    output.send(new GenericMessage<>("foo".getBytes()));
    output.send(new GenericMessage<>("bar".getBytes()));

    Object out = spyOn("batching.0.default").receive(false);
    assertThat(out).isInstanceOf(byte[].class);
    assertThat(new String((byte[]) out)).isEqualTo("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar");

    ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
    verify(logger).trace(captor.capture());
    assertThat(captor.getValue().toString()).contains(("Compressed 14 to "));

    QueueChannel input = new QueueChannel();
    input.setBeanName("batchingConsumer");
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("batching.0", "test", input,
            createConsumerProperties());

    output.send(new GenericMessage<>("foo".getBytes()));
    output.send(new GenericMessage<>("bar".getBytes()));

    Message<byte[]> in = (Message<byte[]>) input.receive(10000);
    assertThat(in).isNotNull();
    assertThat(new String(in.getPayload())).isEqualTo("foo");
    in = (Message<byte[]>) input.receive(10000);
    assertThat(in).isNotNull();
    assertThat(new String(in.getPayload())).isEqualTo("bar");
    assertThat(in.getHeaders().get(AmqpHeaders.DELIVERY_MODE)).isNull();

    producerBinding.unbind();
    consumerBinding.unbind();
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@Test
public void testLateBinding() throws Exception {
    RabbitTestSupport.RabbitProxy proxy = new RabbitTestSupport.RabbitProxy();
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost", proxy.getPort());

    RabbitMessageChannelBinder rabbitBinder = new RabbitMessageChannelBinder(cf, new RabbitProperties(),
            new RabbitExchangeQueueProvisioner(cf));
    RabbitTestBinder binder = new RabbitTestBinder(cf, rabbitBinder);

    ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
    producerProperties.getExtension().setPrefix("latebinder.");
    producerProperties.getExtension().setAutoBindDlq(true);

    MessageChannel moduleOutputChannel = createBindableChannel("output",
            createProducerBindingProperties(producerProperties));
    Binding<MessageChannel> late0ProducerBinding = binder.bindProducer("late.0", moduleOutputChannel,
            producerProperties);//w  ww .  j  a v  a  2  s  .c o  m

    QueueChannel moduleInputChannel = new QueueChannel();
    ExtendedConsumerProperties<RabbitConsumerProperties> rabbitConsumerProperties = createConsumerProperties();
    rabbitConsumerProperties.getExtension().setPrefix("latebinder.");
    Binding<MessageChannel> late0ConsumerBinding = binder.bindConsumer("late.0", "test", moduleInputChannel,
            rabbitConsumerProperties);

    producerProperties
            .setPartitionKeyExpression(spelExpressionParser.parseExpression("payload.equals('0') ? 0 : 1"));
    producerProperties.setPartitionSelectorExpression(spelExpressionParser.parseExpression("hashCode()"));
    producerProperties.setPartitionCount(2);

    MessageChannel partOutputChannel = createBindableChannel("output",
            createProducerBindingProperties(producerProperties));
    Binding<MessageChannel> partlate0ProducerBinding = binder.bindProducer("partlate.0", partOutputChannel,
            producerProperties);

    QueueChannel partInputChannel0 = new QueueChannel();
    QueueChannel partInputChannel1 = new QueueChannel();

    ExtendedConsumerProperties<RabbitConsumerProperties> partLateConsumerProperties = createConsumerProperties();
    partLateConsumerProperties.getExtension().setPrefix("latebinder.");
    partLateConsumerProperties.setPartitioned(true);
    partLateConsumerProperties.setInstanceIndex(0);
    Binding<MessageChannel> partlate0Consumer0Binding = binder.bindConsumer("partlate.0", "test",
            partInputChannel0, partLateConsumerProperties);
    partLateConsumerProperties.setInstanceIndex(1);
    Binding<MessageChannel> partlate0Consumer1Binding = binder.bindConsumer("partlate.0", "test",
            partInputChannel1, partLateConsumerProperties);

    ExtendedProducerProperties<RabbitProducerProperties> noDlqProducerProperties = createProducerProperties();
    noDlqProducerProperties.getExtension().setPrefix("latebinder.");
    MessageChannel noDLQOutputChannel = createBindableChannel("output",
            createProducerBindingProperties(noDlqProducerProperties));
    Binding<MessageChannel> noDlqProducerBinding = binder.bindProducer("lateNoDLQ.0", noDLQOutputChannel,
            noDlqProducerProperties);

    QueueChannel noDLQInputChannel = new QueueChannel();
    ExtendedConsumerProperties<RabbitConsumerProperties> noDlqConsumerProperties = createConsumerProperties();
    noDlqConsumerProperties.getExtension().setPrefix("latebinder.");
    Binding<MessageChannel> noDlqConsumerBinding = binder.bindConsumer("lateNoDLQ.0", "test", noDLQInputChannel,
            noDlqConsumerProperties);

    MessageChannel outputChannel = createBindableChannel("output",
            createProducerBindingProperties(noDlqProducerProperties));
    Binding<MessageChannel> pubSubProducerBinding = binder.bindProducer("latePubSub", outputChannel,
            noDlqProducerProperties);
    QueueChannel pubSubInputChannel = new QueueChannel();
    noDlqConsumerProperties.getExtension().setDurableSubscription(false);
    Binding<MessageChannel> nonDurableConsumerBinding = binder.bindConsumer("latePubSub", "lategroup",
            pubSubInputChannel, noDlqConsumerProperties);
    QueueChannel durablePubSubInputChannel = new QueueChannel();
    noDlqConsumerProperties.getExtension().setDurableSubscription(true);
    Binding<MessageChannel> durableConsumerBinding = binder.bindConsumer("latePubSub", "lateDurableGroup",
            durablePubSubInputChannel, noDlqConsumerProperties);

    proxy.start();

    moduleOutputChannel.send(new GenericMessage<>("foo"));
    Message<?> message = moduleInputChannel.receive(10000);
    assertThat(message).isNotNull();
    assertThat(message.getPayload()).isNotNull();

    noDLQOutputChannel.send(new GenericMessage<>("bar"));
    message = noDLQInputChannel.receive(10000);
    assertThat(message);
    assertThat(message.getPayload()).isEqualTo("bar");

    outputChannel.send(new GenericMessage<>("baz"));
    message = pubSubInputChannel.receive(10000);
    assertThat(message);
    assertThat(message.getPayload()).isEqualTo("baz");
    message = durablePubSubInputChannel.receive(10000);
    assertThat(message).isNotNull();
    assertThat(message.getPayload()).isEqualTo("baz");

    partOutputChannel.send(new GenericMessage<>("0"));
    partOutputChannel.send(new GenericMessage<>("1"));
    message = partInputChannel0.receive(10000);
    assertThat(message).isNotNull();
    assertThat(message.getPayload()).isEqualTo("0");
    message = partInputChannel1.receive(10000);
    assertThat(message).isNotNull();
    assertThat(message.getPayload()).isEqualTo("1");

    late0ProducerBinding.unbind();
    late0ConsumerBinding.unbind();
    partlate0ProducerBinding.unbind();
    partlate0Consumer0Binding.unbind();
    partlate0Consumer1Binding.unbind();
    noDlqProducerBinding.unbind();
    noDlqConsumerBinding.unbind();
    pubSubProducerBinding.unbind();
    nonDurableConsumerBinding.unbind();
    durableConsumerBinding.unbind();

    binder.cleanup();

    proxy.stop();
    cf.destroy();

    this.rabbitAvailableRule.getResource().destroy();
}

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

@Test
public void testCompleteGroupWithinTimeout() 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);

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

    Message<?> reply = replyChannel.receive(10000);
    assertNotNull(reply);/* ww  w .  jav a 2s  . c om*/
    assertEquals(reply.getPayload(), 105);
}

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

@Test
public void testShouldNotSendPartialResultOnTimeoutByDefault() throws InterruptedException {
    QueueChannel discardChannel = new QueueChannel();
    this.aggregator.setDiscardChannel(discardChannel);
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = createMessage(3, "ABC", 2, 1, replyChannel, null);
    this.aggregator.handleMessage(message);
    this.store.expireMessageGroups(-10000);
    Message<?> reply = replyChannel.receive(1000);
    assertNull("No message should have been sent normally", reply);
    Message<?> discardedMessage = discardChannel.receive(1000);
    assertNotNull("A message should have been discarded", discardedMessage);
    assertEquals(message, discardedMessage);
    assertEquals(1, expiryEvents.size());
    assertSame(this.aggregator, expiryEvents.get(0).getSource());
    assertEquals("ABC", this.expiryEvents.get(0).getGroupId());
    assertEquals(1, this.expiryEvents.get(0).getMessageCount());
    assertEquals(true, this.expiryEvents.get(0).isDiscarded());
}

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

@Test
public void testShouldSendPartialResultOnTimeoutTrue() throws InterruptedException {
    this.aggregator.setSendPartialResultOnExpiry(true);
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message2);
    this.store.expireMessageGroups(-10000);
    Message<?> reply = replyChannel.receive(1000);
    assertNotNull("A reply message should have been received", reply);
    assertEquals(15, reply.getPayload());
    assertEquals(1, expiryEvents.size());
    assertSame(this.aggregator, expiryEvents.get(0).getSource());
    assertEquals("ABC", this.expiryEvents.get(0).getGroupId());
    assertEquals(2, this.expiryEvents.get(0).getMessageCount());
    assertEquals(false, this.expiryEvents.get(0).isDiscarded());
    Message<?> message3 = createMessage(5, "ABC", 3, 3, replyChannel, null);
    this.aggregator.handleMessage(message3);
    assertEquals(1, this.store.getMessageGroup("ABC").size());
}

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

@Test
public void testGroupRemainsAfterTimeout() throws InterruptedException {
    this.aggregator.setSendPartialResultOnExpiry(true);
    this.aggregator.setExpireGroupsUponTimeout(false);
    QueueChannel replyChannel = new QueueChannel();
    QueueChannel discardChannel = new QueueChannel();
    this.aggregator.setDiscardChannel(discardChannel);
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel, null);
    this.aggregator.handleMessage(message1);
    this.aggregator.handleMessage(message2);
    this.store.expireMessageGroups(-10000);
    Message<?> reply = replyChannel.receive(1000);
    assertNotNull("A reply message should have been received", reply);
    assertEquals(15, reply.getPayload());
    assertEquals(1, expiryEvents.size());
    assertSame(this.aggregator, expiryEvents.get(0).getSource());
    assertEquals("ABC", this.expiryEvents.get(0).getGroupId());
    assertEquals(2, this.expiryEvents.get(0).getMessageCount());
    assertEquals(false, this.expiryEvents.get(0).isDiscarded());
    assertEquals(0, this.store.getMessageGroup("ABC").size());
    Message<?> message3 = createMessage(5, "ABC", 3, 3, replyChannel, null);
    this.aggregator.handleMessage(message3);
    assertEquals(0, this.store.getMessageGroup("ABC").size());
    Message<?> discardedMessage = discardChannel.receive(1000);
    assertNotNull("A message should have been discarded", discardedMessage);
    assertSame(message3, discardedMessage);
}

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

@Test
public void testMultipleGroupsSimultaneously() throws InterruptedException {
    QueueChannel replyChannel1 = new QueueChannel();
    QueueChannel replyChannel2 = new QueueChannel();
    Message<?> message1 = createMessage(3, "ABC", 3, 1, replyChannel1, null);
    Message<?> message2 = createMessage(5, "ABC", 3, 2, replyChannel1, null);
    Message<?> message3 = createMessage(7, "ABC", 3, 3, replyChannel1, null);
    Message<?> message4 = createMessage(11, "XYZ", 3, 1, replyChannel2, null);
    Message<?> message5 = createMessage(13, "XYZ", 3, 2, replyChannel2, null);
    Message<?> message6 = createMessage(17, "XYZ", 3, 3, replyChannel2, null);
    aggregator.handleMessage(message1);//from  w  w w .  j  ava2 s.c  om
    aggregator.handleMessage(message5);
    aggregator.handleMessage(message3);
    aggregator.handleMessage(message6);
    aggregator.handleMessage(message4);
    aggregator.handleMessage(message2);
    @SuppressWarnings("unchecked")
    Message<Integer> reply1 = (Message<Integer>) replyChannel1.receive(1000);
    assertNotNull(reply1);
    assertThat(reply1.getPayload(), is(105));
    @SuppressWarnings("unchecked")
    Message<Integer> reply2 = (Message<Integer>) replyChannel2.receive(1000);
    assertNotNull(reply2);
    assertThat(reply2.getPayload(), is(2431));
}