2011-08-23 76 views

回答

33

你需要写一份你的工作作为InterruptableJob的实施。 要中断这份工作,你需要处理对Scheduler,并调用interrupt(jobKey<<job name & job group>>)

请具有以上班看看@的javadoc,还石英分发包含一个这样的例子(实施例7)。

+0

Thx))))))))))) – user253202

+1

此方法的限制是**不** **群集意识。 Java doc: *此方法不支持群集。也就是说,它只会中断当前在此计划程序实例中执行的InterruptableJob的实例,而不会在整个群集中执行。* –

4

石英2.1使用Spring,你可以:

@Autowired 
private Scheduler schedulerFactoryBean; //injected by spring 
... 
... 

List<JobExecutionContext> currentlyExecuting = schedulerFactoryBean.getCurrentlyExecutingJobs(); 

//verifying if job is running  
for (JobExecutionContext jobExecutionContext : currentlyExecuting) { 
    if(jobExecutionContext.getJobDetail().getKey().getName().equals("JobKeyNameToInterrupt")){ 
     result = schedulerFactoryBean.interrupt(jobExecutionContext.getJobDetail().getKey()); 
    } 
} 
2

在我看来,最好的办法是在这个线程所描述的:一套停止后http://forums.terracotta.org/forums/posts/list/7700.page

我刚刚出台了“休眠”标志为true以允许工作完成干净。

@Override 
public void interrupt() throws UnableToInterruptJobException { 
    stopFlag.set(true); 
    try { 
     Thread.sleep(30000); 
    } catch (InterruptedException e) { 
     //logger.error("interrupt()", e); 
    } 
    Thread thread = runningThread.getAndSet(null); 
    if (thread != null) 
     thread.interrupt(); 
} 
2

我不知道为什么没有人提到过这个问题,或者这个问题在提问时没有提供。

调度程序实例有一种称为关闭的方法。

SchedulerFactory factory = new StdSchedulerFactor(); 
Scheduler scheduler = factory.getScheduler(); 

以上用于启动工作像

scheduler.start(); 

使用标志或东西,知道什么时候该停止运行的工作。 然后使用

scheduler.shutdown(); 

我如何实现我的要求:

if(flag==true) 
    { 
     scheduler.start(); 
     scheduler.scheduleJob(jobDetail, simpleTrigger); 
    } 
    else if(flag==false) 
    { 
     scheduler.shutdown(); 
    } 

其中的JobDetail和simpleTrigger不言自明。

希望它有帮助。 :)

+7

此解决方案将关闭整个计划程序,除非您将一个作业分配给计划程序,否则这种方法并不正确。您应该有办法只删除特定的作业,而不是整个调度程序,因为可能有多个作业在同一个调度程序上运行。 – Salman

相关问题