Powered By Blogger

Sunday, June 24, 2012

Quartz 2.0 cron example

This can be considered as an enhancement done to Quartz 2.0 with Spring , which had the job implementations as inner classes. The same can be accomplished with jobs defined as outer classes. The following method schedules the job.

 =======================================================

private void sheduleMyJob() throws SchedulerException, ParseException {

        JobDetail job = newJob(com.shyarmal.quartz.daemon.jobs.MyJob.class)
                .withIdentity("my-job").build();
          
        Map dataMap = job.getJobDataMap();
        dataMap.put("quantity", quantity);
        dataMap.put("myService", myService);
        dataMap.put("unitPrice", unitPrice);

        CronTrigger trigger = newTrigger()
                .withIdentity("my-flag", "priority")
                .withSchedule(cronSchedule(chargeCronExpression)).forJob(job)
                .build();

        scheduler.scheduleJob(job, trigger);

        LOGGER.info("initialized scheduler with expression [{}] \n expression summary([{}]) ",
                trigger.getCronExpression(), trigger.getExpressionSummary());

}

=======================================================

JobDetail and CronTrigger initializations are the same as that in the previous example. Here the parameters to be passed are put to a job data map, which is obtained from JobDetails.
      "  Map dataMap = job.getJobDataMap();
        dataMap.put("quantity", quantity);
        dataMap.put("myService", myService);
        dataMap.put("unitPrice", unitPrice); "
 The scheduler is initialized in the same way as the previous example.

The job class may be of the following form.
=======================================================

package com.shyarmal.quartz.daemon.jobs;

import com.shyarmal.quartz.common.charging.service.IMyService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;


public class MyJob implements Job {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Map dataMap = context.getJobDetail().getJobDataMap();
        int quantity = (Integer) dataMap.get("quantity");
        IMyService myService = (IMyService) dataMap.get("myService");
        double unitPrice = (Double) dataMap.get("unitPrice");
        try {
            // implementation code of your task.
        } catch (Exception e) {
            LOGGER.error("Error occurred while doing job.", e);
        } finally {
            // implementation
        }
    }
}

 =======================================================

JobDetails will be available through the JobExecutionContext passed in to the execute method of the job, from which the job data map can be retrieved. 
       " Map dataMap = context.getJobDetail().getJobDataMap();
        int quantity = (Integer) dataMap.get("quantity");
        IMyService myService = (IMyService) dataMap.get("myService");
        double unitPrice = (Double) dataMap.get("unitPrice"); "



thanks,
Shyarmal.

No comments:

Post a Comment