配置受管调度执行程序
可配置 ManagedScheduledExecutorService 实例来安排异步任务以与从中安排该任务的线程的线程上下文一起运行。Java™ EE 应用程序最好避免直接管理它们自己的线程;因此,ManagedScheduledExecutorService 扩展 JSE ExecutorService 来提供一种方法以在应用程序服务器环境内安排异步任务。您还可配置 ManagedScheduledExecutorService 以捕获与 Java EE 应用程序相关的线程上下文并将其传播至计划任务的线程。
关于此任务
<featureManager>
<feature>concurrent-1.0</feature>
</featureManager>
线程上下文捕获和传播由上下文服务管理。服务器会创建上下文服务的缺省实例 (DefaultContextService),该实例配置为至少传播 classloaderContext、jeeMetadataContext 和 securityContext。如果已创建 ManagedScheduledExecutorService 但未引用特定上下文服务实例或直接在其中配置上下文服务实例,那么会使用此缺省上下文服务实例。有关上下文服务实例的更多信息,请参阅“配置线程上下文服务实例”主题。
缺省受管调度执行程序实例 (DefaultManagedScheduledExecutorService) 以 java:comp/DefaultManagedScheduledExecutorService 形式提供,并使用缺省上下文服务实例来捕获和传播线程上下文。
并行策略用于配置要应用于受管调度执行程序的并行相关行为及约束,例如,最大并行度和最大队列大小。缺省情况下,受管调度执行程序使用
concurrencyPolicy 配置元素的缺省实例
defaultConcurrencyPolicy,该实例具有无限制的约束。如果您配置受管调度执行程序,但没有以嵌套元素形式引用或直接配置特定的
concurrencyPolicy 元素,那么将使用这个缺省并行策略。如果多个受管调度执行程序或其他配置元素引用同一个 concurrencyPolicy 元素,那么该策略中的约束将应用于所有这些受管调度执行程序实例和其他已配置的资源。另外,还可以为受管调度执行程序配置一个用于长时间运行的任务的并行策略,该策略将应用于 LONGRUNNING_HINT 执行属性设置为 true 的任务。在
concurrencyPolicy 元素以及长时间运行 concurrencyPolicy
元素中指定的配置将应用于提交为尽快运行的任务。此配置不适用于调度的任务。
过程
server.xml 文件中的示例配置:
示例
将受管调度执行程序注入至应用程序组件(通过使用 @Resource)或使用资源环境引用 (resource-env-ref) 进行查找。无论如何获取实例,都可按可互换方式将其用作 javax.enterprise.concurrent.ManagedScheduledExecutorService 或下列任何超类:java.util.concurrent.ScheduledExecutorSerivce、java.util.concurrent.ExecutorService 或 javax.enterprise.concurrent.ManagedExecutorService
- 查找缺省受管调度执行程序的示例:
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>
- 使用资源环境引用的查找示例:
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) new InitialContext().lookup("java:comp/env/concurrent/scheduledExecutor2"); executor.schedule(payrollTask, fridaysAtMidnightTrigger);