Quartz 中可以推迟触发触发吗?

发布于 2024-12-27 08:49:45 字数 2965 浏览 2 评论 0原文

我有两个进程:

进程1 - 实现可运行并且可以永远运行。 进程 2 - 在一天中的固定时间和分钟触发(我创建了一个使用 Quartz 运行的作业)。

要警告进程 1 其他进程正在运行,我可以使用 TriggerListener,但是如果进程 1 仍在执行某些操作,如何推迟第二个进程的触发?

例如:我需要在下午 2 点触发触发器,但如果进程 1 不空闲,则需要在下午 2 点之后完成。

这是一些示例:

ProcessForever.java

import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

public class ProcessForever implements Runnable {

    private boolean processTwoRunning;
    private Scheduler scheduler;
    private Trigger trgProcessTwo;
    private String status;

    public static final STATUS_PROCESS = "PROCESS";
    public static final STATUS_SLEEP = "SLEEP";

    private static Logger LOGGER = Logger.getLogger( ProcessForever.class.getName() );

    public void init() throws SchedulerException {
        SchedulerFactory fact = new StdSchedulerFactory();
        scheduler = fact.getScheduler();
    }

    @Override
    public void run() {
        try {
          scheduler.start();
          buildTrigger();
          while( true ) {
              //do something and then sleep for some time.
              //the Quartz trigger should fire only in STATUS_SLEEP...
              setStatus( STATUS_PROCESS );      
              try { Thread.sleep(120 * 1000); }catch(Exception e){}
              setStatus( STATUS_SLEEP );        
          }catch( Exception e ) {
              e.printStackTrace();
          }
}

    private void buildTrigger() throws SchedulerException {
    LOGGER.info("defineCargaDadosTrigger()");
    JobDetail dt = newJob( ProcessTwo.class )
                             .withIdentity("coleta","grpcoleta")
                             .build();

    trgProcessTwo = newTrigger().withIdentity( 
                          new TriggerKey("triggerProcessTwo") )
                                  .forJob( dt )
                                  .startNow()
                                  .withSchedule( dailyAtHourAndMinute(13,31) )
                                  .build();
    KeyMatcher<TriggerKey> m = KeyMatcher.keyEquals( trgProcessTwo.getKey() );
    scheduler.scheduleJob(dt, trgProcessTwo );
    //this will notice the process 1 that the trigger is running...
            //scheduler.getListenerManager().addTriggerListener(someclass, m );
}

    //getters & setters ommited...

}

ProcessTwo.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 /**
  ProcessTwo cannot run concurrent with ProcessForever...
  */
 public ProcessTwo implements Job {
      @Override
      public void execute(JobExecutionContext arg0) throws JobExecutionException {
          System.out.println("Doing something...");
          try { Thread.sleep(10000); } catch( InterruptedException i ){}
          System.out.println("Stop doing something...");
      }
 }

I have two processes:

Process 1 - implements runnable and can run forever.
Process 2 - fires at fixed hour and minute of day (i've created a job that run with Quartz).

To warn the process 1 that the other process is running I can use the TriggerListener, but how can I postpone the fire of the second process if the process 1 still doing something?

For example: I need to fire the trigger at 2PM, but this need to be done after 2PM if the process 1 isnt idle.

Here's some sample:

ProcessForever.java

import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

public class ProcessForever implements Runnable {

    private boolean processTwoRunning;
    private Scheduler scheduler;
    private Trigger trgProcessTwo;
    private String status;

    public static final STATUS_PROCESS = "PROCESS";
    public static final STATUS_SLEEP = "SLEEP";

    private static Logger LOGGER = Logger.getLogger( ProcessForever.class.getName() );

    public void init() throws SchedulerException {
        SchedulerFactory fact = new StdSchedulerFactory();
        scheduler = fact.getScheduler();
    }

    @Override
    public void run() {
        try {
          scheduler.start();
          buildTrigger();
          while( true ) {
              //do something and then sleep for some time.
              //the Quartz trigger should fire only in STATUS_SLEEP...
              setStatus( STATUS_PROCESS );      
              try { Thread.sleep(120 * 1000); }catch(Exception e){}
              setStatus( STATUS_SLEEP );        
          }catch( Exception e ) {
              e.printStackTrace();
          }
}

    private void buildTrigger() throws SchedulerException {
    LOGGER.info("defineCargaDadosTrigger()");
    JobDetail dt = newJob( ProcessTwo.class )
                             .withIdentity("coleta","grpcoleta")
                             .build();

    trgProcessTwo = newTrigger().withIdentity( 
                          new TriggerKey("triggerProcessTwo") )
                                  .forJob( dt )
                                  .startNow()
                                  .withSchedule( dailyAtHourAndMinute(13,31) )
                                  .build();
    KeyMatcher<TriggerKey> m = KeyMatcher.keyEquals( trgProcessTwo.getKey() );
    scheduler.scheduleJob(dt, trgProcessTwo );
    //this will notice the process 1 that the trigger is running...
            //scheduler.getListenerManager().addTriggerListener(someclass, m );
}

    //getters & setters ommited...

}

ProcessTwo.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 /**
  ProcessTwo cannot run concurrent with ProcessForever...
  */
 public ProcessTwo implements Job {
      @Override
      public void execute(JobExecutionContext arg0) throws JobExecutionException {
          System.out.println("Doing something...");
          try { Thread.sleep(10000); } catch( InterruptedException i ){}
          System.out.println("Stop doing something...");
      }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2025-01-03 08:49:45

这是 Quartz 中一个很常见的问题。以下是常见问题解答提供的一些提示

That's quite a common question in Quartz. Here are some hints provided by the FAQ

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文