管理対象 executor の構成

指定されたスレッド・コンテキストで非同期タスクを実行するように、ManagedExecutorService インスタンスを構成できます。 ベスト・プラクティスは、 Java™ EE アプリケーションが独自のスレッドを直接管理しないようにすることです。したがって、 ManagedExecutorServiceJSE 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 として使用でき、スレッド・コンテキストのキャプチャーおよび伝搬にデフォルトのコンテキスト・サービス・インスタンスを使用します。

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

手順

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

  • concurrent/execSvcという名前で JNDI に登録され、デフォルトのコンテキスト・サービス・インスタンスを使用する管理対象 executor サービス・インスタンス:
    <managedExecutorService jndiName="concurrent/execSvc"/>
  • 最大 5 つの並行スレッドと、 jeeMetadataContext コンテキストのみを伝搬するように構成されたコンテキスト・サービスを持つ管理対象 executor サービス・インスタンス:
    <managedExecutorService jndiName="concurrent/execSvc1">
    	<contextService>
    		<jeeMetadataContext/>
    	</contextService>
           <concurrencyPolicy max="5"/>
    </managedExecutorService>
  • classloaderContext および securityContextを伝搬するように構成されたコンテキスト・サービスを持つ管理対象 executor サービス・インスタンス:
    <managedExecutorService jndiName="concurrent/execSvc2">
    	<contextService>
    		<classloaderContext/>
    		<securityContext/>
    	</contextService>
    </managedExecutorService>
  • jeeMetadataContext コンテキストのみを伝搬するように構成されたスレッド・コンテキスト・サービス、および最大 4 つの並行スレッドとキュー・サイズ 20 を指定する並行性ポリシー。これらは両方とも複数の管理対象 executor サービス・インスタンスによって共有されます。 concurrent/execSvc4 管理対象 executor には、追加の concurrencyPolicy 構成エレメントがあります。このエレメントは、長期実行タスクに対して最大 2 つの並行スレッドを指定します。
    <contextService id="contextSvc1">
    	<jeeMetadataContext/>
    </contextService>
    
    <concurrencyPolicy id="normal" max="4" maxQueueSize="20"/>
    
    <concurrencyPolicy id="longRunning" max="2"/>
    
    <managedExecutorService 
        jndiName="concurrent/execSvc3" 
        contextServiceRef="contextSvc1" 
        concurrencyPolicyRef="normal"/>
    
    <managedExecutorService 
        jndiName="concurrent/execSvc4" 
        contextServiceRef="contextSvc1" 
        concurrencyPolicyRef="normal" 
        longRunningPolicyRef="longRunning"/>
  • 前の例を継承し、 classloaderContext コンテキストの伝搬を追加し、管理対象 executor サービス・インスタンスによって使用されるスレッド・コンテキスト・サービス:
    <contextService id="contextSvc2" baseContextRef="contextSvc1">
    	<classloaderContext/>
    </contextService>
    
    <managedExecutorService jndiName="concurrent/execSvc5" contextServiceRef="contextSvc2"/>
  • zosWLMContext と、デフォルトのコンテキスト・サービス・インスタンスから継承されたスレッド・コンテキスト伝搬を含む管理対象 executor サービス・インスタンス:
    <managedExecutorService jndiName="concurrent/execSvc6">
    	<contextService baseContextRef="DefaultContextService">
    			<zosWLMContext defaultTransactionClass="TRAN1"/>
    	</contextService>
    </managedExecutorService>

管理対象 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));