quartz 疑问!先addJob,线程开启了。打开removeJob,屏蔽addJob,为什么不能删除任务?

Binas 发布于 2015/09/11 11:52
阅读 930
收藏 0

@kobe_gino 你好,想跟你请教个问题:

package foo;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import org.apache.log4j.Logger;
import org.quartz.Scheduler;


import com.mchange.v2.c3p0.ComboPooledDataSource;


import commons.*;


public class Main {

private static Logger logger = Logger.getLogger(Main.class);


public static void main(String[] args)
    {
QuartzManager qs = new QuartzManager();
QuartzTest qTest = new QuartzTest();
//qs.addJob("test", qTest, "*/10 * * * * ?");
qs.removeJob("DefaultQuartzScheduler", "test");
    }


}





package commons;






import org.apache.log4j.Logger;




import org.quartz.Job;
import org.quartz.CronScheduleBuilder; 
import org.quartz.JobBuilder; 
import org.quartz.JobDetail; 
import org.quartz.JobKey;
import org.quartz.Scheduler; 
import org.quartz.SchedulerException; 
import org.quartz.SchedulerFactory;
import org.quartz.Trigger; 
import org.quartz.TriggerBuilder; 
import org.quartz.impl.StdSchedulerFactory;
 




/**
 * 
 * @author  
 *  QuartzManager 封装quartz的相关操作。
 *  
 */


 


public class QuartzManager {

private static Logger logger = Logger.getLogger(QuartzManager.class);
private String JOBGROUP = "QuartzJobGroup_";
private String TRIGGERGROUP = "QuartzTriggerGroup_";
private  SchedulerFactory schedulerFactory = new StdSchedulerFactory();
private Scheduler scheduler = null;
private JobDetail jobDetail;
private Trigger trigger;

public void QuartzManager(){


}

//设置任务组名、触发组名,不设置即使用默认值
public void QuartzManager(String jobGroup, String triggerGroup){
JOBGROUP = jobGroup;
TRIGGERGROUP = triggerGroup;
}


//获取一个调度器
private void buildScheduler() throws SchedulerException {
this.scheduler = this.schedulerFactory.getScheduler();
logger.info(this.scheduler.getSchedulerName());
}

//创建jobDetail实例,绑定job实现类
private void buildJob(String jobName, Job job){
this.jobDetail = JobBuilder.newJob(job.getClass()).withIdentity(jobName, JOBGROUP).build();
}

//创建触发器,绑定cron表达式
private void buildTrigger(String triggerName, String cron){
this.trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, TRIGGERGROUP)
.withSchedule(CronScheduleBuilder.cronSchedule(cron)).startNow().build();
}




//添加调度任务
public void addJob(String name, Job job, String cron){
try{
this.buildScheduler();
this.buildJob(name, job);
this.buildTrigger(name, cron);
//添加任务、触发器至调度器
this.scheduler.scheduleJob(this.jobDetail, this.trigger);
this.scheduler.start();
}
catch (Exception e) {
logger.error(e.getMessage());
}
}

//更新触发器
public void removeJob(String sc,String name){
try{
this.scheduler = this.schedulerFactory.getScheduler(sc);
this.scheduler.deleteJob(JobKey.jobKey(name, JOBGROUP));
}
catch (Exception e) {
e.printStackTrace();
}

}




//删除调度任务
public void removeJob(String name){
try{


}
catch(Exception e){
e.printStackTrace();  
}
}





}

错误:

java.lang.NullPointerException
at commons.QuartzManager.removeJob(QuartzManager.java:89)
at foo.Main.main(Main.java:24)

加载中
返回顶部
顶部