2010-04-06 109 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值,因此我们将失去灵活性。