标签:轮询 一段时间 System new result sid 退出 scheduledExecutorService wsLinkViewResp
一、循环 + Thread.sleep()
long startTime = System.currentTimeMillis();
do {
System.out.println("run "+ System.currentTimeMillis());
WsLinkViewResp wsLinkViewResp = viewLinkService
.checkInspectionStatus((WsLinkViewReq) object).getData();
sendMessage(sid, wsLinkViewResp.toString());
if (wsLinkViewResp.getFinishedNum().equals(wsLinkViewResp.getSumNum())) {
closeWithReason(sid, "所有作业已执行完成");
}
try {
Thread.sleep(1000);
}
catch (Exception e) {
e.printStackTrace();
}
} while (System.currentTimeMillis() - startTime < CHECK_TIME);
二、ScheduledExecutorService每隔一段时间执行一个任务
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1,
new ThreadFactoryBuilder().setNameFormat("check-inspection-status-scheduler-%d").build());
AtomicLong waitTime = new AtomicLong(0);
try {
while (waitTime.get() < CHECK_TIME) {
ScheduledFuture<WsLinkViewResp> future = scheduledExecutorService.schedule(() -> {
System.out.println(object.toString());
WsLinkViewResp wsLinkViewResp = viewLinkService
.checkInspectionStatus((WsLinkViewReq) object).getData();
System.out.println(wsLinkViewResp.toString());
waitTime.addAndGet(PERIOD);
return wsLinkViewResp;
}, PERIOD, TimeUnit.MILLISECONDS);
WsLinkViewResp result = future.get();
if (result.getFinishedNum().equals(result.getSumNum())) {
close(sid, "所有作业已执行完成");
return;
}
else {
sendResp(sid, result);
}
}
close(sid, "执行时间已超过五分钟,请合理配置环节视图或稍后查看相关作业状态!");
} catch (ExecutionException | InterruptedException e) {
close(sid, "服务端发生中断异常");
Thread.currentThread().interrupt();
}
finally {
scheduledExecutorService.shutdown();
}
尝试历史
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1,
new ThreadFactoryBuilder().setNameFormat("check-inspection-status-scheduler-%d").build());
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("gghjghjghgjgjghj");
WsLinkViewResp wsLinkViewResp = viewLinkService
.checkInspectionStatus((WsLinkViewReq) object).getData();
sendMessage(sid, "dsgdgdfsggsg");
if (wsLinkViewResp.getFinishedNum().equals(wsLinkViewResp.getSumNum())) {
scheduledExecutorService.shutdown();
}
}
}, 0, PERIOD, TimeUnit.MILLISECONDS);
try {
Thread.sleep(CHECK_TIME);
}
catch (Exception e){
e.printStackTrace();
}
scheduledExecutorService.shutdownNow();
long startTime = System.currentTimeMillis();
do {
System.out.println("run "+ System.currentTimeMillis());
WsLinkViewResp wsLinkViewResp = viewLinkService
.checkInspectionStatus((WsLinkViewReq) object).getData();
sendMessage(sid, wsLinkViewResp.toString());
if (wsLinkViewResp.getFinishedNum().equals(wsLinkViewResp.getSumNum())) {
closeWithReason(sid, "所有作业已执行完成");
}
try {
Thread.sleep(1000);
}
catch (Exception e) {
e.printStackTrace();
}
} while (System.currentTimeMillis() - startTime < CHECK_TIME);
IViewLinkService viewLinkService = SpringUtils.getBean(ViewLinkServiceImpl.class);
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1,
new ThreadFactoryBuilder().setNameFormat("check-inspection-status-scheduler-%d").build());
AtomicLong waitTime = new AtomicLong(0);
try {
while (waitTime.get() < CHECK_TIME) {
ScheduledFuture<WsLinkViewResp> future = scheduledExecutorService.schedule(() -> {
System.out.println(object.toString());
WsLinkViewResp wsLinkViewResp = viewLinkService
.checkInspectionStatus((WsLinkViewReq) object).getData();
System.out.println(wsLinkViewResp.toString());
waitTime.addAndGet(PERIOD);
return wsLinkViewResp;
}, PERIOD, TimeUnit.MILLISECONDS);
WsLinkViewResp result = future.get();
if (result.getFinishedNum().equals(result.getSumNum())) {
close(sid, "所有作业已执行完成");
return;
}
else {
sendResp(sid, result);
}
}
close(sid, "执行时间已超过五分钟,请合理配置环节视图或稍后查看相关作业状态!");
} catch (ExecutionException | InterruptedException e) {
close(sid, "服务端发生中断异常");
Thread.currentThread().interrupt();
}
finally {
scheduledExecutorService.shutdown();
}
标签:轮询,一段时间,System,new,result,sid,退出,scheduledExecutorService,wsLinkViewResp 来源: https://www.cnblogs.com/acelin/p/16656924.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。