Hi,
I want to make an ExecutorService that maintains a pool of threads that poll from a shared BlockingQueue.
My original approach was to implement a class that implements Managed class (let's call the class MyClass).
MyClass has a reference to ExecutorService myExecutorService, and a reference to AtomicBoolean isStopRequested, which all Threads that have been submitted to myExecutorService has a reference to.
run() method of each Thread looks like below
public void run() {
while(!isStopRequested.get()) {
/// run some code
}
}
stop and stop method of MyClass (that implements Managed) looks as below
@Override
public void start() throws Exception {
isStopRequested.set(false);
for (int i = 0; i < threadPoolSize; i++) {
final MyThread myThread = MyThread(isStopRequested, sharedBlockingQueue... );
myExecutorService.execute(myThread);
}
}
@Override
public void stop() throws Exception {
isStopRequested.set(true);
waitForSharedQueueToDrain(); // a method that waits until a shared BlockingQueue gets drained
executorService.shutdown();
executorService.awaitTermination(shutdownTimeout, TimeUnit.SECONDS);
}
I realize that DropWizard provides an ExecutorService that is already managed by Dropwizard. ( environment.lifecycle().executorService()....).
It would be nice to use the executorService provided by Dropwizard, but I started to have difficulty to gracefully shutdown my pool of threads if I used the provided ExecutorService.
The biggest problem was how to tell each Thread to continue polling.
Has anybody been able to use an ExecutorService provided by Dropwizard that maintains a pool of threads that poll from a shared BlockingQueue?
Thank you! :-)