ManagedExecutorService インスタンスの構成

指定されたスレッド・コンテキストで非同期タスクを実行するよう ManagedExecutorService インスタンスを構成できます。Java™ EE アプリケーションでのベスト・プラクティスは、アプリケーション独自のスレッドを直接管理するのを避けることです。そのため、ManagedExecutorService は、JSE ExecutorService を拡張して、アプリケーション・サーバー環境内で非同期タスクを起動するための方法を提供しています。また、Java EE アプリケーションに関連するさまざまなスレッド・コンテキストを非同期タスクのスレッドに伝搬するよう ManagedExecutorService を構成することもできます。

このタスクについて

ManagedExecutorService は、以下に示すように <concurrent-1.0> フィーチャーの下にあり、server.xml ファイル内で使用可能になります。
<featureManager>
	<feature>concurrent-1.0</feature>
</featureManager>

ManagedExecutorService により実行されるタスクのスレッドに対するコンテキストの伝搬は、コンテキスト・サービスによって管理されます。コンテキスト・サービスのデフォルト・インスタンス (DefaultContextService) はサーバーによって作成され、少なくとも classloaderContextjeeMetadataContext および securityContext を伝搬するよう構成されます。特定のコンテキスト・サービス・インスタンスを参照したり、内部で直接コンテキスト・サービス・インスタンスを構成したりせずに ManagedExecutorService が作成されると、このデフォルトのコンテキスト・サービス・インスタンスが使用されます。詳しくは、スレッド・コンテキスト・サービス・インスタンスの構成のトピックを参照してください。

デフォルトの管理対象 executor インスタンス (DefaultManagedExecutorService) は、java:comp/DefaultManagedExecutorService として使用でき、スレッド・コンテキストのキャプチャーおよび伝搬にデフォルトのコンテキスト・サービス・インスタンスを使用します。

重要: Alpha での現在の制限により、Future.get が呼び出されるまで、サブミットされたタスクが実行されない場合があります。

手順

server.xml ファイルの構成例:

管理対象 executor サービス・インスタンスは、(@Resource を使用して) アプリケーション・コンポーネントに注入したり、リソース環境参照 (resource-env-ref) で検索したりできます。 インスタンスの取得方法に関係なく、このインスタンスを javax.enterprise.concurrent.ManagedExecutorService またはその java.util.concurrent.ExecutorSerivce スーパークラスとして区別なく使用できます。

  • @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>
  • 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>
  • 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));
  • リソース環境参照を使用し、ManagedExecutorService にキャストするルックアップの例を示します。
    ManagedExecutorService execSvc2 = 
        (ManagedExecutorService) new InitialContext().lookup("java:comp/env/concurrent/execSvc2");
    
    futures = execSvc2.invokeAll(Arrays.asList(task1, task2, task3));

トピックのタイプを示すアイコン タスク・トピック

インフォメーション・センターに関するご使用条件 | フィードバック


タイム・スタンプ・アイコン 最終更新: 2015 年 6 月 17日
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-libcore-mp&topic=twlp_config_managedexecutor
ファイル名: twlp_config_managedexecutor.html