Tried a bit of quartz, the function is very powerful, unfortunately before already has this aspect design, want to replace words, the workload is too big.
For a problem often encountered before: timeout (TimeOut) processing mechanism, as if there is no relevant treatment quartz inside countermeasures, so try to write a:
Import Org.apache.log4j.Logger;
Import Org.quartz.InterruptableJob;
Import Org.quartz.JobExecutionContext;
Import org.quartz.UnableToInterruptJobException;
/**
*
* @author Zhoucan
*/
public class Timeoutthread extends thread{
private static Logger log = Logger.getlogger (Timeoutthread.class);
private static long default_timeout = 100l; The default timeout period is 100S;
Private Jobexecutioncontext context;
Private long timeout = -1l; The number of seconds to timeout;
Private Timeoutthread () {
Super ();
}
Public Timeoutthread (Jobexecutioncontext context) {
This ();
This.context = context;
If (context!= null) {
Long _temp = Context.getjobdetail (). Getjobdatamap (). Getlong ("TimeOut");
This.timeout = _temp > 0l? _temp:default_timeout;
}
}
public void Run () {
if (timeout = = 1)
Return
try{
Sleep (timeout * 1000l);
}catch (Interruptedexception Ire) {
Log.error (IRE);
}
try {
((Interruptablejob) context.getjobinstance ())
. interrupt ();
catch (Unabletointerruptjobexception e) {
throw new RuntimeException ("exception", e);
}
}
}
Import Org.apache.log4j.Logger;
Import Org.quartz.JobExecutionContext;
Import org.quartz.JobExecutionException;
Import Org.quartz.JobListener;
/**
*
* @author Zhoucan
*/
public class Defaultlistener implements joblistener{
Private final static Logger log = Logger.getlogger (Defaultlistener.class);
Timeoutthread _timeout;
Public String GetName () {
return "Defaultlistener";
}
public void Jobexecutionvetoed (Jobexecutioncontext context) {
TODO auto-generated Method Stub
}
public void jobtobeexecuted (Jobexecutioncontext context) {
_timeout = new Timeoutthread (context);
_timeout.start ();
}
public void jobwasexecuted (Jobexecutioncontext context,
Jobexecutionexception jobexception) {
_timeout.interrupt ();
}
}
The main principle is the use of Joblistener mechanism, job began to execute, start an on-duty thread, the first dormant timeout time, and so on after the sleep, if not finished, will this job to interrupt tune.
Not officially used, but only a small range of testing. Welcome everyone to treatise.