Search in sources :

Example 21 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method createProducerBindingProperties.

protected BindingProperties createProducerBindingProperties(PP producerProperties) {
    BindingProperties bindingProperties = new BindingProperties();
    bindingProperties.setProducer(producerProperties);
    return bindingProperties;
}
Also used : BindingProperties(org.springframework.cloud.stream.config.BindingProperties)

Example 22 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method testSendJsonReceiveJsonWithStreamListener.

@SuppressWarnings("rawtypes")
@Test
public void testSendJsonReceiveJsonWithStreamListener() throws Exception {
    StreamListenerMessageHandler handler = this.buildStreamListener(AbstractBinderTests.class, "echoStationString", String.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%s0e", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bad%s0e", getDestinationNameDelimiter()), "test-5", 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<String> reply = (Message<String>) channel.receive(5000);
    assertNotNull(reply);
    assertTrue(reply.getPayload() instanceof String);
    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 23 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method testSendAndReceive.

@SuppressWarnings("rawtypes")
@Test
public void testSendAndReceive() throws Exception {
    Binder binder = getBinder();
    BindingProperties outputBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties);
    BindingProperties inputBindingProperties = createConsumerBindingProperties(createConsumerProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", inputBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("foo%s0", getDestinationNameDelimiter()), moduleOutputChannel, outputBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("foo%s0", getDestinationNameDelimiter()), "testSendAndReceive", moduleInputChannel, inputBindingProperties.getConsumer());
    Message<?> message = MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
    // Let the consumer actually bind to the producer before sending a msg
    binderBindUnbindLatency();
    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().getPayload()).isEqualTo("foo".getBytes(StandardCharsets.UTF_8));
    assertThat(inboundMessageRef.get().getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)).isNull();
    assertThat(inboundMessageRef.get().getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()).isEqualTo("text/plain");
    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)

Example 24 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method testSendAndReceiveKryo.

@Test
@SuppressWarnings({ "rawtypes", "deprecation" })
public void testSendAndReceiveKryo() throws Exception {
    Binder binder = getBinder();
    BindingProperties outputBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties);
    BindingProperties inputBindingProperties = createConsumerBindingProperties(createConsumerProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", inputBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("foo%s0x", getDestinationNameDelimiter()), moduleOutputChannel, outputBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("foo%s0x", getDestinationNameDelimiter()), "testSendAndReceiveKryo", moduleInputChannel, inputBindingProperties.getConsumer());
    Foo foo = new Foo();
    foo.setName("Bill");
    Message<?> message = MessageBuilder.withPayload(foo).setHeader(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_OBJECT).build();
    // Let the consumer actually bind to the producer before sending a msg
    binderBindUnbindLatency();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Message<Foo>> inboundMessageRef = new AtomicReference<Message<Foo>>();
    moduleInputChannel.subscribe(message1 -> {
        try {
            inboundMessageRef.set((Message<Foo>) message1);
        } finally {
            latch.countDown();
        }
    });
    moduleOutputChannel.send(message);
    Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message");
    KryoMessageConverter kryo = new KryoMessageConverter(null, true);
    Foo fooPayload = (Foo) kryo.fromMessage(inboundMessageRef.get(), Foo.class);
    assertNotNull(fooPayload);
    assertThat(inboundMessageRef.get().getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)).isNull();
    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) KryoMessageConverter(org.springframework.cloud.stream.converter.KryoMessageConverter) Test(org.junit.Test)

Example 25 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class PartitionCapableBinderTests method testAnonymousGroup.

@Test
@SuppressWarnings("unchecked")
public void testAnonymousGroup() throws Exception {
    B binder = getBinder();
    BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel output = createBindableChannel("output", producerBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("defaultGroup%s0", getDestinationNameDelimiter()), output, (PP) producerBindingProperties.getProducer());
    QueueChannel input1 = new QueueChannel();
    Binding<MessageChannel> binding1 = binder.bindConsumer(String.format("defaultGroup%s0", getDestinationNameDelimiter()), null, input1, createConsumerProperties());
    QueueChannel input2 = new QueueChannel();
    Binding<MessageChannel> binding2 = binder.bindConsumer(String.format("defaultGroup%s0", getDestinationNameDelimiter()), null, input2, createConsumerProperties());
    String testPayload1 = "foo-" + UUID.randomUUID().toString();
    output.send(MessageBuilder.withPayload(testPayload1).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    Message<byte[]> receivedMessage1 = (Message<byte[]>) receive(input1);
    assertThat(receivedMessage1).isNotNull();
    assertThat(new String(receivedMessage1.getPayload())).isEqualTo(testPayload1);
    Message<byte[]> receivedMessage2 = (Message<byte[]>) receive(input2);
    assertThat(receivedMessage2).isNotNull();
    assertThat(new String(receivedMessage2.getPayload())).isEqualTo(testPayload1);
    binding2.unbind();
    String testPayload2 = "foo-" + UUID.randomUUID().toString();
    output.send(MessageBuilder.withPayload(testPayload2).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    binding2 = binder.bindConsumer(String.format("defaultGroup%s0", getDestinationNameDelimiter()), null, input2, createConsumerProperties());
    String testPayload3 = "foo-" + UUID.randomUUID().toString();
    output.send(MessageBuilder.withPayload(testPayload3).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    receivedMessage1 = (Message<byte[]>) receive(input1);
    assertThat(receivedMessage1).isNotNull();
    assertThat(new String(receivedMessage1.getPayload())).isEqualTo(testPayload2);
    receivedMessage1 = (Message<byte[]>) receive(input1);
    assertThat(receivedMessage1).isNotNull();
    assertThat(new String(receivedMessage1.getPayload())).isNotNull();
    receivedMessage2 = (Message<byte[]>) receive(input2);
    assertThat(receivedMessage2).isNotNull();
    assertThat(new String(receivedMessage2.getPayload())).isEqualTo(testPayload3);
    producerBinding.unbind();
    binding1.unbind();
    binding2.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) Test(org.junit.Test)

Aggregations

BindingProperties (org.springframework.cloud.stream.config.BindingProperties)34 Test (org.junit.Test)26 DirectChannel (org.springframework.integration.channel.DirectChannel)21 MessageChannel (org.springframework.messaging.MessageChannel)21 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)12 QueueChannel (org.springframework.integration.channel.QueueChannel)10 Message (org.springframework.messaging.Message)9 HashMap (java.util.HashMap)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)8 DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)8 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)6 Binder (org.springframework.cloud.stream.binder.Binder)5 Binding (org.springframework.cloud.stream.binder.Binding)5 StreamListenerMessageHandler (org.springframework.cloud.stream.binding.StreamListenerMessageHandler)5 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)4 CompositeMessageConverterFactory (org.springframework.cloud.stream.converter.CompositeMessageConverterFactory)4 ExtendedProducerProperties (org.springframework.cloud.stream.binder.ExtendedProducerProperties)3