配置受管理執行程式
您可以將 ManagedExecutorService 實例配置成以指定的執行緒環境定義來執行非同步作業。 Java™ EE 應用程式的最佳作法是避免直接管理它們自己的執行緒;因此,ManagedExecutorService 會延伸 JSE ExecutorService,藉以在應用程式伺服器環境內啟動非同步作業。您也可以將 ManagedExecutorService 配置成會將 Java EE 應用程式的各種相關的執行緒環境定義,傳播至非同步作業的執行緒。
關於這項作業
<featureManager>
<feature>concurrent-1.0</feature>
</featureManager>
將環境定義傳播到 ManagedExecutorService 所執行之作業的執行緒,是由環境定義服務來管理。 伺服器會建立環境定義服務 (DefaultContextService) 的預設實例,且該預設實例會配置成至少傳播 classloaderContext、jeeMetadataContext 和 securityContext。如果建立了 ManagedExecutorService,但沒有參照特定的環境定義服務實例,或沒有直接在其中配置環境定義服務實例,就會使用這個預設的環境定義服務實例。 如需環境定義服務實例的相關資訊,請參閱「配置執行緒環境定義服務實例」主題。
預設的受管理執行程式實例 (DefaultManagedExecutorService) 是以 java:comp/DefaultManagedExecutorService 來提供,且會將預設的環境定義服務實例用於執行緒環境定義擷取和傳播之中。
程序
server.xml 檔中的配置範例:
範例
受管理執行程式服務實例可以注入至應用程式元件(使用 @Resource),或利用資源環境參照 (resource-env-ref) 來查閱。 不論如何取得實例,您都可以將它與 javax.enterprise.concurrent.ManagedExecutorService 或其 java.util.concurrent.ExecutorSerivce 超類別替換使用。
- 查閱預設受管理執行程式的範例:
ManagedExecutorService executor = (ManagedExecutorService) new InitialContext().lookup( "java:comp/DefaultManagedExecutorService"); executor.submit(doSomethingInParallel);
- 利用 @Resource 來注入成為 java.util.concurrent.ExecutorService 的範例:
@Resource(lookup="concurrent/execSvc1") ExecutorService execSvc1; ... // 提交要執行的作業 Future<Integer> future1 = execSvc1.submit(new Callable<Integer>() { public Integer call() throws Exception { // 由於配置了 <jeeMetadataContext>,因此有可能進行 java:comp 查閱 DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/ds1"); ... make updates to the database return updateCount; } }); Future<Integer> future2 = execSvc1.submit(anotherTaskThatUpdatesADatabase); numUpdatesCompleted = future1.get() + future2.get();
- 利用 @Resource 來注入成為 javax.enterprise.concurrent.ManagedExecutorService 的範例:
@Resource(lookup="concurrent/execSvc1") ManagedExecutorService execSvc1; ... // 提交要執行的作業 Future<Integer> future1 = execSvc1.submit(new Callable<Integer>() { public Integer call() throws Exception { // 由於配置了 <jeeMetadataContext>,因此有可能進行 java:comp 查閱 DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/ds1"); ... make updates to the database return updateCount; } }); Future<Integer> future2 = execSvc1.submit(anotherTaskThatUpdatesADatabase); numUpdatesCompleted = future1.get() + future2.get();
- web.xml 檔中 java.util.concurrent.ExecutorService 的 <resource-env-ref> 範例:
<resource-env-ref> <resource-env-ref-name>concurrent/execSvc2</resource-env-ref-name> <resource-env-ref-type>java.util.concurrent.ExecutorService</resource-env-ref-type> </resource-env-ref>
- 使用資源環境參照的查閱範例:
ExecutorService execSvc2 = (ExecutorService) new InitialContext().lookup("java:comp/env/concurrent/execSvc2"); futures = execSvc2.invokeAll(Arrays.asList(task1, task2, task3));
- web.xml 檔中 javax.enterprise.concurrent.ManagedExecutorService 的 <resource-env-ref> 範例:
<resource-env-ref> <resource-env-ref-name>concurrent/execSvc2</resource-env-ref-name> <resource-env-ref-type>javax.enterprise.concurrent.ManagedExecutorService</resource-env-ref-type> </resource-env-ref>
- 使用資源環境參照並強制轉型成 ManagedExecutorService 的查閱範例:
ManagedExecutorService execSvc2 = (ManagedExecutorService) new InitialContext().lookup("java:comp/env/concurrent/execSvc2"); futures = execSvc2.invokeAll(Arrays.asList(task1, task2, task3));