summaryrefslogtreecommitdiff
path: root/src/com/nis/nmsclient/thread/task/LoopTaskThread.java
blob: b3e8a363548bb1a932c6692fbf6b0d43c18d939e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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));
		}
	}
}