Search in sources :

Example 6 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-cloud-stream by spring-cloud.

the class PartitionCapableBinderTests method testOneRequiredGroup.

@Test
public void testOneRequiredGroup() throws Exception {
    B binder = getBinder();
    PP producerProperties = createProducerProperties();
    DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties));
    String testDestination = "testDestination" + UUID.randomUUID().toString().replace("-", "");
    producerProperties.setRequiredGroups("test1");
    Binding<MessageChannel> producerBinding = binder.bindProducer(testDestination, output, producerProperties);
    String testPayload = "foo-" + UUID.randomUUID().toString();
    output.send(MessageBuilder.withPayload(testPayload).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    QueueChannel inbound1 = new QueueChannel();
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(testDestination, "test1", inbound1, createConsumerProperties());
    Message<?> receivedMessage1 = receive(inbound1);
    assertThat(receivedMessage1).isNotNull();
    assertThat(new String((byte[]) receivedMessage1.getPayload())).isEqualTo(testPayload);
    producerBinding.unbind();
    consumerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Test(org.junit.Test)

Example 7 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-cloud-stream by spring-cloud.

the class PartitionCapableBinderTests method testPartitionedModuleSpEL.

@Test
public void testPartitionedModuleSpEL() throws Exception {
    B binder = getBinder();
    CP consumerProperties = createConsumerProperties();
    consumerProperties.setConcurrency(2);
    consumerProperties.setInstanceIndex(0);
    consumerProperties.setInstanceCount(3);
    consumerProperties.setPartitioned(true);
    QueueChannel input0 = new QueueChannel();
    input0.setBeanName("test.input0S");
    Binding<MessageChannel> input0Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input0, consumerProperties);
    consumerProperties.setInstanceIndex(1);
    QueueChannel input1 = new QueueChannel();
    input1.setBeanName("test.input1S");
    Binding<MessageChannel> input1Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input1, consumerProperties);
    consumerProperties.setInstanceIndex(2);
    QueueChannel input2 = new QueueChannel();
    input2.setBeanName("test.input2S");
    Binding<MessageChannel> input2Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input2, consumerProperties);
    PP producerProperties = createProducerProperties();
    producerProperties.setPartitionKeyExpression(spelExpressionParser.parseExpression("payload"));
    producerProperties.setPartitionSelectorExpression(spelExpressionParser.parseExpression("hashCode()"));
    producerProperties.setPartitionCount(3);
    DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties));
    output.setBeanName("test.output");
    Binding<MessageChannel> outputBinding = binder.bindProducer(String.format("part%s0", getDestinationNameDelimiter()), output, producerProperties);
    try {
        Object endpoint = extractEndpoint(outputBinding);
        checkRkExpressionForPartitionedModuleSpEL(endpoint);
    } catch (UnsupportedOperationException ignored) {
    }
    Message<String> message2 = MessageBuilder.withPayload("2").setHeader(IntegrationMessageHeaderAccessor.CORRELATION_ID, "foo").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, 42).setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, 43).build();
    output.send(message2);
    output.send(MessageBuilder.withPayload("1").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    output.send(MessageBuilder.withPayload("0").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    Message<?> receive0 = receive(input0);
    assertThat(receive0).isNotNull();
    Message<?> receive1 = receive(input1);
    assertThat(receive1).isNotNull();
    Message<?> receive2 = receive(input2);
    assertThat(receive2).isNotNull();
    Condition<Message<?>> correlationHeadersForPayload2 = new Condition<Message<?>>() {

        @Override
        public boolean matches(Message<?> value) {
            IntegrationMessageHeaderAccessor accessor = new IntegrationMessageHeaderAccessor(value);
            return "foo".equals(accessor.getCorrelationId()) && 42 == accessor.getSequenceNumber() && 43 == accessor.getSequenceSize();
        }
    };
    if (usesExplicitRouting()) {
        assertThat(receive0.getPayload()).isEqualTo("0".getBytes());
        assertThat(receive1.getPayload()).isEqualTo("1".getBytes());
        assertThat(receive2.getPayload()).isEqualTo("2".getBytes());
        assertThat(receive2).has(correlationHeadersForPayload2);
    } else {
        List<Message<?>> receivedMessages = Arrays.asList(receive0, receive1, receive2);
        assertThat(receivedMessages).extracting("payload").containsExactlyInAnyOrder("0".getBytes(), "1".getBytes(), "2".getBytes());
        Condition<Message<?>> payloadIs2 = new Condition<Message<?>>() {

            @Override
            public boolean matches(Message<?> value) {
                return value.getPayload().equals("2".getBytes());
            }
        };
        assertThat(receivedMessages).filteredOn(payloadIs2).areExactly(1, correlationHeadersForPayload2);
    }
    input0Binding.unbind();
    input1Binding.unbind();
    input2Binding.unbind();
    outputBinding.unbind();
}
Also used : Condition(org.assertj.core.api.Condition) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 8 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-cloud-stream by spring-cloud.

the class PartitionCapableBinderTests method testTwoRequiredGroups.

@Test
public void testTwoRequiredGroups() throws Exception {
    B binder = getBinder();
    PP producerProperties = createProducerProperties();
    DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties));
    String testDestination = "testDestination" + UUID.randomUUID().toString().replace("-", "");
    producerProperties.setRequiredGroups("test1", "test2");
    Binding<MessageChannel> producerBinding = binder.bindProducer(testDestination, output, producerProperties);
    String testPayload = "foo-" + UUID.randomUUID().toString();
    output.send(MessageBuilder.withPayload(testPayload).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    QueueChannel inbound1 = new QueueChannel();
    Binding<MessageChannel> consumerBinding1 = binder.bindConsumer(testDestination, "test1", inbound1, createConsumerProperties());
    QueueChannel inbound2 = new QueueChannel();
    Binding<MessageChannel> consumerBinding2 = binder.bindConsumer(testDestination, "test2", inbound2, createConsumerProperties());
    Message<?> receivedMessage1 = receive(inbound1);
    assertThat(receivedMessage1).isNotNull();
    assertThat(new String((byte[]) receivedMessage1.getPayload())).isEqualTo(testPayload);
    Message<?> receivedMessage2 = receive(inbound2);
    assertThat(receivedMessage2).isNotNull();
    assertThat(new String((byte[]) receivedMessage2.getPayload())).isEqualTo(testPayload);
    consumerBinding1.unbind();
    consumerBinding2.unbind();
    producerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Test(org.junit.Test)

Example 9 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-cloud-stream by spring-cloud.

the class MessageConverterConfigurerTests method testConfigureOutputChannelCannotConvert.

@Test
@Ignore
public void testConfigureOutputChannelCannotConvert() {
    BindingServiceProperties props = new BindingServiceProperties();
    BindingProperties bindingProps = new BindingProperties();
    bindingProps.setContentType("foo/bar");
    props.setBindings(Collections.singletonMap("foo", bindingProps));
    MessageConverter converter = new AbstractMessageConverter(new MimeType("foo", "bar")) {

        @Override
        protected boolean supports(Class<?> clazz) {
            return true;
        }

        @Override
        protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
            return null;
        }
    };
    CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory(Collections.<MessageConverter>singletonList(converter), null);
    MessageConverterConfigurer configurer = new MessageConverterConfigurer(props, converterFactory);
    QueueChannel out = new QueueChannel();
    configurer.configureOutputChannel(out, "foo");
    try {
        out.send(new GenericMessage<Foo>(new Foo(), Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE, "bad/ct")));
        fail("Expected MessageConversionException: " + out.receive(0));
    } catch (MessageConversionException e) {
        assertThat(e.getMessage()).endsWith("to the configured output type: 'foo/bar'");
    }
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) QueueChannel(org.springframework.integration.channel.QueueChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AbstractMessageConverter(org.springframework.messaging.converter.AbstractMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) AbstractMessageConverter(org.springframework.messaging.converter.AbstractMessageConverter) MimeType(org.springframework.util.MimeType) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) MessageHeaders(org.springframework.messaging.MessageHeaders) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration-samples by spring-projects.

