@@ -118,6 +118,9 @@ static ConsumerFlowStrategy creditOnChunkArrival() {
118118 *
119119 * <p>Calls to {@link MessageHandler.Context#processed()} are ignored.
120120 *
121+ * <p>Consider using {@link #creditEveryNthChunk(int, int)} instead as it generates less network
122+ * traffic.
123+ *
121124 * @param initialCredits number of initial credits
122125 * @return flow strategy
123126 * @see com.rabbitmq.stream.ConsumerBuilder.FlowConfiguration#initialCredits(int)
@@ -168,12 +171,79 @@ static ConsumerFlowStrategy creditOnProcessedMessageCount(int initialCredits, do
168171 return new MessageCountConsumerFlowStrategy (initialCredits , ratio );
169172 }
170173
174+ /**
175+ * Strategy that provides the specified number of initial credits and <code>n</code> credits every
176+ * <code>n</code> chunks.
177+ *
178+ * <p>This strategy generates less network traffic than {@link
179+ * com.rabbitmq.stream.ConsumerFlowStrategy.CreditOnChunkArrivalConsumerFlowStrategy} and should
180+ * be used instead, unless <code>n</code> is equal to 1.
181+ *
182+ * <p>A rule of thumb is to set <code>n</code> to half the value of initial credits.
183+ *
184+ * <p>Calls to {@link MessageHandler.Context#processed()} are ignored.
185+ *
186+ * @param initialCredits number of initial credits
187+ * @param n number of chunks and number of credits
188+ * @return flow strategy
189+ */
190+ static ConsumerFlowStrategy creditEveryNthChunk (int initialCredits , int n ) {
191+ return new CreditEveryNthChunkConsumerFlowStrategy (initialCredits , n );
192+ }
193+
194+ /**
195+ * Strategy that provides the specified number of initial credits and <code>n</code> credits every
196+ * <code>n</code> chunks.
197+ *
198+ * <p>This strategy generates less network traffic than {@link
199+ * com.rabbitmq.stream.ConsumerFlowStrategy.CreditOnChunkArrivalConsumerFlowStrategy} and should
200+ * be used instead, unless <code>n</code> is equal to 1.
201+ *
202+ * <p>Calls to {@link MessageHandler.Context#processed()} are ignored.
203+ */
204+ final class CreditEveryNthChunkConsumerFlowStrategy implements ConsumerFlowStrategy {
205+
206+ private static final MessageProcessedCallback CALLBACK = v -> {};
207+
208+ private final int initialCredits ;
209+ private final AtomicLong chunkCount = new AtomicLong (0 );
210+ private final int n ;
211+
212+ private CreditEveryNthChunkConsumerFlowStrategy (int initialCredits , int n ) {
213+ if (n <= 0 ) {
214+ throw new IllegalArgumentException ("The n argument must be greater than 0" );
215+ }
216+ if (initialCredits <= n ) {
217+ throw new IllegalArgumentException (
218+ "The number of initial credits must be greater than the limit" );
219+ }
220+ this .initialCredits = initialCredits ;
221+ this .n = n ;
222+ }
223+
224+ @ Override
225+ public int initialCredits () {
226+ this .chunkCount .set (0 );
227+ return this .initialCredits ;
228+ }
229+
230+ @ Override
231+ public MessageProcessedCallback start (Context context ) {
232+ if (chunkCount .incrementAndGet () % n == 0 ) {
233+ context .credits (n );
234+ }
235+ return CALLBACK ;
236+ }
237+ }
238+
171239 /**
172240 * Strategy that provides the specified number of initial credits and a credit on each new chunk.
173241 *
174242 * <p>Calls to {@link MessageHandler.Context#processed()} are ignored.
175243 */
176- class CreditOnChunkArrivalConsumerFlowStrategy implements ConsumerFlowStrategy {
244+ final class CreditOnChunkArrivalConsumerFlowStrategy implements ConsumerFlowStrategy {
245+
246+ private static final MessageProcessedCallback CALLBACK = v -> {};
177247
178248 private final int initialCredits ;
179249
@@ -189,7 +259,7 @@ public int initialCredits() {
189259 @ Override
190260 public MessageProcessedCallback start (Context context ) {
191261 context .credits (1 );
192- return value -> {} ;
262+ return CALLBACK ;
193263 }
194264 }
195265
@@ -200,7 +270,7 @@ public MessageProcessedCallback start(Context context) {
200270 * <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using
201271 * this strategy, otherwise the broker may stop sending messages to the consumer.
202272 */
203- class MessageCountConsumerFlowStrategy implements ConsumerFlowStrategy {
273+ final class MessageCountConsumerFlowStrategy implements ConsumerFlowStrategy {
204274
205275 private final int initialCredits ;
206276 private final double ratio ;
0 commit comments