2010-04-06 121 views
0

您好我正在使用Quartz調度程序來觸發需要執行一系列活動的cron。 我對相同的代碼如下:石英調度程序無法啓動cron作業

在我InitServlet類的init()方法,我定義我的TimerServer

public class InitServlet extends HttpServlet { 
     public void init(ServletConfig config) throws ServletException { 
     try { 
      System.out.println("Starting the CRON"); 
      //Set the DSO Handler CRON 
      TimerServer task = TimerServer.getInstance(); 
      task.setTask(); 
     } catch (Exception ex) { 
      System.out.println("Failed to start the cron"); 
      ex.printStackTrace(); 
     } 
    } 

在我TimerServer類,我有以下方法

public void setTask() { 
     try{    
      this.setSubscriptionDailyJob(); 
     } catch(SchedulerException ex) { 
      log.error("SchedulerException: "+ex.getMessage(), ex); 
     } 

private void setSubscriptionDailyJob() throws SchedulerException { 
     log.info("Step 1 "); 

     Scheduler scheduler = schedulerFactory.getScheduler(); 
     log.info("Step 2 "); 

     JobDetail subscriptionJob = new JobDetail("subscription", "subscriptiongroup",   SubscriptionDaily.class); 
log.info("Step 3 "); 
     // Initiate CronTrigger with its name and group name 
     CronTrigger subscriptionCronTrigger = new CronTrigger("subscriptionCronTrigger", "subscriptionTriggerGroup"); 

     try { 
      log.info("Subscription cron: "+Constants.SUBSCRIPTION_CRON); 
      // setup CronExpression 
      CronExpression cexp = new CronExpression(Constants.SUBSCRIPTION_CRON); 
      // Assign the CronExpression to CronTrigger 
      subscriptionCronTrigger.setCronExpression(cexp); 
     } catch (Exception ex) { 
      log.warn("Exception: "+ex.getMessage(), ex); 
     } 
     scheduler.scheduleJob(subscriptionJob, subscriptionCronTrigger);  
     scheduler.start(); 
    } 

在我SubscriptionDaily類:

public class SubscriptionDaily implements Job {  
    public void execute(JobExecutionContext arg0) throws JobExecutionException { 
     //Actions to be performed 
    } 
} 

現在檢查我的日誌,我得到步驟1,步驟2,但不是更進一步。

我的代碼正在被TimerServer類自身阻塞。 日誌WRT到調度是:

17:24:43 INFO [TimerServer]: Step 1  
17:24:43 INFO [SimpleThreadPool]: Job execution threads will use class loader of thread: http-8080-1  
17:24:43 INFO [SchedulerSignalerImpl]: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl            
17:24:43 INFO [QuartzScheduler]: Quartz Scheduler v.1.6.5 created. 
17:24:43 INFO [RAMJobStore]: RAMJobStore initialized.        
17:24:43 INFO [StdSchedulerFactory]: Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 
17:24:43 INFO [StdSchedulerFactory]: Quartz scheduler version: 1.6.5    17:24:43 INFO [TimerServer]: Step 2 

我覺得一個日誌條目丟失: [QuartzScheduler]:調度DefaultQuartzScheduler _ $ _ NON_CLUSTERED開始。

請幫忙。

回答

1

雖然我的應用程序中沒有任何錯誤或異常因爲它而引發,但我並沒有在庫中包含common-collections jar。所以我是在虧損!

我從來沒有見過Java在這之前很愚蠢。這是Java的正確行爲還是我期望太多?

我也在我的應用程序中使用spring,Spring提供了一種很好且簡單的方法來通過bean處理Quartz和Java的TimerTask功能。幾個不錯的優雅教程: http://static.springsource.org/spring/docs/1.2.9/reference/scheduling.html http://www.javaranch.com/journal/200711/combining_spring_and_quartz.html

雖然在使用bean的方法的限制是,你必須硬編碼在春天XML文件的cron值,因此我們將失去靈活性。