the class AggregatorDemoTest method testGatewayDemo.

@Test
public void testGatewayDemo() throws InterruptedException {
    System.setProperty("spring.profiles.active", "testCase");
    final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(configFilesGatewayDemo);
    final MessageChannel stdinToJmsOutChannel = applicationContext.getBean("stdinToJmsOutChannel", MessageChannel.class);
    stdinToJmsOutChannel.send(MessageBuilder.withPayload("jms test").build());
    final QueueChannel queueChannel = applicationContext.getBean("queueChannel", QueueChannel.class);
    @SuppressWarnings("unchecked") Message<List<String>> reply = (Message<List<String>>) queueChannel.receive(600000);
    Assert.assertNotNull(reply);
    List<String> out = reply.getPayload();
    Assert.assertEquals("[JMS TEST, JMS TEST]", out.toString());
    applicationContext.close();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) List(java.util.List) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) Test(org.junit.Test)

Aggregations

QueueChannel (org.springframework.integration.channel.QueueChannel)709 Test (org.junit.Test)669 GenericMessage (org.springframework.messaging.support.GenericMessage)186 Message (org.springframework.messaging.Message)173 BeanFactory (org.springframework.beans.factory.BeanFactory)162 MessageChannel (org.springframework.messaging.MessageChannel)100 Matchers.containsString (org.hamcrest.Matchers.containsString)66 CountDownLatch (java.util.concurrent.CountDownLatch)59 DirectChannel (org.springframework.integration.channel.DirectChannel)57 ArrayList (java.util.ArrayList)55 PollableChannel (org.springframework.messaging.PollableChannel)55 MessagingException (org.springframework.messaging.MessagingException)53 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 Socket (java.net.Socket)44 ErrorMessage (org.springframework.messaging.support.ErrorMessage)42 ServerSocket (java.net.ServerSocket)41 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)39 IOException (java.io.IOException)35 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)35