This project is a sample demo to schedule a job using Quartz Job Scheduler.
This is the main java class:
RunScheduler.java
---
package quartz;
import java.lang.Class;
import java.lang.Integer;
import java.lang.String;
import java.lang.Long;
import java.util.Date;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class RunScheduler {
public void run() throws Exception {
Log log = LogFactory.getLog(RunScheduler.class);
// log.info("------- Initializing ----------------------");
String KeyFile;
// First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
ResourceBundle rb = ResourceBundle.getBundle("application");
String actualName="job.classname";
String hours="job.starthours";
String minutes="job.startminutes";
String interval = "job.intervalmin";
String getHour= rb.getString(hours);
int getHourInt = Integer.parseInt(getHour);
String getMinute= rb.getString(minutes);
int getMinuteInt = Integer.parseInt(getMinute);
String intervalget=rb.getString(interval);
long intervalL1=Long.parseLong(intervalget);
long intervalL=intervalL1*60000;
Date date=new Date();
int month=date.getMonth();
int date1= date.getDate();
int year=date.getYear();
Date timeUser= new Date(year,month,date1,getHourInt,getMinuteInt);
KeyFile = rb.getString(actualName);
Class t= Class.forName(KeyFile);//returns a class object of the given string
String jobq="job"; // define the job and tie it to our Job class
JobDetail job = new JobDetail(jobq, "group1",t);
String triggerq="trigger";// Trigger the job to run on the next round minute
SimpleTrigger trigger = new SimpleTrigger(triggerq, "group1",timeUser,null,SimpleTrigger.REPEAT_INDEFINITELY,intervalL);
sched.scheduleJob(job, trigger);// Tell quartz to schedule the job using our trigger
log.info("Dated : " + date);
log.info("Job Start Time : " + timeUser);
log.info("Job Source File : " + t);
log.info("Job Interval Hours : " + intervalL1);
log.info("Job Scheduled Successfully");
sched.start();// Start up the scheduler (nothing can actually run until the scheduler has been started)
log.info("Scheduler Started Successfully");
}//end of method "run"
public static void main(String[] args) throws Exception {
RunScheduler runScheduler = new RunScheduler();
runScheduler.run();
}//end of "main" method
}
This is the class that will execute at the time scheduler is set up.
TestUpdate.java
---
package quartz;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class TestUpdate implements Job {
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("In EXECUTE Method..........");
System.out.println("Running the scheduled job");
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Job executed at: " + ft.format(dNow));
}
}
Create quartz.properties file:
org.quartz.scheduler.instanceName = new23_atruntime
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
Create application.properties file. Here you provide hours and mins when the job needs to execute: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
job.classname=quartz.TestUpdate
job.intervalmin=28
job.starthours=16
job.startminutes=35
job.intervalmin=28
job.starthours=16
job.startminutes=35
Create pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>QuartzTest</groupId>
<artifactId>QuartzTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
<modelVersion>4.0.0</modelVersion>
<groupId>QuartzTest</groupId>
<artifactId>QuartzTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
Now, run the java program RunScheduler.java and it will wait until it reaches in time to schedule the job.
No comments:
Post a Comment