Quartz Cron Schedular Java

Quartz Cron Schedular Java

As you saw in previous tutorial how to integrate and run Quarz scheduler from Spring application. If you are not using Spring and looking for standalone java program which can be integrated with the application you can below:

Note: To run this application quartz.x.x.verion.jar which you can download from maven here

Recap form previous tutorail different cron schedule time:

#Cron scheduler run every minute
0 0/1 * 1/1 * ? *

#Run job every 30 minute:
0 0/30 * 1/1 * ? *

#Run job everyday at 16:00 PM:
0 00 18 1/1 * ? *

#Run job everyday at multiple time start from 16:01,16:02… 21:01,21:02 etc..:
0 00,01,02,03 18,19,20,21 1/1 * ? *

#Run job every 15 minute:
0 0/15 * 1/1 * ? *

  • StandAloneQuartzJob.java:
package com.javahonk;

import java.time.LocalDateTime;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author Java Honk
 *
 */
public class StandAloneQuartzJob implements Job {
	
	public static void main(String[] args) {
		
		System.out.println("CronQuartzJob started.");		
		StandAloneQuartzJob.startJavaHonkQuartzJob();		
		
	}

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		
		System.out.println("Cron Quartz Job started at time: "+ LocalDateTime.now());

	}
	
	public static void startJavaHonkQuartzJob() {

		try {

			JobDetail job = JobBuilder.newJob(StandAloneQuartzJob.class).withIdentity("StandAloneQuartzJob", "StandAloneQuartzJob detials").build();
			
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("StandAloneQuartzJob", "StandAloneQuartzJob detials")
					.withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).build();

			Scheduler scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.start();
			scheduler.scheduleJob(job, trigger);
			
		} catch (SchedulerException e) {
			System.out.println("Exception occured class name:-> "+ StandAloneQuartzJob.class.getName()+ "method: startJavaHonkQuartzJob:-->{}"+ e);
			e.printStackTrace();
			
		}

	}	

}
  • Output:

Quartz Cron Schedular Java

Leave a Reply

Your email address will not be published. Required fields are marked *