import java.sql.SQLException;
import java.text.ParseException;
import org.apache.log4j.Logger;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import com.util.FacesUtils;
/**
*
*/
/**
* @author jijo
*
*/
public class JobScheduler {
/**
* Author Jijo Mathew
* This method is used to start the scheduler.
* @return
* @throws Exception
*/
private static Logger logger = Logger.getLogger(JobScheduler.class);
private String expression = null; //put your expreession
private String cronExpressionSummary = null; //
public void setSchedulerStart() throws SchedulerException, SQLException{
StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = (Scheduler) schedulerFactory.getScheduler();
JobDetail jobDetail = null;
CronTrigger cronTrigger = null;
String expression = null;
jobDetail = new JobDetail("clientJob", "clientJobGroup", Job1.class);
String triggerName = "cronTrigger";
setScheduler(triggerName,expression,jobDetail);
jobDetail =new JobDetail("clientJob1", "clientJobGroup", Job2.class);
triggerName = "cronTrigger1";
setScheduler(triggerName,expression,jobDetail);
//}
scheduler.getContext().put("servletContext", FacesUtils.getServletContext());
// start the scheduler
scheduler.start();
FacesUtils.getServletContext().setAttribute("schedulerFlag", true);
}
/**
* Author Jijo Mathew
* This method will schedule the jobs to the scheduler.
* @return
* @throws Exception
*/
private void setScheduler(String triggerName,String expression,JobDetail jobDetail) throws SchedulerException{
StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
CronTrigger cronTrigger = new CronTrigger(triggerName, "triggerGroup");
CronExpression cexp = null;
try {
cexp = new CronExpression(expression);
} catch (ParseException e) {
//e.printStackTrace();
logger.error("SchedulerBean:setScheduler():"+e);
}
// Assign the CronExpression to CronTrigger
cronTrigger.setCronExpression(cexp);
// schedule a job with JobDetail and Trigger
scheduler.scheduleJob(jobDetail, cronTrigger);
}
/**
* Author Jijo Mathew
* This method is used to unschedule the jobs from the scheduler.
* @return
* @throws Exception
*/
public void setJobUnschedule(int schId) throws SchedulerException{
if(schId != 0){
String triggerName = null;
if(schId == 1){
triggerName = "cronTrigger";
}
if(schId == 2){
triggerName = "cronTrigger1";
}
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();
String[] triggerGroups;
String[] triggers;
triggerGroups = scheduler.getTriggerGroupNames();
for (int i = 0; i < triggerGroups.length; i++) { triggers = scheduler.getTriggerNames(triggerGroups[i]); for (int j = 0; j < triggers.length; j++) { Trigger tg = scheduler.getTrigger(triggers[j], triggerGroups[i]); if (tg instanceof CronTrigger && tg.getName().equals(triggerName)) { // unschedule the job scheduler.unscheduleJob(triggers[j], triggerGroups[i]); } } } } } /** * Author Jijo Mathew * This method is used to set the jobs to the scheduler. * @return * @throws ParseException * @throws Exception */ public void setJobSchedule(int schId) throws SchedulerException, SQLException, ParseException{ JobDetail jobDetail = null; CronTrigger cronTrigger = null; String expression = null; String triggerName = null; if(CronExpression.isValidExpression(expression)){ /* if(CronExpression.isValidExpression(expression)){ CronExpression cronExpression = new CronExpression(expression); System.out.println(cronExpression.getExpressionSummary()); System.out.println("valid"); } else{ System.out.println("invalid"); } */ if(schId == 1){ triggerName = "cronTrigger"; //jobDetail =new JobDetail("clientJob", "clientJobGroup", ClientJob.class); cronTrigger = new CronTrigger("cronTrigger", "triggerGroup"); cronTrigger.setJobGroup("clientJobGroup"); cronTrigger.setJobName("clientJob"); expression = ""; //ur expression } if(schId == 2){ triggerName = "cronTrigger1"; //jobDetail =new JobDetail("clientJob1", "clientJobGroup", ClientJob1.class); cronTrigger = new CronTrigger("cronTrigger1", "triggerGroup"); cronTrigger.setJobGroup("clientJobGroup"); cronTrigger.setJobName("clientJob1"); expression = "";////ur expression } // Initiate a Schedule Factory StdSchedulerFactory schedulerFactory = new StdSchedulerFactory(); // schedulerFactory.initialize(""); // SchedulerFactoryBean factoryBean = new SchedulerFactoryBean(); // factoryBean.setApplicationContext((ApplicationContext)FacesUtils.getServletContext()); // Retrieve a scheduler from schedule factory Scheduler scheduler = schedulerFactory.getScheduler(); String[] triggerGroups; String[] triggers; triggerGroups = scheduler.getTriggerGroupNames(); // Retrieve a scheduler from schedule factory CronExpression cexp = new CronExpression(expression); try { // setup CronExpression // Assign the CronExpression to CronTrigger cronTrigger.setCronExpression(cexp); } catch (Exception e) { //e.printStackTrace(); logger.error("SchedulerBean:setJobSchedule():"+e); } // schedule a job with JobDetail and Trigger for (int i = 0; i < triggerGroups.length; i++) { triggers = scheduler.getTriggerNames(triggerGroups[i]); for (int j = 0; j < triggers.length; j++) { Trigger tg = scheduler.getTrigger(triggers[j], triggerGroups[i]); if (tg instanceof CronTrigger && tg.getName().equals(triggerName)) { // unschedule the job scheduler.rescheduleJob(triggers[j], triggerGroups[i],cronTrigger); } } } this.cronExpressionSummary = cexp.getExpressionSummary(); } } } ////////////////Job1 and Job2..... package jobs; import javax.servlet.ServletContext; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class Job1 implements Job { public void execute(JobExecutionContext ctx)throws JobExecutionException { try{ ServletContext servletContext = (ServletContext) ctx.getScheduler().getContext().get("servletContext"); //TODO } catch (Exception e) { e.printStackTrace(); } } } package jobs; import javax.servlet.ServletContext; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class Job2 implements Job { public void execute(JobExecutionContext ctx)throws JobExecutionException { try{ ServletContext servletContext = (ServletContext) ctx.getScheduler().getContext().get("servletContext"); //TODO } catch (Exception e) { e.printStackTrace(); } } }
also put in the web.xml
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
For more details visit
http://www.opensymphony.com/quartz/wikidocs/Tutorial.html
No comments:
Post a Comment