Commit a54e2871 authored by yanzg's avatar yanzg

将源码打包进jar包

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