Commit 952116fc by shentao

2018/4/27 超时控制

parent 66bad2e5
...@@ -378,8 +378,7 @@ public class ES4RedisTask { ...@@ -378,8 +378,7 @@ public class ES4RedisTask {
log.info("{}项目本次获取消息数:{}", project.getProjectName(), num); log.info("{}项目本次获取消息数:{}", project.getProjectName(), num);
} catch (Exception e) { } catch (Exception e) {
log.error("{} {}", e.getMessage(), e.getStackTrace()); log.error("项目本次获取获取出错或超时{}{}", e.getMessage(), e.getStackTrace());
e.printStackTrace();
return false; return false;
} }
return true; return true;
......
...@@ -64,6 +64,18 @@ public class ES4RedisThread extends Thread { ...@@ -64,6 +64,18 @@ public class ES4RedisThread extends Thread {
// 通知执行run方法 // 通知执行run方法
t.start(); t.start();
} }
// 超时控制器
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 超时则线程中止
if (t.isAlive()) {
t.interrupt();
log.warn("{}项目超时线程状态:{}", project.getProjectName(),t.isInterrupted());
}
}
}, 49 * 1000L);
} }
@SuppressWarnings("static-access") @SuppressWarnings("static-access")
...@@ -74,15 +86,15 @@ public class ES4RedisThread extends Thread { ...@@ -74,15 +86,15 @@ public class ES4RedisThread extends Thread {
Thread.sleep(10L); Thread.sleep(10L);
// 超时控制器 // 超时控制器
Timer timer = new Timer(); // Timer timer = new Timer();
timer.schedule(new TimerTask() { // timer.schedule(new TimerTask() {
@Override // @Override
public void run() { // public void run() {
// 超时则线程中止 // // 超时则线程中止
Thread.currentThread().interrupted(); // Thread.currentThread().interrupted();
log.warn("{}项目超时", project.getProjectName()); // log.warn("{}项目超时", project.getProjectName());
} // }
}, 49 * 1000L); // }, 49 * 1000L);
// 程序运行 // 程序运行
log.info("Running {}", threadName); log.info("Running {}", threadName);
...@@ -92,12 +104,13 @@ public class ES4RedisThread extends Thread { ...@@ -92,12 +104,13 @@ public class ES4RedisThread extends Thread {
if (!flag) { if (!flag) {
// 程序执行出现异常则线程中止 // 程序执行出现异常则线程中止
timer.cancel(); // timer.cancel();
Thread.currentThread().interrupted(); Thread.currentThread().interrupted();
log.error("{}项目出现异常", project.getProjectName()); log.error("{}项目出现异常,线程状态:{}", project.getProjectName(),Thread.currentThread().isInterrupted());
} else }
// 程序正常执行完毕,关闭超时控制器 // else
timer.cancel(); // // 程序正常执行完毕,关闭超时控制器
// timer.cancel();
} catch (JsonParseException e) { } catch (JsonParseException e) {
e.printStackTrace(); e.printStackTrace();
......
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