Вопрос

Our system processes messages delivered from a messaging system. If no message is received after 10 seconds, an error should be raised (inactivity timeout).

I was thinking of using a ScheduledExecutorService (with 1 Thread). Each time a message is received, I cancel the previous timeout task and submit a new one:

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Callable timeoutTask = new Callable() {...};

...

synchronized {
    timeout.cancel();
    timeout = executor.schedule( timeoutTask, 10, TimeUnit.SECONDS);
}

In normal case, we process ~ 1000 / sec. Would this approach scale?

Это было полезно?

Решение

If you share the threadpool and keep the running time of timeoutTask low, it'd be most probably fine. If you'd have one threadpool per ~ 1000 / sec tasks then that would not work.

If you are still worried, you can have a look at HashedWheelTimer from the Netty project (link). This thing is very efficient in scheduling timeouts. Note that you MUST share instance of HashedWheelTimer as well.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top