Search in sources :

Example 16 with BindingProperties

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

the class MessageConverterConfigurer method configureMessageChannel.

/**
 * Setup data-type and message converters for the given message channel.
 *
 * @param channel message channel to set the data-type and message converters
 * @param channelName the channel name
 * @param inbound inbound (i.e., "input") or outbound channel
 */
private void configureMessageChannel(MessageChannel channel, String channelName, boolean inbound) {
    Assert.isAssignable(AbstractMessageChannel.class, channel.getClass());
    AbstractMessageChannel messageChannel = (AbstractMessageChannel) channel;
    BindingProperties bindingProperties = this.bindingServiceProperties.getBindingProperties(channelName);
    String contentType = bindingProperties.getContentType();
    ProducerProperties producerProperties = bindingProperties.getProducer();
    if (!inbound && producerProperties != null && producerProperties.isPartitioned()) {
        messageChannel.addInterceptor(new PartitioningInterceptor(bindingProperties, getPartitionKeyExtractorStrategy(producerProperties), getPartitionSelectorStrategy(producerProperties)));
    }
    ConsumerProperties consumerProperties = bindingProperties.getConsumer();
    if (this.isNativeEncodingNotSet(producerProperties, consumerProperties, inbound)) {
        if (inbound) {
            messageChannel.addInterceptor(new InboundContentTypeEnhancingInterceptor(contentType));
        } else {
            messageChannel.addInterceptor(new OutboundContentTypeConvertingInterceptor(contentType, this.compositeMessageConverterFactory.getMessageConverterForAllRegistered()));
        }
    }
}
Also used : ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties)

Example 17 with BindingProperties

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

the class MessageConverterConfigurer method configurePolledMessageSource.

@Override
public void configurePolledMessageSource(PollableMessageSource binding, String name) {
    BindingProperties bindingProperties = this.bindingServiceProperties.getBindingProperties(name);
    String contentType = bindingProperties.getContentType();
    ConsumerProperties consumerProperties = bindingProperties.getConsumer();
    if ((consumerProperties == null || !consumerProperties.isUseNativeDecoding()) && binding instanceof DefaultPollableMessageSource) {
        ((DefaultPollableMessageSource) binding).addInterceptor(new InboundContentTypeEnhancingInterceptor(contentType));
    }
}
Also used : BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultPollableMessageSource(org.springframework.cloud.stream.binder.DefaultPollableMessageSource)

Example 18 with BindingProperties

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

the class TurbineStreamAutoConfiguration method init.

@PostConstruct
public void init() {
    BindingProperties inputBinding = this.bindings.getBindings().get(TurbineStreamClient.INPUT);
    if (inputBinding == null) {
        this.bindings.getBindings().put(TurbineStreamClient.INPUT, new BindingProperties());
    }
    BindingProperties input = this.bindings.getBindings().get(TurbineStreamClient.INPUT);
    if (input.getDestination() == null) {
        input.setDestination(this.properties.getDestination());
    }
    if (input.getContentType() == null) {
        input.setContentType(this.properties.getContentType());
    }
}
Also used : BindingProperties(org.springframework.cloud.stream.config.BindingProperties) PostConstruct(javax.annotation.PostConstruct)

Example 19 with BindingProperties

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

the class AbstractBinderTests method testSendPojoReceivePojoWithStreamListenerDefaultContentType.

@SuppressWarnings("rawtypes")
@Test
public void testSendPojoReceivePojoWithStreamListenerDefaultContentType() 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%s0a", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bad%s0a", getDestinationNameDelimiter()), "test-1", moduleInputChannel, consumerBindingProperties.getConsumer());
    Station station = new Station();
    Message<?> message = MessageBuilder.withPayload(station).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 20 with BindingProperties

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

the class AbstractBinderTests method testSendAndReceiveJavaSerialization.

@Test
@SuppressWarnings({ "rawtypes", "deprecation" })
public void testSendAndReceiveJavaSerialization() 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%s0y", getDestinationNameDelimiter()), moduleOutputChannel, outputBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("foo%s0y", getDestinationNameDelimiter()), "testSendAndReceiveJavaSerialization", moduleInputChannel, inputBindingProperties.getConsumer());
    SerializableFoo foo = new SerializableFoo();
    Message<?> message = MessageBuilder.withPayload(foo).setHeader(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT).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");
    JavaSerializationMessageConverter converter = new JavaSerializationMessageConverter();
    SerializableFoo serializableFoo = (SerializableFoo) converter.convertFromInternal(inboundMessageRef.get(), SerializableFoo.class, null);
    assertNotNull(serializableFoo);
    assertThat(inboundMessageRef.get().getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)).isNull();
    assertThat(inboundMessageRef.get().getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT);
    producerBinding.unbind();
    consumerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) JavaSerializationMessageConverter(org.springframework.cloud.stream.converter.JavaSerializationMessageConverter) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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