[Fix #21] 修复了RandomIntervalSendTimer在高版本Java中因非法反射导致应用异常终止的问题;

This commit is contained in:
LamGC 2020-07-09 22:17:09 +08:00
parent 4784f8773b
commit 73a1caaf46
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D

View File

@ -4,7 +4,6 @@ import com.google.common.base.Throwables;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -23,7 +22,7 @@ public class RandomIntervalSendTimer extends TimerTask {
private final long time; private final long time;
private final int floatTime; private final int floatTime;
private final AtomicBoolean loop = new AtomicBoolean(); private final AtomicBoolean loop = new AtomicBoolean();
private final AtomicBoolean start = new AtomicBoolean(); private final AtomicBoolean running = new AtomicBoolean();
private final String hashId = Integer.toHexString(this.hashCode()); private final String hashId = Integer.toHexString(this.hashCode());
@ -88,7 +87,6 @@ public class RandomIntervalSendTimer extends TimerTask {
this.sender = sender; this.sender = sender;
this.time = time; this.time = time;
this.floatTime = floatTime; this.floatTime = floatTime;
timerMap.put(timerId, this);
if(startNow) { if(startNow) {
start(loop); start(loop);
} }
@ -108,21 +106,18 @@ public class RandomIntervalSendTimer extends TimerTask {
Date nextDate = new Date(); Date nextDate = new Date();
nextDate.setTime(nextDate.getTime() + nextDelay); nextDate.setTime(nextDate.getTime() + nextDelay);
log.info("定时器 {} 下一延迟: {}ms ({})", hashId, nextDelay, nextDate); log.info("定时器 {} 下一延迟: {}ms ({})", hashId, nextDelay, nextDate);
if(start.get()) { if(running.get()) {
try { reset();
Field state = this.getClass().getSuperclass().getDeclaredField("state"); return;
state.setAccessible(true);
state.setInt(this, 0);
state.setAccessible(false);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
return;
}
} }
start.set(true); running.set(true);
timer.schedule(this, nextDelay); timer.schedule(this, nextDelay);
} }
public void reset() {
timerMap.put(timerId, (RandomIntervalSendTimer) clone());
}
@Override @Override
public void run() { public void run() {
log.info("定时器 {} 开始执行...(Sender: {}@{})", this.hashId, sender.getClass().getSimpleName(), sender.hashCode()); log.info("定时器 {} 开始执行...(Sender: {}@{})", this.hashId, sender.getClass().getSimpleName(), sender.hashCode());
@ -145,7 +140,7 @@ public class RandomIntervalSendTimer extends TimerTask {
*/ */
@Override @Override
public boolean cancel() { public boolean cancel() {
start.set(false); running.set(false);
loop.set(false); loop.set(false);
return super.cancel(); return super.cancel();
} }
@ -158,4 +153,18 @@ public class RandomIntervalSendTimer extends TimerTask {
timerMap.remove(this.timerId); timerMap.remove(this.timerId);
} }
/**
* 克隆一个参数完全一样的TimerTask对象.
* @return 返回对象不同, 参数相同的TimerTask对象.
*/
@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
public Object clone() {
RandomIntervalSendTimer newTimerTask = new RandomIntervalSendTimer(
this.timerId, this.sender,
time, floatTime,
running.get(), loop.get());
this.destroy();
return newTimerTask;
}
} }