StdSchedulerFactory
class is used to create QuartzScheduler instances.
How to get an
instance of StdSchedulerFactory?
StdSchedulerFactory
class provides below constructors to get an instance of StdSchedulerFactory.
public
StdSchedulerFactory()
public
StdSchedulerFactory(Properties props)throws SchedulerException
public
StdSchedulerFactory(String fileName) throws SchedulerException
What is the fileName
in StdSchedulerFactory constructor?
It
is the property file, that specifies the properties used by quartz while
running the jobs.
Ex:
org.quartz.threadPool.class:
org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount:
10
org.quartz.threadPool.threadPriority:
5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread:
true
By
default a properties file named "quartz.properties" is loaded from
the 'current working directory'. If that fails, then the
"quartz.properties" file located (as a resource) in the org/quartz
package is loaded.
How to specify custom
quartz property file?
There
are three ways.
a.
By
passing the custom quartz property file path as an argument to StdSchedulerFactory constructor
b.
By
defining system property 'org.quartz.properties' to point to the file you want.
c.
By
explicitly initializing the factory by calling one of the initialize(xx)
methods before calling getScheduler()
myQuartzConfig.properties
org.quartz.scheduler.instanceName: DefaultQuartzScheduler org.quartz.scheduler.rmi.export: false org.quartz.scheduler.rmi.proxy: false org.quartz.scheduler.wrapJobExecutionInUserTransaction: false org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount: 10 org.quartz.threadPool.threadPriority: 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true org.quartz.jobStore.misfireThreshold: 60000 org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
HelloWorldJob.java
package com.sample.test; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloWorldJob implements Job{ public void execute(JobExecutionContext jec) throws JobExecutionException { System.out.println("Hello World"); } }
Test.java
package com.sample.test; import java.util.Date; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.SimpleTriggerImpl; public class Test { private static String quartzConfigFileName = "C:\\Users\\Krishna\\quartz_tutorial\\myQuartzConfig.properties"; public static void main(String args[]) throws SchedulerException { SchedulerFactory schedulerFactory = new StdSchedulerFactory(quartzConfigFileName); // Retrieve a scheduler from schedule factory Scheduler scheduler = schedulerFactory.getScheduler(); // Creating JobDetailImpl and link to our Job class JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setName("First Job"); jobDetail.setJobClass(HelloWorldJob.class); // Creating schedule time with trigger SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl(); simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); simpleTrigger.setRepeatInterval(3000); simpleTrigger.setName("FirstTrigger"); // Start scheduler scheduler.start(); scheduler.scheduleJob(jobDetail, simpleTrigger); } }
Comments
in the program are self-explanatory. When you run above program "Hello
World" message is printed to the console for every 3 seconds.
No comments:
Post a Comment