管理対象スケジュール executor の構成
ManagedScheduledExecutorService インスタンスを構成して、 非同期タスクがそのスケジュール元のスレッドのスレッド・コンテキストで実行されるようにスケジュールすることができます。 Java™ EE アプリケーションでのベスト・プラクティスは、アプリケーション独自のスレッドを直接管理するのを避けることです。 そのため、ManagedScheduledExecutorService は、JSE ExecutorService を拡張して、アプリケーション・サーバー環境内で非同期タスクをスケジュールするための方法を提供しています。 また、Java EE アプリケーションに関連するスレッド・コンテキストをキャプチャーして、 スケジュール・タスクのスレッドに伝搬するよう ManagedScheduledExecutorService を構成することもできます。
このタスクについて
<featureManager>
<feature>concurrent-1.0</feature>
</featureManager>
スレッド・コンテキストのキャプチャーと伝搬は、コンテキスト・サービスによって管理されます。 コンテキスト・サービスのデフォルト・インスタンス (DefaultContextService) はサーバーによって作成され、少なくとも classloaderContext、jeeMetadataContext および securityContext を伝搬するよう構成されます。特定のコンテキスト・サービス・インスタンスを参照したり、内部で直接コンテキスト・サービス・インスタンスを構成したりせずに ManagedScheduledExecutorService が作成されると、このデフォルトのコンテキスト・サービス・インスタンスが使用されます。 コンテキスト・サービス・インスタンスについて詳しくは、スレッド・コンテキスト・サービス・インスタンスの構成のトピックを参照してください。
デフォルトの管理対象スケジュール executor インスタンス (DefaultManagedScheduledExecutorService) は、java:comp/DefaultManagedScheduledExecutorService として使用でき、スレッド・コンテキストのキャプチャーおよび伝搬にデフォルトのコンテキスト・サービス・インスタンスを使用します。
手順
server.xml ファイルの構成例:
例
管理対象スケジュール executor を (@Resource を使用して) アプリケーション・コンポーネントに注入したり、リソース環境参照 (resource-env-ref) で検索します。 インスタンスの取得方法に関係なく、 javax.enterprise.concurrent.ManagedScheduledExecutorService として、 またはスーパークラス java.util.concurrent.ScheduledExecutorSerivce、 java.util.concurrent.ExecutorService、javax.enterprise.concurrent.ManagedExecutorService のいずれかとして、区別なく使用できます。
- デフォルト管理対象スケジュール executor を検索する例:
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) new InitialContext().lookup( "java:comp/DefaultManagedScheduledExecutorService"); executor.schedule(beginSalePrices, 12, TimeUnit.HOURS); executor.schedule(restoreNormalPrices, 60, TimeUnit.HOURS);
- @Resource を使用して、java.util.concurrent.ScheduledExecutorService として注入する場合の例を示します。
@Resource(lookup="concurrent/scheduledExecutor2") ScheduledExecutorService executor; ... // schedule a task to run every half hour from now Runnable updateSalesReport = new Runnable() { public void run() throws Exception { // java:comp lookup is possible because <jeeMetadataContext> is configured DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/ds1"); ... query and update various database tables } }; ScheduledFuture<?> future = executor.scheduleAtFixedRate(updateSalesReport, 0, 30, TimeUnit.MINUTES);
- @Resource を使用して、javax.enterprise.concurrent.ManagedScheduledExecutorService として注入する場合の例を示します。
@Resource(lookup="concurrent/scheduledExecutor2") ManagedScheduledExecutorService executor; ... usage is same as previous example
- web.xml ファイル内の java.util.concurrent.ScheduledExecutorService 用 <resource-env-ref> の例を示します。
<resource-env-ref> <resource-env-ref-name>concurrent/scheduledExecutor1</resource-env-ref-name> <resource-env-ref-type>java.util.concurrent.ScheduledExecutorService</resource-env-ref-type> </resource-env-ref>
- web.xml ファイル内の javax.enterprise.concurrent.ManagedScheduledExecutorService 用 <resource-env-ref> の例を示します。
<resource-env-ref> <resource-env-ref-name>concurrent/scheduledExecutor2</resource-env-ref-name> <resource-env-ref-type>javax.enterprise.concurrent.ManagedScheduledExecutorService</resource-env-ref-type> </resource-env-ref>
- Example lookup that uses a resource environment reference:
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) new InitialContext().lookup("java:comp/env/concurrent/scheduledExecutor2"); executor.schedule(payrollTask, fridaysAtMidnightTrigger);