import java.util.Date;
import java.util.Observable;
import java.util.Observer;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
/**
* @author 10131788
* Aug 15, 2008
*/
public class SessionScheduler extends Observable implements JobListener{
private Scheduler scheduler = null;
private StdSchedulerFactory stdSchedulerFactory = null;
/**
* @param args
* @throws SchedulerException
*/
public static void main(String[] args) throws SchedulerException {
// TODO Auto-generated method stub
/* SessionScheduler scheduler = new SessionScheduler();
try {
scheduler.initialize();
} catch (AccountServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
scheduler.start();
} catch (AccountServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
public void initialize() {
try {
// lookup "Scheduler"
stdSchedulerFactory = new StdSchedulerFactory();
if (stdSchedulerFactory != null) {
scheduler = stdSchedulerFactory.getScheduler();
}
} catch (Exception e) {
}
}
public void addJobListener(Observer observer) {
this.addObserver(observer);
try {
scheduler.addJobListener(this);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getName() {
// TODO Auto-generated method stub
return "sessionJob";
}
public void jobExecutionVetoed(JobExecutionContext arg0) {
// TODO Auto-generated method stub
// set the notify
}
public void start() {
try {
scheduler.start();
} catch (SchedulerException e) {
}
}
public void jobToBeExecuted(JobExecutionContext arg0) {
// TODO Auto-generated method stub
// set the notify
setChanged();
// notify the observers
notifyObservers();
// reset the notify
clearChanged();
}
public void jobWasExecuted(JobExecutionContext arg0,
JobExecutionException arg1) {
}
public void addCronJob(int hour, int minute){
CronTrigger trigger = null;
JobDetail job = null;
String cronExpression = null;
Date scheduleDate = null;
if (scheduler != null) {
try {
// repeat every day at the requested hour, minute
cronExpression = new String("0 " + minute + " " + hour + " * * ?");
// build the trigger
// the trigger and the job have a one to one correspondance
// so use the same name and group for both
trigger = new CronTrigger("sessionTrigger", scheduler.DEFAULT_GROUP,
"sessionJob",scheduler.DEFAULT_GROUP, cronExpression);
// build the job
job = new JobDetail("sessionJob", scheduler.DEFAULT_GROUP, new SessionJob().getClass());
job.addJobListener(this.getName());
// Schedule the job to use the associated trigger
scheduleDate = scheduler.scheduleJob(job, trigger);
} catch (Exception e) {
}
}
}
}