我正在通过代码实现quartz job调度。下面是我计划和重新计划石英作业的方法。
void setJobSchedular(ScraperSearchOnRequest scraperSearchOnRequestInstance) {
try {
String groupName = "Scraper Group" + scraperSearchOnRequestInstance?.id
String jobName = "ScraperOnRequestJob"
Scheduler scheduler = getJobManagerService().quartzScheduler
JobKey jobKey = new JobKey(jobName, groupName);
//create JobDetail and Trigger
JobDetail jobDetail = JobBuilder.newJob(com.dogmasystems.scraper.ScraperOnRequestJob1.class).withIdentity(jobKey).build();
//add passing parameters to JobDataMap for first JobDetail
jobDetail.getJobDataMap().put("id", scraperSearchOnRequestInstance?.id?.toString());
jobDetail.getJobDataMap().put("groupName", groupName);
if (scheduler.checkExists(jobKey)) {
println("Rescheduling the old job having group name " + groupName)
Trigger oldTrigger = scheduler.getTrigger(new TriggerKey(jobName, groupName))
TriggerBuilder tb = oldTrigger.getTriggerBuilder();
Trigger newTrigger = tb.startAt(scraperSearchOnRequestInstance.scheduledTime).usingJobData("id",
scraperSearchOnRequestInstance?.id?.toString()).build()
scheduler.rescheduleJob(oldTrigger.key, newTrigger);
} else {
println("Scheduling the new job having group name " + groupName)
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, groupName)
.startAt(scraperSearchOnRequestInstance.scheduledTime).usingJobData("id",
scraperSearchOnRequestInstance?.id?.toString()).build();
scheduler.scheduleJob(jobDetail, trigger);
}
} catch (Exception e) {
println("**********************Error in setJobSchedular for scraperOnRequestJob****************************")
e.printStackTrace("Error while setting ScraperOnRequestJob " + e.printStackTrace())
println("******************************************************")
}
}
基本上我的作业在指定的时间运行,并且使用上述方法可以正常工作。
需求:
用于停止工作我要提供该特定工作的jobName和groupName。
void stopSchedularJob(String id) throws Exception {
String groupName = "Scraper Group" + id
String jobName = "ScraperOnRequestJob"
JobKey jobKey = new JobKey(jobName, groupName);
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName);
Scheduler scheduler = getJobManagerService().quartzScheduler
Trigger trigger = scheduler.getTrigger(triggerKey)
if (trigger) {
println "Trigger key name to stop " + triggerKey.getName()
// wait long enough to see the job execution
Thread.sleep(10 * 1000); //1 minute
scheduler.unscheduleJob(triggerKey)
println "The ScraperOnRequestJob having " + groupName + " has been stopped."
} else {
println "No trigger could be found for " + triggerKey.getName()
}
}
但是它给了我错误:
Error |
2019-11-21 11:20:10,411 [http-bio-8078-exec-2] ERROR spi.SqlExceptionHelper - ERROR: canceling statement due to user request
Where: while updating tuple (0,36) in relation "scraper_search"
Error |
org.springframework.dao.DataAccessResourceFailureException: Hibernate operation: could not execute statement; SQL [n/a]; ERROR: canceling statement due to user request
Where: while updating tuple (0,36) in relation "scraper_search"; nested exception is org.postgresql.util.PSQLException: ERROR: canceling statement due to user request
Where: while updating tuple (0,36) in relation "scraper_search"
我尝试了很多解决方案,例如
scheduler.interrupt(jobKey)
scheduler.shutdown()
(我只想杀死特定的工作,而不是所有工作)
石英作业侦听器
但是我仍然找不到特定的解决方案。
您也可以将timer task用于计划任务。这样您就可以轻松创建和终止它们。
公共抽象类TimerTask扩展对象实现Runnable*可以安排一次或通过计时器重复执行的任务。
根据您的要求,使用Hashtable
将timer-obj与密钥存储起来,以备将来使用。
public class TimerTasks {
public static void main(String[] args) throws SchedulerException, IOException {
Timer timerObj = new Timer();
MyTask myTask = new MyTask(); // This task is scheduled to run every 10 seconds
startTimer("T1", timerObj, myTask, 2, 10000);
System.out.println("Enter Something to stop Timer.");
System.in.read();
System.in.read();
terminateTimer("T1");
System.out.println("Main end.");
}
static Map<String, Timer> taskGroup = new Hashtable<String, Timer>();
public static String startTimer(String key, Timer timerObj, TimerTask task, long delay, long period) {
if (taskGroup.containsKey(key)) {
return "Fialure, On this key alreay timer is available.";
} else {
timerObj.scheduleAtFixedRate(task, delay, period);
// You can save multiple, from key get timer object and you can cancel.
taskGroup.put(key, timerObj);
return "Success";
}
}
public static void terminateTimer(String key) {
if (taskGroup.containsKey(key)) {
Timer timer = taskGroup.get("T1");
// This task has been cancelled (with a call to TimerTask.cancel).
timer.cancel();
taskGroup.remove(key);
}
}
}
class MyTask extends TimerTask{
public MyTask(){
//Some stuffs
}
@Override
public void run() {
System.out.println("Hi, see its Timer task.");
}
}