Quartz_Scheduler - Yash-777/LearnJava GitHub Wiki

Getting started with Quartz - GitHUB

Quartz Job Scheduler is Open Source for Job Automation by scheduling Time Interval. So that the job can be executed when ever the Trigger is fired. Trigger Listeners fires at a specified fire time. When ever trigger is raised, execute() method of the Specified class which implements JOB Interface will be invoked.

Use Cron Maker to Generate Corn Expression.

You need Quartz core library, named quartz-x.y.z.jar (where x.y.z is a version number), in its classpath.

Version_1.8.5

Example Main Class where the Scheduler Demon is Going to Start:

@Controller public class SchedulerFactoryTest {

	public static Scheduler schedulerDemon = null;
	static {
		try { //Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 20 threads.
			if(schedulerDemon == null){
				Properties p = new Properties();
				p.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
				p.setProperty("org.quartz.threadPool.threadCount", "20");
				StdSchedulerFactory factory = new StdSchedulerFactory(p);
				schedulerDemon = factory.getScheduler();
				schedulerDemon.start();
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		HttpServletRequest request = null; HttpServletResponse response = null;
		ModelMap model = null;
		new SchedulerFactoryTest().Test(request, response, model);
	}
	
	@RequestMapping(value = "/ScheduleReq")
	public void Test(HttpServletRequest request,HttpServletResponse response, ModelMap model){
		String jobName = "Unique Name";
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			String startDate = "2017-1-10", startTime = "19:39:05";
			String dateInString = startDate + " " + startTime;
			date = formatter.parse(dateInString);
			System.out.println("Dtae Supplied : "+date);
		} catch (ParseException e) {
		}
		
		JobDetail job = new JobDetail();
		job.setName(jobName);
		job.setJobClass(Quartz_JOB_Impl.class);
		job.getJobDataMap().put("myKEY", jobName);
		
		SimpleTrigger trigger = new SimpleTrigger();
		trigger.setName(jobName);
		trigger.setStartTime(date);

		try {
			schedulerDemon.scheduleJob(job, trigger);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}

Class which implements JOB interface and @override execute(). so that the execute() method code will be executed by scheduler when trigger is raised.

public class Quartz_JOB_Impl implements Job {

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		Date time = context.getFireTime();
		String jobName = (String)context.getJobDetail().getJobDataMap().get("myKEY");
		System.out.format("Trigger Time : %s, KEY : %s \n", time, jobName);
		try {
			System.out.println("Scheduler Demon Thread is Shutdown.");
			SchedulerFactoryTest.schedulerDemon.shutdown();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}

Version_2.1.5

When ever Records get changed in Data Base trigger fires event example.

Note: If you are facing compile time Problem regarding jar version, then exclude them in POM.

<properties>
	<hibernateVersion>3.1.2.RELEASE</hibernateVersion>
</properties>
<dependencies>
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-validator</artifactId>
		<version>${hibernateVersion}</version>
		<!--
		<exclusions>
			<exclusion>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-api</artifactId>
			</exclusion>
		</exclusions>
		-->
	</dependency>
</dependencies>

Quartz is the most well known solution to schedule processes in Java environments, but you have a lot of options list.

⚠️ **GitHub.com Fallback** ⚠️