管理対象 executor の構成

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

このタスクについて

重要: Liberty では、管理対象 executor に独自のスレッド・プールはありません。管理対象 executor インスタンスにサブミットされたタスクは、共通の Liberty executor スレッド・プールで実行されます。
ManagedExecutorService は、以下に示すように <concurrent-1.0> フィーチャーの下にあり、server.xml ファイル内で使用可能にします。
<featureManager>
	<feature>concurrent-1.0</feature>
</featureManager>

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

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

[17.0.0.4 and later]並行性ポリシーは、並行性に関する動作と、管理対象 executor に適用される制約 (最大並行性や最大キュー・サイズなど) を構成します。 デフォルトでは、管理対象 executor は、concurrencyPolicy 構成エレメントのデフォルト・インスタンスである、制約が無制限の defaultConcurrencyPolicy を使用します。管理対象 executor を構成するときに、ネストされたエレメントとして特定の concurrencyPolicy エレメントを参照することも直接構成することもしないと、このデフォルト並行性ポリシーが使用されます。複数の管理対象 executor または他の構成エレメントが同じ concurrencyPolicy エレメントを参照している場合、 そのポリシー内の制約は、それらの管理対象 executor インスタンスおよび構成された他のリソースのすべてに適用されます。LONGRUNNING_HINT 実行プロパティーが true に設定されたタスクに適用される、長時間実行タスク用の並行性ポリシーを指定して、管理対象 executor を構成することもできます。concurrencyPolicy エレメントおよび長時間実行 concurrencyPolicy エレメントに指定された構成は、できるだけ早く実行するようにサブミットされるタスクに適用されます。 この構成は、スケジュールされたタスクには適用されません。

手順

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));

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

ファイル名: twlp_config_managedexecutor.html