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:
  • Method Details

    • 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
    • start

      Return the behavior for MessageHandler.Context.processed() calls.

      This method is called for each chunk of messages. Implementations return a callback that will be called when applications consider a message dealt with and call MessageHandler.Context.processed(). The callback can count messages and provide credits accordingly.

      Parameters:
      context - chunk context
      Returns:
      the message processed callback
    • creditOnChunkArrival

      static ConsumerFlowStrategy creditOnChunkArrival()
      Strategy that provides 1 initial credit and a credit on each new chunk.

      Calls to MessageHandler.Context.processed() are ignored.

      Returns:
      flow strategy
    • creditOnChunkArrival

      static ConsumerFlowStrategy creditOnChunkArrival(int initialCredits)
      Strategy that provides the specified number of initial credits and a credit on each new chunk.

      Calls to MessageHandler.Context.processed() are ignored.

      Parameters:
      initialCredits - number of initial credits
      Returns:
      flow strategy
      See Also:
    • 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:
    • 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