gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:54:52 27 4
gpt4 key购买 nike

我有两个进程:

进程 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...");
}
}

最佳答案

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

关于java - Quartz 中可以推迟触发触发吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8853374/

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