2013-04-10 108 views
0

石英調度程序的新功能。我試圖實現的是在將來觸發一個觸發器,然後關閉調度器。我爲此使用了scheduler.shutdown(true),但在執行作業之前它會關閉。我必須調用shutdown(),因爲我將在Web應用程序中實現調度程序。石英調度程序在執行作業之前關閉

那麼如何在作業執行後關閉調度程序?

JOB:

public class HelloJob implements Job { 

    public HelloJob(){ 
    } 

    public void execute(JobExecutionContext context) 
      throws JobExecutionException { 
       System.out.println("Hello Quartz on " + new Date());  
      } 
} 

調度程序:

public class QuartzTest { 

    public void scheduleLoad(String time) { 
     try { 
      // Transform user input into a date 
      SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy:HH:mm:ss"); 
      Date scheduleDate = dateFormat.parse(time); 

      // Print Current vs. Scheduled time/date 
      System.out.println("Current time - " + new Date()); 
      System.out.println("Scheduled time - " + scheduleDate); 

      // Grab the Scheduler instance from the Factory 
      Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 

      // and start it off 
      scheduler.start(); 

      // Define a job and tie it to a class 
      JobDetail job = newJob(HelloJob.class) 
        .withIdentity("job1", "group1") 
        .build(); 

      // Trigger job to run now and repeat every 10 secs 
      SimpleTrigger trigger = (SimpleTrigger) newTrigger() 
        .withIdentity("trigger1", "group1") 
        .startAt(scheduleDate) 
        .forJob("job1","group1") 
        .build(); 

      // Schedule job using trigger 
      scheduler.scheduleJob(job, trigger); 

      // Shutdown the scheduler after job is executed 
      scheduler.shutdown(true); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     String runTime = "04/10/2013:20:07:00"; 
     QuartzTest quartz = new QuartzTest(); 
     quartz.scheduleLoad(runTime); 
    } 
} 

輸出:

Current time - Wed Apr 10 20:06:31 IST 2013 
Scheduled time - Wed Apr 10 20:07:00 IST 2013 
[main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor 
[main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main 
[main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 
[main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.1.7 created. 
[main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized. 
[main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.1.7) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED' 
    Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. 
    NOT STARTED. 
    Currently in standby mode. 
    Number of jobs executed: 0 
    Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. 
    Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 

[main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 
[main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.1.7 
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. 
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. 
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. 
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete. 

quartz.properties:

org.quartz.scheduler.instanceName = MyScheduler 
org.quartz.threadPool.threadCount = 3 
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore 
org.quartz.scheduler.skipUpdateCheck: true 

回答

5

我想你誤解的scheduler.shutdown(true);的目的,將等待執行作業完成,但它不會等待預定作業開始和結束。在關閉調度程序之前,您的工作沒有開始。在關閉它之前,您可以放置​​一個Thread.sleep(wait);。爲了讓您的代碼運行,因爲我知道你想要它,刪除此行(你基本上不用以往任何時候都需要關閉調度程序):

// Shutdown the scheduler after job is executed 
scheduler.shutdown(true); 

另外,移動此行,它就會只執行一次。當你把它取決於你的應用程序,它可以在main方法的獨立的應用程序,或者在Servlet的init方法或在Web應用程序運行的監聽器,如果:

// and start it off 
scheduler.start(); 

跟帖:

我不明白你爲什麼需要關閉Quartz。如果其他用戶需要安排任務,會發生什麼情況?你計劃在每個計劃任務中啓動一個不同的Quartz實例嗎?根據需要運行並安排任務會更有意義。這是運行Quartz的正常方式。你可能能夠以你想要的方式行事,但它可能是人爲的。如果你真的只想在啓動任務後關閉並在任務運行後關閉,可能需要查看JDK提供的Timer和TimerTask。見示例here

+0

好的,但是如果我不能預測'等待'將會是什麼值?例如 - 在Web應用程序中,假設我讓用戶安排只運行一次的作業,但在5/3/4/2天后,我將如何在應用程序中硬編碼Thread.sleep(wait)? – 2013-04-11 07:31:26

+1

正常的做法是讓石英模擬器在你的應用程序中運行。如果您只需要執行一項工作然後停止,那麼爲什麼要使用調度程序呢? – darrenmc 2013-04-11 09:54:41

+0

@darrenmc因爲這一項工作將由用戶安排在一個月後執行。之後,該線程應該被殺死嗎?你在說什麼是一個cronTrigger的權利?我將使用簡單的觸發器 – 2013-04-11 11:16:44