In my previous post here I demonstrated how to register a quartz job and trigger it manually. Now it’s time to move ahead and schedule quartz jobs with cron expression. Like I had created a generic class for manual jobs let’s create one for schedule jobs. Here’s the source:

public class ScheduledJob {
    public static final Logger log = Logger.getLogger(ScheduledJob.class.getName());

    private String name;
    private String groupName;
    private JobDetail jobDetail;
    private JobDataMap jobDataMap = new JobDataMap();
    private String cronExpression;
    private Trigger trigger;

    public ScheduledJob(String firstName, String groupName,Class<? extends Job> clazz,
                         String cronExpression, Map<? extends String,?> jobDataMap){
        this.name = firstName;
        this.groupName = groupName;
        this.cronExpression = cronExpression ;
        if (jobDataMap != null)this.jobDataMap.putAll(jobDataMap);
        this.jobDetail = buildJob(firstName, groupName, clazz);
        this.trigger = this.buildTrigger();
    }

    public ScheduledJob(String firstName, String groupName,Class<? extends Job> clazz, String cronExpression){
        this(firstName, groupName, clazz, cronExpression, null);
    }

    private JobDetail buildJob(String firstName, String lastName, Class<? extends Job> clazz){
        return JobBuilder
                .newJob(clazz)
                .usingJobData(jobDataMap)
                .withIdentity(JobKey.jobKey(firstName, lastName))
                .storeDurably(true)
                .build();
    }

    private Trigger buildTrigger(){
        return TriggerBuilder.newTrigger().withIdentity(TriggerKey.triggerKey(this.getName(),this.getGroupName()))
                .withSchedule(CronScheduleBuilder.cronSchedule(this.cronExpression)).build();
    }

    public String getName() {
        return this.name;
    }

    public String getGroupName() {
        return this.groupName;
    }

    public JobDetail getJobDetail() {
        return this.jobDetail;
    }

    public JobKey getKey(){
        return JobKey.jobKey(this.name, this.groupName);
    }

    public Trigger getTrigger(){
        return this.trigger;
    }
}

That’s the job itself but it still doesn’t know what block does it need to execute when it will be triggered. Like in the previous post. We’ll need to create the class that implements Job interface and overrides the execute method of it like:

public class JobExecutor implements Job{
    public static final Logger log = Logger.getLogger(Loggable.class.getName());
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        StringBuffer dataString = new StringBuffer();
        for(Map.Entry<String,Object> e:jobExecutionContext.getMergedJobDataMap().entrySet()){
            dataString.append(e.getKey()).append(" : ").append(e.getValue()).append(" || ");
        }
        log.info("============JOB RUNNING with data============ "+dataString.toString());

    }
}

Now, let’s start scheduling the jobs.

public static void main(String... args) throws Exception{
        JobDataMap dataMap = new JobDataMap();
        dataMap.put("username", "prashant");
        ScheduledJob job3 = new ScheduledJob("job1","scheduled",JobExecutor.class,"*/6 * * * * ?",dataMap);
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        log.info("Starting the Scheduler... ");
        scheduler.start();
        log.info("Scheduler started... ");
        dataMap.put("app", "sample");
        log.info("-----------------"+job3.getTrigger());
        scheduler.scheduleJob(job3.getJobDetail(),job3.getTrigger());
    }

And that’s all to it.