Search in sources :

Example 1 with ConsumerProperties

use of org.springframework.cloud.stream.binder.ConsumerProperties in project spring-cloud-stream by spring-cloud.

the class BindingServiceTests method testConsumerPropertiesValidation.

@Test
public void testConsumerPropertiesValidation() {
    BindingServiceProperties serviceProperties = new BindingServiceProperties();
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    ConsumerProperties consumerProperties = new ConsumerProperties();
    consumerProperties.setConcurrency(0);
    props.setDestination("foo");
    props.setConsumer(consumerProperties);
    final String inputChannelName = "input";
    bindingProperties.put(inputChannelName, props);
    serviceProperties.setBindings(bindingProperties);
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    BindingService service = new BindingService(serviceProperties, binderFactory);
    MessageChannel inputChannel = new DirectChannel();
    try {
        service.bindConsumer(inputChannel, inputChannelName);
        fail("Consumer properties should be validated.");
    } catch (IllegalStateException e) {
        assertThat(e).hasMessageContaining("Concurrency should be greater than zero.");
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) Test(org.junit.Test)

Example 2 with ConsumerProperties

use of org.springframework.cloud.stream.binder.ConsumerProperties 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 3 with ConsumerProperties

use of org.springframework.cloud.stream.binder.ConsumerProperties 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 4 with ConsumerProperties

use of org.springframework.cloud.stream.binder.ConsumerProperties in project spring-cloud-consul by spring-cloud.

the class TestConsumer method run.

@Override
public void run(ApplicationArguments args) throws Exception {
    logger.info("Consumer running with binder {}", binder);
    SubscribableChannel consumerChannel = new ExecutorSubscribableChannel();
    consumerChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            messagePayload = (String) message.getPayload();
            logger.info("Received message: {}", messagePayload);
        }
    });
    String group = null;
    if (args.containsOption("group")) {
        group = args.getOptionValues("group").get(0);
    }
    binder.bindConsumer(ConsulBinderTests.BINDING_NAME, group, consumerChannel, new ConsumerProperties());
    isBound = true;
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) MessageHandler(org.springframework.messaging.MessageHandler) MessagingException(org.springframework.messaging.MessagingException) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties)

Example 5 with ConsumerProperties

use of org.springframework.cloud.stream.binder.ConsumerProperties in project spring-cloud-stream by spring-cloud.

the class BindingServiceProperties method getConsumerProperties.

public ConsumerProperties getConsumerProperties(String inputBindingName) {
    Assert.notNull(inputBindingName, "The input binding name cannot be null");
    BindingProperties bindingProperties = getBindingProperties(inputBindingName);
    ConsumerProperties consumerProperties = bindingProperties.getConsumer();
    if (consumerProperties == null) {
        consumerProperties = new ConsumerProperties();
        bindingProperties.setConsumer(consumerProperties);
    }
    // propagate instance count and instance index if not already set
    if (consumerProperties.getInstanceCount() < 0) {
        consumerProperties.setInstanceCount(this.instanceCount);
    }
    if (consumerProperties.getInstanceIndex() < 0) {
        consumerProperties.setInstanceIndex(this.instanceIndex);
    }
    return consumerProperties;
}
Also used : ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties)

Aggregations

ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)7 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)3 Test (org.junit.Test)2 Binder (org.springframework.cloud.stream.binder.Binder)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Binding (org.springframework.cloud.stream.binder.Binding)1 DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)1 DefaultPollableMessageSource (org.springframework.cloud.stream.binder.DefaultPollableMessageSource)1 ExtendedConsumerProperties (org.springframework.cloud.stream.binder.ExtendedConsumerProperties)1 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)1 PollableConsumerBinder (org.springframework.cloud.stream.binder.PollableConsumerBinder)1 PollableSource (org.springframework.cloud.stream.binder.PollableSource)1 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)1 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)1 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 MessageChannel (org.springframework.messaging.MessageChannel)1