The magic really happens here in the ExecutorProxy task. First note that we are defining it as follows:
@Singleton
We are making sure to have one and only one instance of it in the context.
Now note that even though we are creating our own executor, we are still relying on the application server context for it:
@Resource(name = "LocalManagedThreadFactory") private ManagedThreadFactory factory; @Resource(name = "LocalContextService") private ContextService context;
This guarantees that you don't violate any context rules and ruin your application for good.
Then we create a pool for executing threads:
private ExecutorService executor; @PostConstruct public void init(){ executor = new ThreadPoolExecutor(1, 5, 10, TimeUnit.SECONDS, ...