管理対象 executor の構成
指定されたスレッド・コンテキストで非同期タスクを実行するように、ManagedExecutorService インスタンスを構成できます。Java™ EE アプリケーションでのベスト・プラクティスは、アプリケーション独自のスレッドを直接管理するのを避けることです。 そのため、ManagedExecutorService では、JSE ExecutorService を拡張して、アプリケーション・サーバー環境内で非同期タスクを開始するための方法を提供しています。 また、Java EE アプリケーションに関連するさまざまなスレッド・コンテキストを非同期タスクのスレッドに伝搬するように、ManagedExecutorService を構成することもできます。
このタスクについて
<featureManager>
<feature>concurrent-1.0</feature>
</featureManager>
ManagedExecutorService によって実行されるタスクのスレッドに対するコンテキストの伝搬は、コンテキスト・サービスによって管理されます。コンテキスト・サービスのデフォルト・インスタンス (DefaultContextService) はサーバーによって作成され、少なくとも classloaderContext、jeeMetadataContext および securityContext を伝搬するよう構成されます。特定のコンテキスト・サービス・インスタンスを参照せずに、あるいは内部で直接コンテキスト・サービス・インスタンスを構成せずに ManagedExecutorService が作成された場合は、このデフォルトのコンテキスト・サービス・インスタンスが使用されます。 コンテキスト・サービス・インスタンスについて詳しくは、スレッド・コンテキスト・サービス・インスタンスの構成のトピックを参照してください。
デフォルトの管理対象 executor インスタンス (DefaultManagedExecutorService) は、java:comp/DefaultManagedExecutorService として使用でき、スレッド・コンテキストのキャプチャーおよび伝搬にデフォルトのコンテキスト・サービス・インスタンスを使用します。
手順
server.xml ファイルの構成例:
例
管理対象 executor サービス・インスタンスは、(@Resource を使用して) アプリケーション・コンポーネントに注入したり、リソース環境参照 (resource-env-ref) で検索したりできます。 インスタンスの取得方法に関係なく、このインスタンスを javax.enterprise.concurrent.ManagedExecutorService またはその java.util.concurrent.ExecutorSerivce スーパークラスとして区別なく使用できます。
- デフォルト管理対象 executor を検索する例:
ManagedExecutorService executor = (ManagedExecutorService) new InitialContext().lookup( "java:comp/DefaultManagedExecutorService"); executor.submit(doSomethingInParallel);
- @Resource を使用して、java.util.concurrent.ExecutorService として注入する場合の例を示します。
@Resource(lookup="concurrent/execSvc1") ExecutorService execSvc1; ... // submit task to run Future<Integer> future1 = execSvc1.submit(new Callable<Integer>() { public Integer call() throws Exception { // java:comp lookup is possible because <jeeMetadataContext> is configured 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; ... // submit task to run Future<Integer> future1 = execSvc1.submit(new Callable<Integer>() { public Integer call() throws Exception { // java:comp lookup is possible because <jeeMetadataContext> is configured 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>
- Example lookup that uses a resource environment reference:
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));