在会话 Bean 的方法中委托 "task"(Ex:Runnable)

Delegate a "task"(Ex:Runnable) inside a method of a Session Bean

我有一个无状态 EJB 方法(ejbMethod1)需要调用 Web 服务操作(wsOper1),如果此调用失败它需要调用另一个网络服务操作(wsOper2).

我的问题是:我不需要从 ejbMethod1[=42= wsOper2 到 return 的 returning ]!我想将这项工作委托给我的应用程序的另一部分,并立即 return。

Option1::我解决这个问题的第一个方法是使用全局 LinkedBlockingQueue 来存储表示 wsOper2 我需要做,让 N 个线程从这个队列中检索元素并执行调用。

Option2:: 创建一个 Runnable 并委托给一些全局线程池。有没有我可以用的?

或者我没有考虑的另一种选择?

... PS: 我正在使用 Glassfish 4.1 和 JRE8,我是 Java EE 世界的新手 :P


更新:

我使用 ManagedExecutorService 作为 Runnable 异步执行 wsOper2

@Resource
private ManagedExecutorService executor;

谢谢瑞安 :)

来自 EJB 规范:

The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread, or to change a thread’s priority or name. The enterprise bean must not attempt to manage thread groups.

有关 EJB 和线程的详细信息,请参阅 Adam's Blog