Scheduler In Java

Whenever people think of creating any scheduler in java the first thing that come to mind is Quartz. Quartz is Enterprise Job Scheduler which is well tested across the J2EE Industry. So here i’ll explain How to use Quartz and few of its useful features.

First and Foremost thing one has to do to create a scheduler is to Create an Instance of scheduler either by implementing the interface and implementing its methods or By using a default one which Quartz Provides.

Quartz provides a default scheduler which can be get by calling StdSchedulerFactory.getDefaultScheduler()

Now let me explain some basic Features of a scheduler

  1. Job   : This is the actual job which we want to run
  2. JobDetail : This is the data which we supply for JOB to run (Like context we pass to servlet)
  3. Trigger : This actually attached the Job with Scheduler in sense that Scheduler triggers the Job using Trigger Provided

Now Lets See How Simple is to create all the above Elements

			Trigger durectoryReaderTrigger = TriggerUtils

			JobDetail directoryListenerDetail = null;
			try {
				directoryListenerDetail = new JobDetail("DirectoryListenerJob",
						"DirectoryListenerGroup", FilePresenceListener.class);
			} catch (Throwable e) {
				System.out.println("Exception in Creation of Job Detail"
						+ e.getLocalizedMessage());
			}"Job Scheduled For Every 1 Minute");
			// Now Attach Job To Scheduler
			sched.scheduleJob(directoryListenerDetail, durectoryReaderTrigger);
			// Now Start The Scheduler

In The above code you can see how easy it is to create a scheduler and attach a trigger. Scheduler also provides a facility where we can create a Trigger Using Cron Expression.



2 thoughts on “Scheduler In Java

  1. Creating a Thread takes some resources/consumes some time. While in server environment where we think creating threads for each concurrent request is seemingly costly we use ThreadPoolExecutor. ThreadPoolExecutor removes the creation/instantiation of threads for each new request rather than Pooling it for future use same as Our Database Connection Pools. The thread in threadpool executes the moment the worker thread assigns the thread any task.

    But Quartz is a Scheduler and is not at all related to ThreadPoolExecutor. What quartz does is to execute/schedule your jobs as an when you define it to execute. For Example Quartz have CronTriggers apart From SimpleTrigger which i created in Example

    CronTriggers are often more useful than SimpleTrigger, if you need a job-firing schedule that recurs based on calendar-like notions, rather than on the exactly specified intervals of SimpleTrigger.

    With CronTrigger, you can specify firing-schedules such as “every Friday at noon”, or “every weekday and 9:30 am”, or even “every 5 minutes between 9:00 am and 10:00 am on every Monday, Wednesday and Friday”.

    Even so, like SimpleTrigger, CronTrigger has a startTime which specifies when the schedule is in force, and an (optional) endTime that specifies when the schedule should be discontinued.

    For More Clarity you can go through Quartz Tutorial at

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s