Search in sources :

Example 1 with PartitionSelectorStrategy

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

the class CustomPartitionedProducerTest method testCustomPartitionedProducer.

@Test
public void testCustomPartitionedProducer() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorClass=org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass", "--spring.cloud.stream.bindings.output.producer.partitionSelectorClass=org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Example 2 with PartitionSelectorStrategy

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

the class CustomPartitionedProducerTest method testCustomPartitionedProducerMultipleInstances.

public void testCustomPartitionedProducerMultipleInstances() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSourceMultipleStrategies.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractorOne", "--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelectorTwo");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext)

Example 3 with PartitionSelectorStrategy

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

the class MessageConverterConfigurer method getPartitionSelectorStrategy.

@SuppressWarnings("deprecation")
private PartitionSelectorStrategy getPartitionSelectorStrategy(ProducerProperties producerProperties) {
    PartitionSelectorStrategy partitionSelector;
    if (producerProperties.getPartitionSelectorClass() != null) {
        logger.warn("'partitionSelectorClass' option is deprecated as of v2.0. Please configure partition " + "selector as a @Bean that implements 'PartitionSelectorStrategy'. Additionally you can " + "specify 'spring.cloud.stream.bindings.output.producer.partitionSelectorName' to specify which " + "bean to use in the event there are more then one.");
        partitionSelector = instantiate(producerProperties.getPartitionSelectorClass(), PartitionSelectorStrategy.class);
    } else if (StringUtils.hasText(producerProperties.getPartitionSelectorName())) {
        partitionSelector = this.beanFactory.getBean(producerProperties.getPartitionSelectorName(), PartitionSelectorStrategy.class);
        Assert.notNull(partitionSelector, "PartitionSelectorStrategy bean with the name '" + producerProperties.getPartitionSelectorName() + "' can not be found. Has it been configured (e.g., @Bean)?");
    } else {
        Map<String, PartitionSelectorStrategy> selectors = this.beanFactory.getBeansOfType(PartitionSelectorStrategy.class);
        Assert.isTrue(selectors.size() <= 1, "Multiple  beans of type 'PartitionSelectorStrategy' found. " + selectors + ". Please " + "use 'spring.cloud.stream.bindings.output.producer.partitionSelectorName' property to specify " + "the name of the bean to be used.");
        partitionSelector = CollectionUtils.isEmpty(selectors) ? new DefaultPartitionSelector() : selectors.values().iterator().next();
    }
    return partitionSelector;
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) Map(java.util.Map)

Example 4 with PartitionSelectorStrategy

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

the class CustomPartitionedProducerTest method testCustomPartitionedProducerByName.

@Test
public void testCustomPartitionedProducerByName() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractor", "--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelector");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Example 5 with PartitionSelectorStrategy

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

the class CustomPartitionedProducerTest method testCustomPartitionedProducerAsSingletons.

@Test
public void testCustomPartitionedProducerAsSingletons() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Aggregations

PartitionSelectorStrategy (org.springframework.cloud.stream.binder.PartitionSelectorStrategy)5 Field (java.lang.reflect.Field)4 PartitionHandler (org.springframework.cloud.stream.binder.PartitionHandler)4 PartitionKeyExtractorStrategy (org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy)4 Source (org.springframework.cloud.stream.messaging.Source)4 CustomPartitionKeyExtractorClass (org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass)4 CustomPartitionSelectorClass (org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass)4 ApplicationContext (org.springframework.context.ApplicationContext)4 PropertySource (org.springframework.context.annotation.PropertySource)4 DirectChannel (org.springframework.integration.channel.DirectChannel)4 MessageSource (org.springframework.integration.core.MessageSource)4 ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)4 Test (org.junit.Test)3 Map (java.util.Map)1