Commit a54e2871 authored by yanzg's avatar yanzg

将源码打包进jar包

parent a2a010f0
......@@ -11,7 +11,7 @@ import com.yanzuoguang.util.vo.Ref;
*/
public class YzgTimeout {
public static final long TIME_OUT_DEFAULT = 15 * 1000;
public static final int TIME_OUT_DEFAULT = 15 * 1000;
public static final int TIME_OUT_TIP = 10 * 1000;
public static final int TIME_OUT_UNIT = 10;
......@@ -23,19 +23,45 @@ public class YzgTimeout {
* @param runnable 运行函数
*/
public static void timeOut(Class<?> cls, String message, Runnable runnable) {
timeHeart(TIME_OUT_DEFAULT, TIME_OUT_UNIT, TIME_OUT_TIP, runnable, (time) -> {
Log.error(cls, "%s超时,已经执行%d豪秒,正在等待执行完成", message, time);
});
}
/**
* 超时监控
*
* @param runnable 运行函数
* @param heart 心跳函数
*/
public static void timeHeart(Runnable runnable, YzgTimeoutHeart heart) {
timeHeart(1000, 1000, 10, runnable, heart);
}
/**
* 超时监控
*
* @param tipOutDefault 默认超时时间
* @param timeOutTip 超时心跳间隔
* @param tipUnit 监听间隔时间(监听任务完成间隔时间)
* @param runnable 运行函数
* @param heart 心跳函数
*/
public static void timeHeart(int tipOutDefault, int timeOutTip, int tipUnit,
Runnable runnable, YzgTimeoutHeart heart) {
final Ref<Boolean> isRun = new Ref<>(false);
ThreadHelper.runThread(() -> {
try {
long timeMax = TIME_OUT_DEFAULT;
long timeMax = tipOutDefault;
long start = System.currentTimeMillis();
do {
ThreadHelper.sleep(TIME_OUT_UNIT);
long end = System.currentTimeMillis();
long time = end - start;
if (time > timeMax) {
timeMax += TIME_OUT_TIP;
Log.error(cls, "%s超时,已经执行%d豪秒,正在等待执行完成", message, time);
timeMax += timeOutTip;
heart.heart(time);
}
ThreadHelper.sleep(tipUnit);
} while (!isRun.value);
} catch (Exception ex) {
ex.printStackTrace();
......@@ -44,7 +70,9 @@ public class YzgTimeout {
try {
runnable.run();
} finally {
isRun.value = true;
synchronized (isRun) {
isRun.value = true;
}
}
}
}
package com.yanzuoguang.util.helper;
/**
* 超时监控
*
* @author 颜佐光
*/
public interface YzgTimeoutHeart {
/**
* 心跳运行
*
* @param time 已经运行的时间
*/
void heart(long time);
}
......@@ -10,6 +10,9 @@ import java.util.Date;
* @author 颜佐光
*/
public class PlanInfo<T> {
public static final long HEART_MAX = 10 * 1000;
/**
* 编号,判断时间是否满足
*/
......@@ -19,6 +22,10 @@ public class PlanInfo<T> {
* 获取当前时间是否满足,这里记录上次执行时间(用于时间隔天执行对比)
*/
private volatile long time;
/**
* 心跳时间
*/
private volatile long heart;
/**
* 缓存数据
*/
......@@ -64,10 +71,28 @@ public class PlanInfo<T> {
if (newId > this.getId()) {
return true;
} else {
return newId == this.getId() && System.currentTimeMillis() - this.getTime() >= times;
long now = System.currentTimeMillis();
if (now - this.heart < HEART_MAX) {
return false;
}
return newId == this.getId() && now - this.getTime() >= times;
}
}
/**
* 心跳执行函数
*/
public void heart() {
this.heart = System.currentTimeMillis();
}
/**
* 心跳执行完成
*/
public void heartFinish() {
this.heart = 0;
}
/**
* 间隔N天后第N个小时执行
*
......
......@@ -10,6 +10,7 @@ import com.yanzuoguang.redis.mq.PlanProcedure;
import com.yanzuoguang.redis.vo.PlanConfigVo;
import com.yanzuoguang.redis.vo.PlanLevelType;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.helper.YzgTimeout;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
......@@ -78,8 +79,17 @@ public class PlanService {
if (time == null) {
return;
}
// 运行任务
planConfigVo.getPlan().plan();
YzgTimeout.timeHeart(() -> {
// 运行任务
planConfigVo.getPlan().plan();
}, time1 -> {
timeNew.heart();
// 通知完成写入缓存
cachePlan.put(key, time);
});
timeNew.heartFinish();
// 通知完成写入缓存
cachePlan.put(key, time);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment