import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class MediaCronJobTrigger {
private static Logger logger = Logger
.getLogger(MediaCronJobTrigger.class.getName());
private static class CronJobTriggerHolder {
static MediaCronJobTrigger instance = new MediaCronJobTrigger();
}
public static MediaCronJobTrigger buildSingleton() {
return CronJobTriggerHolder.instance;
}
public MediaCronJobTrigger() {
try {
sf = new StdSchedulerFactory();
sched = sf.getScheduler();
} catch (SchedulerException e) {
logger.error("定时任务初始化失败,异常:" + e);
}
}
private SchedulerFactory sf = null;
private Scheduler sched = null;
private JobDetail mediaTaskJob = null;
public JobDetail getMediaTaskJob() {
return mediaTaskJob;
}
public void setMediaTaskJob(JobDetail mediaTaskJob) {
this.mediaTaskJob = mediaTaskJob;
}
public CronTrigger getMediaTaskTrigger() {
return mediaTaskTrigger;
}
public void setMediaTaskTrigger(CronTrigger mediaTaskTrigger) {
this.mediaTaskTrigger = mediaTaskTrigger;
}
private CronTrigger mediaTaskTrigger = null;
private String cronRegx = "0 * * ? * * *";
public void doCronJob() throws SchedulerException,ParseException
{
if(sched == null)
return;
mediaTaskJob = new JobDetail("mediaTaskJob", "mediaTaskJobGroup", MediaTaskJob.class);
mediaTaskTrigger = new CronTrigger("mediaTaskTrigger", "mediaTaskTriggerGroup", "mediaTaskJob",
"mediaTaskJobGroup", cronRegx);
sched.scheduleJob(mediaTaskJob, mediaTaskTrigger);
Date currentTime=new Date();
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
logger.debug("MulteMediaHandle Job Start At:" + simpleFormat.format(currentTime));
sched.start();
}
public void setSchedulerFactory(SchedulerFactory sf) {
this.sf = sf;
}
public SchedulerFactory getSchedulerFactory() {
return sf;
}
public void setScheduler(Scheduler sched) {
this.sched = sched;
}
public Scheduler getScheduler() {
return sched;
}
public void setCronRegx(String cronRegx) {
this.cronRegx = cronRegx;
}
public String getCronRegx() {
return cronRegx;
}
public static void main(String[] args) throws Exception {
MediaCronJobTrigger cronJobTrigger = new MediaCronJobTrigger();
cronJobTrigger.doCronJob();
}
}