First, create an instance of SchedulerFactory using the
reference of org.Quartz.impl.StdSchedulerFactory Class. It
invokes the getScheduler() method on this instance and
instantiates the Scheduler.
Now, start the scheduler using the start() method. Now
instantiate the JobDetail and SimpleTrigger classes.
Finally, pass the objects of the JobDetail and SimpleTrigger
classes to the scheduler object using the scheduleJob()
method.
Code for the Job Class:
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
public class HelloJob implements Job {
public void execute(JobExecutionContext arg0) throws
JobExecutionException{
System.out.println(“Hello World Quartz Scheduler: “ + new
Date()); }}
Code for the Scheduler Class:
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class HelloSchedule {
public static void main(String args[]){
try{
new HelloSchedule();
}
catch(Exception e){}
}
public HelloSchedule()throws Exception{
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler sched=sf.getScheduler();
sched.start();
JobDetail jd=new
JobDetail(“myjob”,sched.DEFAULT_GROUP,HelloJob.class);
SimpleTrigger st=new
SimpleTrigger(“mytrigger”,sched.DEFAULT_GROUP,new
Date(),null,SimpleTrigger.REPEAT_INDEFINITELY,60L*1000L);
sched.scheduleJob(jd, st); }}
Before executing this program, set the class
path as shown:
C:\> cd C:\JavaJazzup\quartz
C:\JavaJazzup\quartz>
setclasspath=%classpath%;C:\quartz-1.6.0\quartz-
1.6.0.jar;C:\quartz-1.6.0\quartz-all-1.6.0.jar;C:\quartz-
1.6.0\quartz-jboss-1.6.0.jar;C:\quartz-1.6.0\quartz-oracle-
1.6.0.jar;
|
C:\quartz-1.6.0\quartz-weblogic-1.6.0.jar;C:\quartz-.6.0\mysqlconnector-
java-3.1.6-bin.jar;
C:\quartz-1.6.0\lib\build\activation.jar;
C:\quartz-1.6.0\lib\build\ejb.jar;C:\quartz-
1.6.0\lib\build\javax.jms.jar;
C:\quartz-1.6.0\lib\build\jdbc2_0-stdext.jar;
C:\quartz-1.6.0\lib\build\jmx.jar;C:\quartz-1.6.0\lib\build\jta.jar;
C:\quartz-1.6.0\lib\build\junit.jar;
C:\quartz-1.6.0\lib\build\mail.jar;
C:\quartz-1.6.0\lib\build\servlet.jar;
C:\quartz-1.6.0\lib\core\commons-collections-3.1.jar;
C:\quartz-1.6.0\lib\core\commons-logging.jar;
C:\quartz-1.6.0\lib\core\commons-logging-api.jar;
C:\quartz-1.6.0\lib\optional\commons-beanutils.jar;
C:\quartz-1.6.0\lib\optional\commons-dbcp-1.2.1.jar;
C:\quartz-1.6.0\lib\optional\commons-digester-1.7.jar;
C:\quartz-1.6.0\lib\optional\commons-modeler-.1.jar;
C:\quartz-1.6.0\lib\optional\commons-pool-1.2.jar;
C:\quartz-1.6.0\lib\optional\commons-validator-1.1.4.jar;
C:\quartz-1.6.0\lib\optional\log4j-1.2.11.jar;
Now compile both java files (job and scheduler) and run
it.
You will see the output of the application as:
C:\JavaJazzup\quartz>javac HelloJob.java
C:\JavaJazzup\quartz>javac HelloSchedule.java
C:\JavaJazzup\quartz>java HelloSchedule log4j: WARN No
appenders could be found for logger
(org.quartz.simpl.SimpleThreadPool).
log4j: WARN Please initialize the log4j system properly.
Hello World Quartz Scheduler:
Fri Jul 13 11:16:35 GMT+05:30 2007
Hello World Quartz Scheduler:
Fri Jul 13 11:17:35 |