Monitoring wrapped ThreadPoolExecutor returned from Executors

Tommy Ludwig tludwig at
Wed Jan 6 03:11:05 UTC 2021


In the Micrometer project, we provide metrics instrumentation of `ExectorService`s. For `ThreadPoolExecutor`s, we track the number of completed tasks, active tasks, thread pool sizes, task queue size and remaining capacity via methods from `ThreadPoolExecutor`. We are currently using a brittle reflection hack[1] to do this for the wrapped `ThreadPoolExecutor` returned from `Executors` methods `newSingleThreadExecutor` and `newSingleThreadScheduledExecutor`. With the introduction of JEP-396 in JDK 16, our reflection hack throws an InaccessibleObjectException by default.

I am not seeing a proper way to get at the methods we use for the metrics (e.g. `ThreadPoolExecutor::getCompletedTaskCount`) in this case. Is there a way that I am missing?

>From the JavaDocs, the intention of the wrapping is to prevent changing the ThreadPool configuration. Our use case does not call for changing the thread pool configuration - we want to observe the ExecutorService (e.g. thread pool state) via getter methods for monitoring purposes.



More information about the core-libs-dev mailing list