天天看點

在Jboss中使用Quartz

     Jboss EJB預設使用的定時服務是TimerService,TimerService的使用過程較為繁瑣,需要使用一個無狀态的serviceBean去實作scheduleTimer, timeoutHandler方法,并且要通過外部調用去啟動和控制定時器的頻率。在使用過程中覺得TimerService定時器非常不好用。是以希望能使用簡單好用Quartz來做為伺服器的定時器。

     如果使用Quartz定時器,在啟動定時服務前,需要初始化定時的内容,但在Jboss Ejb中,我實在是找不到能讓Quartz初始化的辦法,沒有web.xml檔案,也無法配置init方法。最初想到的辦法是讓TimerService去調用一次Quartz定時器的初始化方法,來啟動Quartz定時器。但這實在不是一個好的方法,。後來在一次無意中使用google搜尋ejb quartz時(使用baidu,真找不到這文章), 看到了曙光http://docs.jboss.org/ejb3/docs/tutorial/1.0.7/html/Quartz_scheduler_integration.html 原來在jboss的ejb3文檔中,已經清楚的說明整合Quartz的辦法。 下面簡單的解釋一下,EJB3中使用Quartz的方法  

1 import javax.ejb.ActivationConfigProperty;
 2 import javax.ejb.MessageDriven;
 3 import javax.inject.Inject;
 4 
 5 import org.apache.log4j.Logger;
 6 import org.jboss.annotation.ejb.ResourceAdapter;
 7 import org.quartz.Job;
 8 import org.quartz.JobExecutionContext;
 9 import org.quartz.JobExecutionException;
10 //@MessageDriven注釋指明這是一個消息驅動Bean,并使用@ActivationConfigProperty注釋配置消息的各種屬性
11 //@ResourceAdapter告訴EJB容器使用哪種ResourceAdapter
12 @MessageDriven(activationConfig =
13 {@ActivationConfigProperty(propertyName = "cronTrigger", propertyValue = "0/2 * * * * ?")})
14 @ResourceAdapter("quartz-ra.rar")
15 public class AnnotatedQuartzMDBBean implements Job
16 {
17      @Override
18     public void execute(JobExecutionContext arg0) throws JobExecutionException {
19       //定時器執行内容
20      }
21 }      

轉載于:https://www.cnblogs.com/pfxiong/p/4024213.html