gpt4 book ai didi

java - 停止在 JobService Groovy 中执行的所有作业

转载 作者:行者123 更新时间:2023-12-02 16:02:35 25 4
gpt4 key购买 nike

我的问题是,当我使用以下方法从 Controller 执行作业时:

def startScheduler(){
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
///////////////////////////////
JobDetail job = JobBuilder.newJob(SchedulerJobService.class)
.withIdentity("SendMailJob","DB")
.build();
///////////////////////////////
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("crontrigger","DB")
.withSchedule(CronScheduleBuilder.cronSchedule(cronSce))
.build();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}

来电:
class SchedulerJobService implements Job {  
private static final log = LogFactory.getLog(SchedulerJobService.class)

boolean transactional = true

public void execute (JobExecutionContext jobExecutionContext) {
new SendMailController().sendMail()
}
}

在方法 sendMail() for循环执行,发送大量邮件,当我执行 stopScheduler() :
public def stopScheduler(){
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey("crontrigger","DB");
scheduler.unscheduleJob(triggerKey);
}

作业停止,但 fornew SendMailController().sendMail()继续他的工作,直到最终完成。当我执行 stopSceduler() 时如何停止它?

最佳答案

我用 DB(Grails 中的域)解决了这个问题。在方法sendMail()我有:

     def sendMail(correoList, nombreList){
int index = correoList.size()

try{
for(int i = 0; i < index; i++){
def tcp = TemplateControlPanelText.get(1)
if(!tcp.enviando){
break;
}
.....
}
}catch(Exception e){
}
}

一开始:
def startScheduler(){
def t = TemplateControlPanelText.get(1)
t.enviando = true
t.save(flush:true)
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
.....
}

最后,当我停止工作时,我有:
public def stopScheduler(){
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey("crontrigger","DB");
scheduler.unscheduleJob(triggerKey);
def t = TemplateControlPanelText.get(1)
t.enviando = false
t.save(flush:true)
}

关于java - 停止在 JobService Groovy 中执行的所有作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28982016/

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