Gateway Thread Pools & Task Management (Which One)

Hi All,

I see there are ~3 gatewayContext objects that can essentially schedule/execute tasks like a thread pool. I wanted to know if there is an intended use for each of these and which I should be using in my context?

    gatewayContext.getExecutionManager() or gatewayContext.createExecutionManager()
    gatewayContext.getScheduledExecutorService();
    gatewayContext.getExecutorService();

I have a few different services running that I will need to run tasks periodically (somewhere between 1-60s). Some of these might be very CPU intensive on larger systems. In the past I would just make my own executionManager with the gateway, but is that still the best practice?

There's some tradeoffs to each.

The ExecutionManager gives you some visibility at the gateway level about what's running (right? am I imagining this?), but it's based on a fixed-size thread pool, which can sometimes be an issue when there's a lot of tasks submitted.

I prefer the ExecutorService/ScheduledExecutorService combo, but the trick here is that you need to make sure never to actually perform the blocking work on the ScheduledExecutorService thread. The pattern should look something like:

getScheduledExecutor().schedule(
    () -> getExecutor().execute(() -> ...),
    time,
    TimeUnit.MILLISECONDS
);
3 Likes

Yeah, I think we can categorize the execution manager and it'll give some good diagnostic info. Don't know if that works for the other systems?

Right that's good info because I thought the scheduledExecutor would execute whatever you submitted asynchronously.

The scheduled executor service is asynchronous, in that it doesn't block the thread you called it from, but it still has to do that work somewhere, and the current implementation is a fixed-size thread pool sized to the number of processors available to the JVM. The 'regular' executor pool is a basic cached thread pool of unbound size. getExecutionManager() is a fixed size pool configurable by system prop, defaulting to an arbitrary 12 threads.

1 Like