配置受管理排程執行程式
您可以配置 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);