我的问题是,当我使用以下方法从控制器执行作业时:
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);
}
作业停止,但for
中的new SendMailController().sendMail()
继续他的工作,直到它最终完成。如何在执行stopSceduler()
时停止它?
答案 0 :(得分:0)
我用DB(Grails中的Domain)解决了这个问题。在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)
}