Search in sources :

Example 76 with DirectChannel

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

the class AbstractBinderTests method testStreamListenerJavaSerializationNonSerializable.

@SuppressWarnings("rawtypes")
@Test(expected = MessageDeliveryException.class)
public void testStreamListenerJavaSerializationNonSerializable() throws Exception {
    Binder binder = getBinder();
    BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties);
    BindingProperties consumerBindingProperties = createConsumerBindingProperties(createConsumerProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("bad%s0c", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bad%s0c", getDestinationNameDelimiter()), "test-3", moduleInputChannel, consumerBindingProperties.getConsumer());
    try {
        Station station = new Station();
        Message<?> message = MessageBuilder.withPayload(station).setHeader(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT).build();
        moduleOutputChannel.send(message);
    } finally {
        producerBinding.unbind();
        consumerBinding.unbind();
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) Test(org.junit.Test)

Example 77 with DirectChannel

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

the class AbstractBinderTests method testSendJsonReceivePojoWithStreamListener.

@SuppressWarnings("rawtypes")
@Test
public void testSendJsonReceivePojoWithStreamListener() throws Exception {
    StreamListenerMessageHandler handler = this.buildStreamListener(AbstractBinderTests.class, "echoStation", Station.class);
    Binder binder = getBinder();
    BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties);
    BindingProperties consumerBindingProperties = createConsumerBindingProperties(createConsumerProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("bad%s0d", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bad%s0d", getDestinationNameDelimiter()), "test-4", moduleInputChannel, consumerBindingProperties.getConsumer());
    String value = "{\"readings\":[{\"stationid\":\"fgh\"," + "\"customerid\":\"12345\",\"timestamp\":null},{\"stationid\":\"hjk\",\"customerid\":\"222\",\"timestamp\":null}]}";
    Message<?> message = MessageBuilder.withPayload(value).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON).build();
    moduleInputChannel.subscribe(handler);
    moduleOutputChannel.send(message);
    QueueChannel channel = (QueueChannel) handler.getOutputChannel();
    Message<Station> reply = (Message<Station>) channel.receive(5000);
    assertNotNull(reply);
    assertTrue(reply.getPayload() instanceof Station);
    producerBinding.unbind();
    consumerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) StreamListenerMessageHandler(org.springframework.cloud.stream.binding.StreamListenerMessageHandler) Test(org.junit.Test)

Example 78 with DirectChannel

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

the class AbstractBinderTests method testSendPojoReceivePojoKryoWithStreamListener.

@SuppressWarnings("rawtypes")
@Test
public void testSendPojoReceivePojoKryoWithStreamListener() throws Exception {
    StreamListenerMessageHandler handler = this.buildStreamListener(AbstractBinderTests.class, "echoStation", Station.class);
    Binder binder = getBinder();
    BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties);
    BindingProperties consumerBindingProperties = createConsumerBindingProperties(createConsumerProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("bad%s0b", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bad%s0b", getDestinationNameDelimiter()), "test-2", moduleInputChannel, consumerBindingProperties.getConsumer());
    Station station = new Station();
    Message<?> message = MessageBuilder.withPayload(station).setHeader(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_OBJECT).build();
    moduleInputChannel.subscribe(handler);
    moduleOutputChannel.send(message);
    QueueChannel replyChannel = (QueueChannel) handler.getOutputChannel();
    Message<?> replyMessage = replyChannel.receive(5000);
    assertTrue(replyMessage.getPayload() instanceof Station);
    producerBinding.unbind();
    consumerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) StreamListenerMessageHandler(org.springframework.cloud.stream.binding.StreamListenerMessageHandler) Test(org.junit.Test)

Example 79 with DirectChannel

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

the class AbstractBinderTests method testSendAndReceiveMultipleTopics.

@Test
@SuppressWarnings("rawtypes")
public void testSendAndReceiveMultipleTopics() throws Exception {
    Binder binder = getBinder();
    BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel1 = createBindableChannel("output1", producerBindingProperties);
    DirectChannel moduleOutputChannel2 = createBindableChannel("output2", producerBindingProperties);
    QueueChannel moduleInputChannel = new QueueChannel();
    Binding<MessageChannel> producerBinding1 = binder.bindProducer(String.format("foo%sxy", getDestinationNameDelimiter()), moduleOutputChannel1, producerBindingProperties.getProducer());
    Binding<MessageChannel> producerBinding2 = binder.bindProducer(String.format("foo%syz", getDestinationNameDelimiter()), moduleOutputChannel2, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding1 = binder.bindConsumer(String.format("foo%sxy", getDestinationNameDelimiter()), "testSendAndReceiveMultipleTopics", moduleInputChannel, createConsumerProperties());
    Binding<MessageChannel> consumerBinding2 = binder.bindConsumer(String.format("foo%syz", getDestinationNameDelimiter()), "testSendAndReceiveMultipleTopics", moduleInputChannel, createConsumerProperties());
    String testPayload1 = "foo" + UUID.randomUUID().toString();
    Message<?> message1 = MessageBuilder.withPayload(testPayload1.getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM).build();
    String testPayload2 = "foo" + UUID.randomUUID().toString();
    Message<?> message2 = MessageBuilder.withPayload(testPayload2.getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM).build();
    // Let the consumer actually bind to the producer before sending a msg
    binderBindUnbindLatency();
    moduleOutputChannel1.send(message1);
    moduleOutputChannel2.send(message2);
    Message<?>[] messages = new Message[2];
    messages[0] = receive(moduleInputChannel);
    messages[1] = receive(moduleInputChannel);
    assertThat(messages[0]).isNotNull();
    assertThat(messages[1]).isNotNull();
    assertThat(messages).extracting("payload").containsExactlyInAnyOrder(testPayload1.getBytes(), testPayload2.getBytes());
    producerBinding1.unbind();
    producerBinding2.unbind();
    consumerBinding1.unbind();
    consumerBinding2.unbind();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) Test(org.junit.Test)

Example 80 with DirectChannel

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

the class AbstractBinderTests method testSendAndReceiveNoOriginalContentType.

@Test
@SuppressWarnings("rawtypes")
public void testSendAndReceiveNoOriginalContentType() throws Exception {
    Binder binder = getBinder();
    BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties);
    BindingProperties inputBindingProperties = createConsumerBindingProperties(createConsumerProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", inputBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("bar%s0", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bar%s0", getDestinationNameDelimiter()), "testSendAndReceiveNoOriginalContentType", moduleInputChannel, createConsumerProperties());
    binderBindUnbindLatency();
    Message<?> message = MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build();
    moduleOutputChannel.send(message);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Message<byte[]>> inboundMessageRef = new AtomicReference<Message<byte[]>>();
    moduleInputChannel.subscribe(message1 -> {
        try {
            inboundMessageRef.set((Message<byte[]>) message1);
        } finally {
            latch.countDown();
        }
    });
    moduleOutputChannel.send(message);
    Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message");
    assertThat(inboundMessageRef.get()).isNotNull();
    assertThat(new String(inboundMessageRef.get().getPayload(), StandardCharsets.UTF_8)).isEqualTo("foo");
    assertThat(inboundMessageRef.get().getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()).isEqualTo(MimeTypeUtils.TEXT_PLAIN_VALUE);
    producerBinding.unbind();
    consumerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)215 Test (org.junit.Test)182 MessageChannel (org.springframework.messaging.MessageChannel)71 Message (org.springframework.messaging.Message)68 QueueChannel (org.springframework.integration.channel.QueueChannel)63 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)38 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 CountDownLatch (java.util.concurrent.CountDownLatch)27 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 HashMap (java.util.HashMap)22 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 MessagingException (org.springframework.messaging.MessagingException)18 Properties (java.util.Properties)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 SubscribableChannel (org.springframework.messaging.SubscribableChannel)15 Expression (org.springframework.expression.Expression)14