Class PartitionHandler

java.lang.Object
org.springframework.cloud.stream.binder.PartitionHandler

public class PartitionHandler extends Object
Utility class to determine if a binding is configured for partitioning (based on the binder properties provided in the constructor) and what partition a message should be delivered to.
Author:
Patrick Peralta, David Turanski, Gary Russell, Ilayaperumal Gopinathan, Mark Fisher, Marius Bogoevici, Oleg Zhurakousky
  • Constructor Details

    • PartitionHandler

      @Deprecated public PartitionHandler(org.springframework.expression.EvaluationContext evaluationContext, ProducerProperties properties, PartitionKeyExtractorStrategy partitionKeyExtractorStrategy, PartitionSelectorStrategy partitionSelectorStrategy)
      Deprecated.
      since 3.0.2. Please use another constructor which allows you to pass an instance of beanFactory
      Construct a PartitionHandler.
      Parameters:
      evaluationContext - evaluation context for binder
      properties - binder properties
      partitionKeyExtractorStrategy - PartitionKeyExtractor strategy
      partitionSelectorStrategy - PartitionSelector strategy
    • PartitionHandler

      public PartitionHandler(org.springframework.expression.EvaluationContext evaluationContext, ProducerProperties properties, org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
      Construct a PartitionHandler.
      Parameters:
      evaluationContext - evaluation context for binder
      properties - binder properties
      beanFactory - instance of ConfigurableListableBeanFactory
      Since:
      3.0.2
  • Method Details

    • setPartitionCount

      public void setPartitionCount(int partitionCount)
      Set the actual partition count (if different to the configured count).
      Parameters:
      partitionCount - the count.
    • determinePartition

      public int determinePartition(org.springframework.messaging.Message<?> message)
      Determine the partition to which to send this message.

      If a partition key extractor class is provided, it is invoked to determine the key. Otherwise, the partition key expression is evaluated to obtain the key value.

      If a partition selector class is provided, it will be invoked to determine the partition. Otherwise, if the partition expression is not null, it is evaluated against the key and is expected to return an integer to which the modulo function will be applied, using the partitionCount as the divisor. If no partition expression is provided, the key will be passed to the binder partition strategy along with the partitionCount. The default partition strategy uses key.hashCode(), and the result will be the mod of that value.

      Parameters:
      message - the message.
      Returns:
      the partition