Set up the timeout design inside the quartz

Source: Internet
Author: User
Tags sleep log4j

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.