views:

322

answers:

1

Hi I am using Quartz scheduler to trigger a cron which needs to perform a host of activities. My Code for the same is as follow:

In the init() method of my InitServlet class, I am defining my 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();
        }
    }

In my TimerServer class I have the following methods

    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();
    }

In my SubscriptionDaily class :

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

Now checking my logs, I am getting Step 1, Step 2 but not further.

My code is getting stucked at the TimerServer class itself. Logs wrt to Scheduler are :

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

I think a log entry is missing : [QuartzScheduler]: Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.

Please help.

+1  A: 

I had not included common-collections jar in my library though there were no errors or exceptions being thrown anywhere in my application because of it. So I was at loss !!

I have never seen Java being so dumb before this. Is this the right behavior of java or am I expecting too much from it ?

I am also using spring in my application and Spring provides a good and simple way to handle Quartz and Java's TimerTask feature through beans. Few good and elegant tutorials are : http://static.springsource.org/spring/docs/1.2.9/reference/scheduling.html http://www.javaranch.com/journal/200711/combining_spring_and_quartz.html

Though the restriction in using the bean approach is, you have to hard code the cron values in the spring xml file, and so we will loose the flexibility.

Amit