-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathExecute.java
More file actions
666 lines (528 loc) · 17.2 KB
/
Execute.java
File metadata and controls
666 lines (528 loc) · 17.2 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
package org.hy.common;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* 线程方式执行相关的动作
*
* 注意:被执行的方法必须是 public 的。
*
* 可由两种方式获取执行结果:
* 1. 通过本类的 this.getResult() 方法。
* 2. 通过实现事件监听接口 org.hy.common.ExecuteListener。
*
* @author ZhengWei(HY)
* @version v1.0 2013-11-15
* @version v2.0 2014-08-15 1.支持超时自动终止线程功能(核心功能)
* 2.记录线程运行第三方方法的开始时间、结束时间,及计算运行时长
* 3.支持获取异常信息的方法,但不直接抛出异常(已改为:抛出异常)
* 4.当用户自己设置异常信息时,系统不覆盖用户的异常
* @version v3.0 2016-03-02 1.添加:延后多少时间后再执行第三方方法的功能。this.startDelayed();
* 2.添加:停止执行方法 this.stopExecute();
* @version v3.1 2016-12-23 1.修正:将版本v2.0中的第3条改为:直接抛出异常,方便及时发现问题。
* @version v4.0 2017-01-06 1.添加:支持对重载方法的判定。对每参数类型都进行比较确认惟一的执行方法。
* @version v4.1 2017-01-14 1.添加:支持执行实现的父类的方法。
* 将 this.intance.getDeclaredMethods() 改为:this.intance.getClass().getMethods()。
* @version v5.0 2017-01-19 1.添加:保存动作方法的执行结果。
* 添加:执行结果的事件监听功能。
* v5.1 2017-03-22 1.添加:getReturnAwait() 方法在启动线程后,一直等待执行方法的返回结果。
* v6.0 2021-12-16 1.添加:支持永不超时,一直等待
*/
public class Execute extends Thread
{
// private static final Logger $Logger = new Logger(Execute.class);
/** 动作的实例 */
private Object instance;
/** 动作的方法名称(方法的全名称) */
private String methodName;
/** 动作的方法参数 */
private Object [] params;
/** 动作方法的执行结果。(未执行、执行异常、方法无返回值三种情况均返回null) */
private Object result;
/** 执行结果的监听事件 */
private ExecuteResultFire resultFire;
/** 执行第三方方法的开始时间 */
private Date runBeginTime;
/** 执行第三方方法的结束时间 */
private Date runEndTime;
/** 延后多长时间后再执行第三方(单位:毫秒)。默认为0,即立即执行 */
private long delayedTime;
/** 超时终止线程的时长(单位:毫秒)。表示永不超时,一直等待 */
private long timeout;
/** 监控、监视的间隔时长(单位:毫秒) */
private long watchInterval;
/** 标记是否运行完成 */
private boolean isRunFinish;
/**
* 等待方法 waitting() 是否运行完成
*
* 向外界提供一种方便"持续等待"线程运行完成的方法
*/
private boolean isWaittingFinish;
/** 是否报错(内部使用)。对于超时终止线程动作而因起的异常不应报错 */
private boolean isReportError;
/** 调用第三方方法时的异常信息,及超时终止自创建的异常信息 */
private Throwable exception;
/**
* 线程方式执行相关的动作(无入参)
*
* @param i_Intance 动作的实例
* @param i_MethodName 动作的方法名称(方法的全名称)。如果在实例内部运行,此方法可以是私有类型的private。
* @param i_Params 动作的方法参数
*/
public Execute(Object i_Intance ,String i_MethodName)
{
this(i_Intance ,i_MethodName ,new Object[]{});
}
/**
* 线程方式执行相关的动作(一个入参)
*
* @param i_Intance 动作的实例
* @param i_MethodName 动作的方法名称(方法的全名称)。如果在实例内部运行,此方法可以是私有类型的private。
* @param i_Params 动作的方法参数
*/
public Execute(Object i_Intance ,String i_MethodName ,Object i_Param)
{
this(i_Intance ,i_MethodName ,new Object[]{i_Param});
}
/**
* 线程方式执行相关的动作(多个入参)
*
* @param i_Instance 动作的实例
* @param i_MethodName 动作的方法名称(方法的全名称)。如果在实例内部运行,此方法可以是私有类型的private。
* @param i_Params 动作的方法参数
*/
public Execute(Object i_Instance ,String i_MethodName ,Object [] i_Params)
{
this.instance = i_Instance;
this.methodName = i_MethodName.trim();
this.params = i_Params;
this.result = null;
this.resultFire = null;
this.watchInterval = 300;
this.isReportError = true;
this.delayedTime = 0;
}
/**
* 主要执行的方法或动作
*/
@Override
public void run()
{
// 延后多长时间后再执行第三方(单位:毫秒)。默认为0,即立即执行
if ( this.delayedTime > 0 )
{
try
{
Thread.sleep(this.delayedTime);
}
catch (Exception exce)
{
exce.printStackTrace();
}
}
this.result = null;
this.exception = null;
Method v_Method = MethodReflect.getMethod(this.instance ,this.methodName ,this.params);
if ( null != v_Method )
{
try
{
if ( v_Method.getReturnType() == java.lang.Void.TYPE )
{
v_Method.invoke(this.instance ,this.params);
}
else
{
this.result = v_Method.invoke(this.instance ,this.params);
}
}
catch (Throwable exce)
{
if ( isReportError )
{
exce.printStackTrace();
this.exception = exce;
}
}
}
else
{
System.out.println(this.instance.getClass().getName() + "的方法" + this.methodName + "不存在或不可访问到");
// $Logger.warn(this.instance.getClass().getName() + "的方法" + this.methodName + "不存在或不可访问到");
}
this.runEndTime = new Date();
this.isRunFinish = true;
// 通知所有事件监听者。在超时被内部关闭时,也同样会执行下面的代码
if ( this.resultFire != null )
{
this.resultFire.fire(new ExecuteEvent(this.instance
,this.runEndTime.getTime()
- this.runBeginTime.getTime()
- this.delayedTime
,this.exception != null
,this.result));
}
}
/**
* 启动线程,开始执行
*
* @see java.lang.Thread#start()
*/
@Override
public synchronized void start()
{
this.startDelayed(0);
}
/**
* 启动线程,延后多长时间后再执行第三方(单位:毫秒)。默认为0,即立即执行
*
* @author ZhengWei(HY)
* @createDate 2016-03-02
* @version v1.0
*
* @param i_DelayedTime 延后时长(单位:毫秒)
*/
public synchronized void startDelayed(long i_DelayedTime)
{
this.runBeginTime = new Date();
this.runEndTime = null;
this.delayedTime = i_DelayedTime;
super.start();
}
/**
* 启动线程,同时线程运行时间超时后,终止线程
*
* @param i_Timeout 超时终止线程的时长(单位:毫秒)。0表示永不超时,一直等待
*/
public synchronized void start(long i_Timeout)
{
this.startDelayed(0 ,i_Timeout);
}
/**
* 启动线程,延后多长时间后再执行第三方(单位:毫秒)。默认为0,即立即执行
* 启动线程,同时线程运行时间超时后,终止线程。
*
* @author ZhengWei(HY)
* @createDate 2016-03-02
* @version v1.0
*
* @param i_DelayedTime 延后时长(单位:毫秒)
* @param i_Timeout 超时终止线程的时长(单位:毫秒)。0表示永不超时,一直等待
*/
public synchronized void startDelayed(long i_DelayedTime ,long i_Timeout)
{
if ( i_Timeout != 0 && i_Timeout <= 1000 )
{
throw new RuntimeException("Timeout is not <= 1000 millisecond.");
}
this.delayedTime = i_DelayedTime;
this.timeout = i_Timeout;
if ( this.timeout != 0 )
{
Execute v_Execute = new Execute(this ,"runIsTimeout");
v_Execute.start();
}
this.start();
}
/**
* 停步执行
*
* @author ZhengWei(HY)
* @createDate 2016-03-02
* @version v1.0
*
*/
public synchronized void stopExecute()
{
if ( !this.isRunFinish )
{
try
{
this.isReportError = false;
this.interrupt();
}
catch (Exception exce)
{
exce.printStackTrace();
}
// 当用户自己设置异常信息时,不覆盖用户的异常
if ( this.exception == null )
{
if ( Help.isNull(this.params) )
{
this.exception = new RuntimeException("Timeout Error: Call " + this.instance.getClass().getName() + "." + this.methodName + "().");
}
else
{
this.exception = new RuntimeException("Timeout Error: Call " + this.instance.getClass().getName() + "." + this.methodName + "(...).");
}
}
this.runEndTime = new Date();
this.isRunFinish = true;
this.isReportError = true;
}
this.isWaittingFinish = true;
}
/**
* 监控、监视是否运行超时
*/
public void runIsTimeout()
{
this.isRunFinish = false;
this.isWaittingFinish = false;
this.exception = null;
while ( this.timeout > 0 && !this.isRunFinish )
{
try
{
Thread.sleep(this.watchInterval);
}
catch (Exception exce)
{
exce.printStackTrace();
}
this.timeout = this.timeout - this.watchInterval;
}
this.stopExecute();
}
public long getWatchInterval()
{
return watchInterval;
}
public void setWatchInterval(long i_WatchInterval)
{
if ( i_WatchInterval <= 10 )
{
throw new RuntimeException("WatchInterval is not <= 10 millisecond.");
}
this.watchInterval = i_WatchInterval;
}
public Date getRunBeginTime()
{
return runBeginTime;
}
public Date getRunEndTime()
{
return runEndTime;
}
/**
* 获取运行时长(单位:毫秒)
*
* @return
*/
public long getRunTimeLen()
{
long v_Ret = 0;
if ( this.runBeginTime == null )
{
return v_Ret;
}
if ( this.runEndTime == null )
{
v_Ret = Date.getNowTime().getTime() - this.runBeginTime.getTime();
}
else
{
v_Ret = this.runEndTime.getTime() - this.runBeginTime.getTime();
}
return v_Ret > 0 ? v_Ret : 0;
}
/**
* 当用户自己设置异常信息时,系统不覆盖用户的异常
*
* @return
*/
public Throwable getException()
{
return exception;
}
/**
* 当用户自己设置异常信息时,系统不覆盖用户的异常
*
* @param exception
*/
public void setException(Throwable exception)
{
this.exception = exception;
}
public boolean isError()
{
return this.exception == null ? false : true;
}
public boolean isRunFinish()
{
return isRunFinish;
}
/**
* 0表示永不超时,一直等待
*
* @return
*/
public boolean isTimeout()
{
return timeout < 0 ? true : false;
}
/**
* 获取:延后多长时间后再执行第三方(单位:毫秒)。默认为0,即立即执行
*/
public long getDelayedTime()
{
return delayedTime;
}
/**
* 向外界提供一种方便"持续等待"线程运行完成的方法
*/
public void waitting()
{
while ( !this.isWaittingFinish )
{
try
{
Thread.sleep(this.watchInterval);
}
catch (Exception exce)
{
exce.printStackTrace();
}
}
}
/**
* 向外界提供一种方便"持续等待"线程运行完成的方法
*
* 并向输出对象输出如 ... 的形式
*/
public void waitting(PrintStream i_Out)
{
while ( !this.isWaittingFinish )
{
try
{
Thread.sleep(this.watchInterval);
i_Out.print(".");
}
catch (Exception exce)
{
exce.printStackTrace();
}
}
}
/**
* 获取:动作方法的执行结果。(未执行、执行异常、方法无返回值三种情况均返回null)
*/
public Object getResult()
{
return result;
}
/**
* 获取:动作方法的执行结果,并一直等待执行结果。
*
* @author ZhengWei(HY)
* @createDate 2017-03-22
* @version v1.0
*
* @return
*/
public Object getResultAwait()
{
while ( this.result == null )
{
Thread.yield();
}
return this.result;
}
/**
* 执行结果:添加事件监听者
*
* 配合XJava的XML文件配置
*
* @author ZhengWei(HY)
* @createDate 2017-01-19
* @version v1.0
*
* @param i_Listener
*/
public synchronized void setListener(ExecuteListener i_Listener)
{
this.addListener(i_Listener);
}
/**
* 执行结果:添加事件监听者
*
* @author ZhengWei(HY)
* @createDate 2017-01-19
* @version v1.0
*
* @param i_Listener
*/
public synchronized void addListener(ExecuteListener i_Listener)
{
if ( this.resultFire == null )
{
this.resultFire = new ExecuteResultFire(new ArrayList<ExecuteListener>());
}
this.resultFire.addListener(i_Listener);
}
/**
* 执行结果:移除事件监听者
*
* @author ZhengWei(HY)
* @createDate 2017-01-19
* @version v1.0
*
* @param i_Listener
* @return
*/
public ExecuteListener removeListener(ExecuteListener i_Listener)
{
return this.resultFire.removeListener(i_Listener);
}
/**
* 执行结果:清空所有事件监听者
*
* @author ZhengWei(HY)
* @createDate 2017-01-19
* @version v1.0
*
*/
public void clearListeners()
{
this.resultFire.clearListeners();
}
/**
* 执行结果的监听事件
*
* @author ZhengWei(HY)
* @createDate 2017-01-19
* @version v1.0
*/
class ExecuteResultFire extends EventFire<ExecuteListener ,ExecuteEvent>
{
public ExecuteResultFire(List<ExecuteListener> i_Listeners)
{
super(i_Listeners);
}
/**
* 触发事件。通知所有的事件监听者。
*
* @author ZhengWei(HY)
* @createDate 2017-01-19
* @version v1.0
*
* @param i_Event
*/
@Override
public void fire(ExecuteEvent i_Event)
{
if ( Help.isNull(this.listeners) )
{
return;
}
for (ExecuteListener v_Listener : this.listeners)
{
v_Listener.result(i_Event);
}
}
}
}