-
-
Save sangramanand/60b236e7fd1432c76248 to your computer and use it in GitHub Desktop.
BasicQuartzSchedulerProgram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
quartz.properties | |
------------------ | |
org.quartz.scheduler.instanceName = ALARM_SCHEDULER | |
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool | |
org.quartz.threadPool.threadCount = 4 | |
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true | |
#specify the jobstore used | |
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX | |
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate | |
org.quartz.jobStore.useProperties = false | |
#The datasource for the jobstore that is to be used | |
org.quartz.jobStore.dataSource = myDS | |
#quartz table prefixes in the database | |
org.quartz.jobStore.tablePrefix = QRTZ_ | |
org.quartz.jobStore.misfireThreshold = 60000 | |
org.quartz.jobStore.isClustered = false | |
#The details of the datasource specified previously | |
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver | |
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/gsraisin | |
org.quartz.dataSource.myDS.user = root | |
org.quartz.dataSource.myDS.password = rootAdmin | |
org.quartz.dataSource.myDS.maxConnections = 20 | |
SimpleJob.java | |
---------------- | |
package com.generalsentiment.test.quartz; | |
import com.generalsentiment.model.dao.organization.OrganizationDAO; | |
import com.generalsentiment.model.om.member.Member; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.logging.Level; | |
import org.quartz.Job; | |
import org.quartz.JobExecutionContext; | |
import org.quartz.JobExecutionException; | |
import org.quartz.JobKey; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class SimpleJob implements Job { | |
private static Logger _log = LoggerFactory.getLogger(SimpleJob.class); | |
/** | |
* Quartz requires a public empty constructor so that the scheduler can | |
* instantiate the class whenever it needs. | |
*/ | |
public SimpleJob() { | |
} | |
/** | |
* <p> Called by the | |
* <code>{@link org.quartz.Scheduler}</code> when a | |
* <code>{@link org.quartz.Trigger}</code> fires that is associated with the | |
* <code>Job</code>. </p> | |
* | |
* @throws JobExecutionException if there is an exception while executing | |
* the job. | |
*/ | |
private String OrganizationId = "402881702110d62f012110e0e8d70001"; | |
private OrganizationDAO organizationDAO = new OrganizationDAO(); | |
@Override | |
public void execute(JobExecutionContext context) | |
throws JobExecutionException { | |
JobKey jobKey = context.getJobDetail().getKey(); | |
System.out.println("SimpleJob says: " + jobKey + " executing at " + new Date()); | |
File file = new File("OrganizationMembers.txt"); | |
FileWriter fw = null; | |
try { | |
fw = new FileWriter(file.getName()); | |
} catch (IOException ex) { | |
java.util.logging.Logger.getLogger(SimpleJob.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
BufferedWriter bw = new BufferedWriter(fw); | |
List<Member> users = organizationDAO.getUsers(OrganizationId); | |
for (Member current : users) { | |
String memberName = current.getMemberfirstname() + " " + current.getMemberlastname(); | |
try { | |
bw.write(memberName+ "\n"); | |
} catch (IOException ex) { | |
java.util.logging.Logger.getLogger(SimpleJob.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
try { | |
bw.close(); | |
} catch (IOException ex) { | |
java.util.logging.Logger.getLogger(SimpleJob.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
System.out.println("Job Done"); | |
} | |
} | |
CronTriggerExample.java | |
----------------------- | |
package com.generalsentiment.test.quartz; | |
import static org.quartz.CronScheduleBuilder.cronSchedule; | |
import static org.quartz.JobBuilder.newJob; | |
import static org.quartz.TriggerBuilder.newTrigger; | |
import java.util.Date; | |
import org.quartz.CronTrigger; | |
import org.quartz.JobDetail; | |
import org.quartz.Scheduler; | |
import org.quartz.SchedulerFactory; | |
import org.quartz.SchedulerMetaData; | |
import org.quartz.impl.StdSchedulerFactory; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class CronTriggerExample { | |
public void run() throws Exception { | |
Logger log = LoggerFactory.getLogger(CronTriggerExample.class); | |
System.out.println("------- Initializing -------------------"); | |
// First we must get a reference to a scheduler | |
SchedulerFactory sf = new StdSchedulerFactory("quartz.properties"); | |
Scheduler sched = sf.getScheduler(); | |
System.out.println("------- Initialization Complete --------"); | |
System.out.println("------- Scheduling Jobs ----------------"); | |
// jobs can be scheduled before sched.start() has been called | |
// job 1 will run exactly at 12:55 daily | |
JobDetail job = newJob(SimpleJob.class).withIdentity("job2", "group2").build(); | |
CronTrigger trigger = newTrigger().withIdentity("trigger2", "group2") | |
.withSchedule(cronSchedule("00 10 17 * * ?")).build(); | |
Date ft = sched.scheduleJob(job, trigger); | |
System.out.println(sched.getSchedulerName()); | |
System.out.println(job.getKey() + " has been scheduled to run at: " + ft | |
+ " and repeat based on expression: " | |
+ trigger.getCronExpression()); | |
System.out.println("------- Starting Scheduler ----------------"); | |
/* | |
* All of the jobs have been added to the scheduler, but none of the | |
* jobs will run until the scheduler has been started. If you have | |
* multiple jobs performing multiple tasks, then its recommended to | |
* write it in separate classes, like SimpleJob.class writes organization | |
* members to file. | |
*/ | |
sched.start(); | |
System.out.println("------- Started Scheduler -----------------"); | |
System.out.println("------- Waiting five minutes... ------------"); | |
try { | |
// wait five minutes to show jobs | |
Thread.sleep(300L * 1000L); | |
// executing... | |
} catch (Exception e) { | |
} | |
System.out.println("------- Shutting Down ---------------------"); | |
sched.shutdown(true); | |
System.out.println("------- Shutdown Complete -----------------"); | |
SchedulerMetaData metaData = sched.getMetaData(); | |
System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); | |
} | |
public static void main(String[] args) throws Exception { | |
CronTriggerExample example = new CronTriggerExample(); | |
example.run(); | |
} | |
} | |
web.xml | |
------- | |
<context-param> | |
<param-name>quartz:config-file</param-name> | |
<param-value>quartz.properties</param-value> | |
</context-param> | |
<context-param> | |
<param-name>quartz:shutdown-on-unload</param-name> | |
<param-value>true</param-value> | |
</context-param> | |
<context-param> | |
<param-name>quartz:wait-on-shutdown</param-name> | |
<param-value>false</param-value> | |
</context-param> | |
<context-param> | |
<param-name>quartz:start-scheduler-on-load</param-name> | |
<param-value>true</param-value> | |
</context-param> | |
<listener> | |
<listener-class> | |
org.quartz.ee.servlet.QuartzInitializerListener | |
</listener-class> | |
</listener> | |
<servlet> | |
<servlet-name>QuartzInitializer</servlet-name> | |
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class> | |
<init-param> | |
<param-name>shutdown-on-unload</param-name> | |
<param-value>true</param-value> | |
</init-param> | |
<load-on-startup>2</load-on-startup> | |
</servlet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment