diff options
| author | chenjinsong <[email protected]> | 2018-09-27 16:11:54 +0800 |
|---|---|---|
| committer | chenjinsong <[email protected]> | 2018-09-27 16:11:54 +0800 |
| commit | 56d71f261a8bd6031e47e2bf80867049a2aa13da (patch) | |
| tree | f09257b2143782a333a9eda3395137837d9bdad1 /src/com/nis/nmsclient/thread/task/LoopTaskThread.java | |
initial commit
Diffstat (limited to 'src/com/nis/nmsclient/thread/task/LoopTaskThread.java')
| -rw-r--r-- | src/com/nis/nmsclient/thread/task/LoopTaskThread.java | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/com/nis/nmsclient/thread/task/LoopTaskThread.java b/src/com/nis/nmsclient/thread/task/LoopTaskThread.java new file mode 100644 index 0000000..b3e8a36 --- /dev/null +++ b/src/com/nis/nmsclient/thread/task/LoopTaskThread.java @@ -0,0 +1,106 @@ +package com.nis.nmsclient.thread.task; + +import java.util.Date; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.log4j.Logger; + +import com.nis.nmsclient.common.Common; +import com.nis.nmsclient.model.CommandPO; +import com.nis.nmsclient.util.Utils; + +public class LoopTaskThread extends Thread{ + Logger logger = Logger.getLogger(LoopTaskThread.class); + + private String threadName; + private long loopDelay; + private CommandPO command; + + private long i = 0; + private Future<?> future = null; + private Thread thread; + + private boolean isCancle; + + public LoopTaskThread(String threadName, CommandPO command, long loopDelay, Future<?> singleFuture, Thread singleThread){ + this.threadName = threadName; + this.loopDelay = loopDelay; + this.command = command; + this.future = singleFuture; + this.thread = singleThread; + + isCancle = false; + } + + public synchronized void cancle() { + isCancle = true; + } + + public void run() { + i++; +// Thread.currentThread().setName(threadName + " 周期" + i); + Thread.currentThread().setName(threadName + " Cycle" + i); + long et = System.currentTimeMillis();// 本次开始时间,即上次执行结束时间 + long st = et - (i * loopDelay * 60 * 1000);// 上次开始时间 + try { + if (i == 1 && future != null + && !future.isCancelled() + && !future.isDone()) { + try { + future.get(1, TimeUnit.MICROSECONDS); + } catch (TimeoutException e) { + if(thread!=null && thread.isAlive()){ + thread.stop(); + logger.debug("LoopTaskThread run Timeout stop singleThread--" + thread.isAlive()); + } + future.cancel(true); + } + + } + if(!isCancle){ + // 周期开始执行 + future = Common.scheduled.schedule(new Runnable() { + public void run() { + thread = Thread.currentThread(); +// Thread.currentThread().setName(threadName + " 周期" + i); + Thread.currentThread().setName(threadName + " Cycle" + i); + new AgentCommand(command).exec(); + } + }, 0, TimeUnit.MILLISECONDS); + try { + future.get(loopDelay, TimeUnit.MINUTES); + } catch (TimeoutException e) { + if(thread!=null && thread.isAlive()){ + thread.stop(); + logger.debug("LoopTaskThread run Timeout stop thread--" + thread.isAlive()); + } + future.cancel(true); + logger.debug("---------LoopTaskThread run timeout----------"); + TaskResultOper.sendTaskResult(command.getExecId(), + command.getExecType(), + AgentCommand.RESULT_FAIL, +// "本周期任务执行超时", "", new Date(st), +// "this task execution timeout", "", new Date(st), + "i18n_client.LoopTaskThread.loopTaskOuttime_n81i", "", new Date(st), + new Date(et), false, command.getIsLoop()); + }catch (InterruptedException e) { + if(thread!=null && thread.isAlive()){ + thread.stop(); + logger.debug("LoopTaskThread run Interrupted stop thread--" + thread.isAlive()); + } + future.cancel(true); + logger.debug("---------LoopTaskThread run Interrupted----------"); + } + } + + if(thread!=null){ + logger.debug("LoopTaskThread run thread state--" + thread.isAlive()); + } + } catch (Exception e) { + logger.info(Utils.printExceptionStack(e)); + } + } +} + |
