gpt4 book ai didi

java - 如何在特定的 quartz 作业运行时停止执行?

转载 作者:行者123 更新时间:2023-12-02 00:03:02 27 4
gpt4 key购买 nike

我正在通过代码实现quartz job调度。以下是我安排和重新安排 quartz 作业的方法。

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("******************************************************")
}
}

基本上我的作业在指定时间运行,并且使用上述方法运行良好。

要求:

  1. 我想通过使用 ajax 调用单击 UI 上的停止按钮来停止正在运行的作业执行。

enter image description here

为了停止作业,我给出了该特定作业的jobNamegroupName

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"

我尝试了很多解决方案,例如

  1. scheduler.interrupt(jobKey)

  2. scheduler.shutdown()(我只想杀死特定作业,而不是所有作业)

  3. quartz 作业监听器

但我仍然无法找到特定的解决方案。

最佳答案

调用scheduler.interrupt(jobKey)不会终止正在运行的作业的执行。您可以找到文档here

如何中断正在运行的作业的示例 here

关于java - 如何在特定的 quartz 作业运行时停止执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58968471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com