Simple Quartz Schedular

In previous example you saw how to write cron Quartz scheduler. In this example you will see how to write simple Quartz schedular with cron expression. For simple Quartz scheduler function are available to schedule jobs as shown below:

Simple Quartz Schedular

Note: To run SimpleQuartzJob.java you will need quartz.x.x.verion.jar which you can download from maven here

  • SimpleQuartzJob.java:
package com.javahonk;

import java.time.LocalDateTime;

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.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author Java Honk
 *
 */
public class SimpleQuartzJob implements Job {
	
	public static void main(String[] args) {
		
		System.out.println("CronQuartzJob started.");		
		SimpleQuartzJob.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(SimpleQuartzJob.class).withIdentity("StandAloneQuartzJob Name", "StandAloneQuartzJob Group").build();
			
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("StandAloneQuartzJob Name", "StandAloneQuartzJob Group")
					.withSchedule(SimpleScheduleBuilder.simpleSchedule()
							.withIntervalInSeconds(30).repeatForever()).build();

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

	}	

}
  • Output:

Simple Quartz Schedular

Leave a Reply

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