Interface ConsumerFlowStrategy

  • All Known Implementing Classes:
    ConsumerFlowStrategy.CreditOnChunkArrivalConsumerFlowStrategy, ConsumerFlowStrategy.MessageCountConsumerFlowStrategy

    public interface ConsumerFlowStrategy
    Contract to determine when a subscription provides credits to get more messages.

    The broker delivers "chunks" of messages to consumers. A chunk can contain from 1 to several thousands of messages. The broker send chunks as long as the subscription has credits. A client connection can provide credits for a given subscription and the broker will send the corresponding number of chunks (1 credit = 1 chunk).

    This credit mechanism avoids overwhelming a consumer with messages. A consumer does not want to provide a credit only when it is done with messages of a chunk, because it will be idle between its credit request and the arrival of the next chunk. The idea is to keep consumers busy as much as possible, without accumulating an in-memory backlog on the client side. There is no ideal solution, it depends on the use cases and several parameters (processing time, network, etc).

    This is an experimental API, subject to change.

    Since:
    0.12.0
    See Also:
    MessageHandler.Context.processed(), ConsumerBuilder.flow()
    • Method Detail

      • initialCredits

        int initialCredits()
        The initial number of credits for a subscription.

        It must be greater than 0. Values are usually between 1 and 10.

        Returns:
        initial number of credits
      • creditWhenHalfMessagesProcessed

        static ConsumerFlowStrategy creditWhenHalfMessagesProcessed()
        Strategy that provides 10 initial credits and a credit when half of the chunk messages are processed.

        Make sure to call MessageHandler.Context.processed() on every message when using this strategy, otherwise the broker may stop sending messages to the consumer.

        Returns:
        flow strategy
      • creditWhenHalfMessagesProcessed

        static ConsumerFlowStrategy creditWhenHalfMessagesProcessed​(int initialCredits)
        Strategy that provides the specified number of initial credits and a credit when half of the chunk messages are processed.

        Make sure to call MessageHandler.Context.processed() on every message when using this strategy, otherwise the broker may stop sending messages to the consumer.

        Parameters:
        initialCredits - number of initial credits
        Returns:
        flow strategy
        See Also:
        ConsumerBuilder.FlowConfiguration.initialCredits(int)
      • creditOnProcessedMessageCount

        static ConsumerFlowStrategy creditOnProcessedMessageCount​(int initialCredits,
                                                                  double ratio)
        Strategy that provides the specified number of initial credits and a credit when the specified ratio of the chunk messages are processed.

        Make sure to call MessageHandler.Context.processed() on every message when using this strategy, otherwise the broker may stop sending messages to the consumer.

        Parameters:
        initialCredits - number of initial credits
        Returns:
        flow strategy