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