永发信息网

spring管理的quartz可以传递参数吗

答案:2  悬赏:70  手机版
解决时间 2021-03-17 02:08
  • 提问者网友:星軌
  • 2021-03-16 04:33
spring管理的quartz可以传递参数吗
最佳答案
  • 五星知识达人网友:逐風
  • 2021-03-16 04:53
spring的quartz定时任务并不能完全满足项目的需要,经常会遇到需要任务的执行时
全部回答
  • 1楼网友:躲不过心动
  • 2021-03-16 05:22
可以,自己写一个,参考: import org.apache.log4j.logger; import org.quartz.interruptablejob; import org.quartz.jobexecutioncontext; import org.quartz.unabletointerruptjobexception; public class timeoutthread extends thread{ private static logger log = logger.getlogger(timeoutthread.class); private static long default_timeout = 100l; //缺省的超时时间是100s; private jobexecutioncontext context; private long timeout = -1l; //超时的秒数; 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; 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(); } } 主要原理是利用joblistener机制,job开始执行时,启动一个值守的thread,先休眠规定的timeout时间,等休眠完后,假如还未执行完,就将这个job给interrupt调。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