---
title: "Batched Invocation Interceptors"
url: https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocation-interceptors
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Batched Invocation Interceptors

This page is written for C++, C#, Java, and Python.

Parts of this page are for some programming languages only, and name them. For one language's text alone, see [C++](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocation-interceptors/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocation-interceptors/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocation-interceptors/java.md), and [Python](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocation-interceptors/python.md).

A batch invocation interceptor lets you flush batched requests with your own policy, and handle a failed flush.

You install an interceptor by setting the `batchRequestInterceptor` field of the InitializationData ([C++](https://code.zeroc.com/ice/3.8/api/cpp/structIce_1_1InitializationData.html), [C#](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.InitializationData.html), [Java](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/InitializationData.html), and [Python](https://code.zeroc.com/ice/3.8/api/python/Ice.InitializationData.html)) object you use to create your communicator. The communicator invokes the interceptor for each batch request, passing the following arguments:

- `req`: an object representing the batch request being queued
- `count`: the number of requests already in the queue
- `size`: the number of bytes of the queued batch message, including its header

The request represented by `req` is not included in `count` and `size`, and is queued only when the interceptor calls `enqueue` on it.

The following interceptor flushes the queued requests when adding the new request would make the batch message larger than 64 KiB:

> **C++**
>
> ```cpp
> constexpr int maxBatchSize = 64 * 1024; // in bytes
> initData.batchRequestInterceptor =
>     [](const Ice::BatchRequest& req, int count, int size)
>     {
>         if (size + req.getSize() > maxBatchSize)
>         {
>             req.getProxy().ice_flushBatchRequestsAsync(
>                 [](std::exception_ptr ex)
>                 {
>                     // Log the failure.
>                 });
>         }
>         req.enqueue();
>     };
> ```

> **C#**
>
> ```csharp
> const int maxBatchSize = 64 * 1024; // in bytes
> initData.batchRequestInterceptor = (req, count, size) =>
> {
>     if (size + req.getSize() > maxBatchSize)
>     {
>         _ = FlushAsync(req.getProxy());
>     }
>     req.enqueue();
> };
>
> static async Task FlushAsync(Ice.ObjectPrx proxy)
> {
>     try
>     {
>         await proxy.ice_flushBatchRequestsAsync();
>     }
>     catch (Ice.LocalException exception)
>     {
>         Console.Error.WriteLine($"Failed to flush batch requests: {exception}");
>     }
> }
> ```

> **Java**
>
> ```java
> final int maxBatchSize = 64 * 1024; // in bytes
> initData.batchRequestInterceptor = (req, count, size) -> {
>     if (size + req.getSize() > maxBatchSize) {
>         req.getProxy().ice_flushBatchRequestsAsync().exceptionally(ex -> {
>             System.err.println("Failed to flush batch requests: " + ex);
>             return null;
>         });
>     }
>     req.enqueue();
> };
> ```

> **Python**
>
> ```py
> maxBatchSize = 64 * 1024  # in bytes
>
>
> async def logFlushFailure(flush: Awaitable[None]) -> None:
>     try:
>         await flush
>     except Ice.LocalException as exception:
>         print(f"Failed to flush batch requests: {exception}", file=sys.stderr)
>
>
> def interceptor(request: Ice.BatchRequest, count: int, size: int) -> None:
>     if size + request.getSize() > maxBatchSize:
>         asyncio.create_task(logFlushFailure(request.getProxy().ice_flushBatchRequestsAsync()))
>     request.enqueue()
>
>
> initData.batchRequestInterceptor = interceptor
> ```

Installing an interceptor does not turn off [automatic flushing](https://docs.zeroc.com/ice/3.8/runtime/invocation/invocation-mode/batched-invocations.md#automatic-flushing).
