0
我有两个进程:是否可以推迟在Quartz中引发火灾?
进程1 - 实现可运行并可以永久运行。 过程2 - 每天固定的小时和分钟发射(我创建了一个使用Quartz运行的作业)。
要警告进程1其他进程正在运行,我可以使用TriggerListener
,但是如果进程1仍在执行某些操作,我该如何推迟第二个进程的启动?
例如:我需要在2PM触发触发器,但是这需要在2PM后完成,如果进程1没有空闲。
下面是一些例子:
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...");
}
}
提供的一些提示。FAQ的确有帮助。我解决了使用JobDataMap选项。谢谢! – 2012-01-16 10:48:37