I'm using Managed Executor Service to implement a service that processes background tasks. The service should be allowed to stop a running task and terminate the worker thread and return it to the threads pool for next task. Once it has been stopped, can you resume where you left off?
No, it is not possible to shutdown, terminate, or resume a ManagedExecutorService in a Java EE environment.
Per the Java EE Concurrency Utilities 1.0 spec, bullet #2
3.1.6.1 Java EE Product Provider Requirements
This subsection describes additional requirements for ManagedExecutorService providers.
- All tasks, when executed from the ManagedExecutorService, will run with the Java EE component identity of the component that submitted the task.
- The lifecycle of a ManagedExecutorService is managed by an application server. All lifecycle operations on the ManagedExecutorService interface will throw a java.lang.IllegalStateException exception. This includes the following methods that are defined in the java.util.concurrent.ExecutorService interface: awaitTermination(), isShutdown(), isTerminated(), shutdown(), and shutdownNow().
- No task submitted to an executor can run if task’s component is not started.
When a ManagedExecutorService instance is being shutdown by the Java EE Product Provider:
- All attempts to submit new tasks are rejected.
- All submitted tasks are cancelled if not running.
- All running task threads are interrupted.
- All registered ManagedTaskListeners are invoked.
This may seem like a limitation, but the reason why this restriction is in place is because the ManagedExeuctorService is managed by the Java EE Product Provider for you.
So don't worry about starting, stopping, or resuming your ManagedExeuctorService in Java EE.
Now, if you want to wait until all of your tasks are complete, that's a perfectly reasonable requirement. This can be achieved a variety of ways:
ExecutorService.invokeAll(java.util.Collection) and track the List> that you get back.ExecutorService.submit(Callable) or ExecutorService.submit(Runnable)Once your tasks are submitted, you will need to manage a collection of Future<T>'s in any way you choose, and use get() (blocking) or isDone() (nonblocking) to check if they are done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With