java动态修改Timer执行频率
程序员文章站
2022-03-23 22:14:12
java动态修改Timer执行频率1、创建TimerTask任务类比集成TimerTask 类public class ServerClient extends TimerTask { private static long period = 0;// 此处为jar包单独程序 所以需要用到main方法 ,可视情况自行触发 public static void main(String[] args) throws Exception { DynamicTaskMa...
java动态修改Timer执行频率
1、创建TimerTask任务类比集成TimerTask 类
public class ServerClient extends TimerTask {
private static long period = 0;
// 此处为jar包单独程序 所以需要用到main方法 ,可视情况自行触发
public static void main(String[] args) throws Exception {
DynamicTaskManager instance = DynamicTaskManager.getInstance();
instance.startTask();
}
@Override
public void run() {
try {
// java业务逻辑代码
// 动态获取period 通过反射方式将period 放进去实现动态修改定时任务执行频率
// 以为小编修改period的逻辑代码,可自行参考
String timerTime = getPropertiesValue("timerTime");
String code = UploadInfoUtil.httpURLGETCase();
if (code != null && !"".equals(code)) {
period = Long.valueOf(code);
setPeriod(period);
} else {
period = Long.valueOf(timerTime.trim());
}
System.out.println(new Date());
} catch (Exception f){
System.out.println(f.getMessage());
}
}
public void setPeriod(long period){
setDeclaredField(TimerTask.class,this,"period",period);
}
/**
* 通过反射修改字段的值
* @param clazz
* @param obj
* @param name
* @param value
* @return
*/
static boolean setDeclaredField(Class<?> clazz, Object obj,String name, Object value) {
try {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
field.set(obj, value);
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
}
2、新建动态Timer管理类
public class DynamicTaskManager {
static {
taskManager = new DynamicTaskManager();
}
/**
* 单例对象
*/
private static DynamicTaskManager taskManager = null;
/**
* 时间调度对象
*/
private static Timer timer = new Timer();
/**
* 任务
*/
private static ServerClient task = null;
/**
* 构建定时任务
* @return
*/
public static DynamicTaskManager getInstance(){
if (taskManager == null){
taskManager = new DynamicTaskManager();
}
return taskManager;
}
/**
* 启动定时器
* jar 监控
* others cpu disk 监控
*/
@SuppressWarnings("unchecked")
public void start(ServerClient task) throws Exception{
// 启动定时任务
startTask();
}
/**
* 构建定时任务
* @return
*/
public ServerClient startTask(){
Date date = new Date();
long period = 1000;
try {
String timerTime = getPropertiesValue("timerTime");
String code = UploadInfoUtil.httpURLGETCase();
if (code != null && !"".equals(code)) {
period = Long.valueOf(code);
} else {
period = Long.valueOf(timerTime.trim());
}
task = new ServerClient();
timer.schedule(task,date,period);
} catch (NumberFormatException e) {
e.getMessage();
}
return task;
}
/**
* 清空timer
* @param task
*/
public void clean(ServerClient task){
if (task != null){
task.cancel();
}
timer.purge();
}
/**
* 停止任务
* @param task
*/
public void stop(ServerClient task){
clean(task);
}
/**
* 根据key获取配置文件中的参数
* @param key
* @return
* @throws FileNotFoundException
*/
public String getPropertiesValue(String key){
Properties props = new Properties();
try {
InputStream inputStream = new BufferedInputStream(new FileInputStream(ReaderConfig.FILE_PATH));
props.load(inputStream);
String values = props.getProperty(key);
return values;
}catch (Exception e){
return e.getMessage();
}
}
}
本文地址:https://blog.csdn.net/qq_20606411/article/details/107667447
上一篇: 空指针异常
下一篇: 垃圾标记阶段算法之可达性分析算法