Search in sources :

Example 1 with QueueChannel

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

the class AbstractBinderTests method testSendPojoReceivePojoWithStreamListener.

@SuppressWarnings("rawtypes")
@Test
public void testSendPojoReceivePojoWithStreamListener() 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%s0f", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bad%s0f", getDestinationNameDelimiter()), "test-6", moduleInputChannel, consumerBindingProperties.getConsumer());
    Readings r1 = new Readings();
    r1.setCustomerid("123");
    r1.setStationid("XYZ");
    Readings r2 = new Readings();
    r2.setCustomerid("546");
    r2.setStationid("ABC");
    Station station = new Station();
    station.setReadings(Arrays.asList(r1, r2));
    Message<?> message = MessageBuilder.withPayload(station).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) Readings(org.springframework.cloud.stream.binder.AbstractBinderTests.Station.Readings) StreamListenerMessageHandler(org.springframework.cloud.stream.binding.StreamListenerMessageHandler) Test(org.junit.Test)

Example 2 with QueueChannel

use of org.springframework.integration.channel.QueueChannel 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 3 with QueueChannel

use of org.springframework.integration.channel.QueueChannel 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 4 with QueueChannel

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

the class AbstractBinderTests method buildStreamListener.

private StreamListenerMessageHandler buildStreamListener(Class<?> handlerClass, String handlerMethodName, Class<?>... parameters) throws Exception {
    String channelName = "reply_" + System.nanoTime();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton(channelName, new QueueChannel());
    Method m = ReflectionUtils.findMethod(handlerClass, handlerMethodName, parameters);
    InvocableHandlerMethod method = new InvocableHandlerMethod(this, m);
    HandlerMethodArgumentResolverComposite resolver = new HandlerMethodArgumentResolverComposite();
    CompositeMessageConverterFactory factory = new CompositeMessageConverterFactory();
    resolver.addResolver(new PayloadArgumentResolver(factory.getMessageConverterForAllRegistered()));
    method.setMessageMethodArgumentResolvers(resolver);
    Constructor<?> c = ReflectionUtils.accessibleConstructor(StreamListenerMessageHandler.class, InvocableHandlerMethod.class, boolean.class, String[].class);
    StreamListenerMessageHandler handler = (StreamListenerMessageHandler) c.newInstance(method, false, new String[] {});
    handler.setOutputChannelName(channelName);
    handler.setBeanFactory(context);
    handler.afterPropertiesSet();
    context.refresh();
    return handler;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) QueueChannel(org.springframework.integration.channel.QueueChannel) InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) Method(java.lang.reflect.Method) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) PayloadArgumentResolver(org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver) HandlerMethodArgumentResolverComposite(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite) StreamListenerMessageHandler(org.springframework.cloud.stream.binding.StreamListenerMessageHandler)

Example 5 with QueueChannel

use of org.springframework.integration.channel.QueueChannel 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)

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