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));
}
}
}
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));
}
}
}
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;
}
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));
}
}
}
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));
}
}
}
Aggregations