提交 8b147486 authored 作者: 张航's avatar 张航

修改了TimerTask无法重复使用的问题

上级 1aaa0158
...@@ -4,6 +4,8 @@ import com.comleader.ldmapdownload.util.OperationTypeEnum; ...@@ -4,6 +4,8 @@ import com.comleader.ldmapdownload.util.OperationTypeEnum;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
/** /**
* @ClassName ResultData * @ClassName ResultData
...@@ -77,4 +79,25 @@ public class SocketResultData implements Serializable { ...@@ -77,4 +79,25 @@ public class SocketResultData implements Serializable {
public void setBody(Map<String, Object> body) { public void setBody(Map<String, Object> body) {
this.body = body; this.body = body;
} }
//public static void main(String[] args) throws InterruptedException {
//
// Timer timer = new Timer();
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// System.out.println("timer start");
// }
// }, 0, 1000);
// Thread.sleep(1000);
// timer.cancel();
// timer.cancel();
// timer = new Timer();
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// System.out.println("after cancel");
// }
// }, 1000, 2000);
//}
} }
...@@ -59,6 +59,10 @@ public class DownMapService { ...@@ -59,6 +59,10 @@ public class DownMapService {
public static volatile boolean isBusy = false; // 正在下载 public static volatile boolean isBusy = false; // 正在下载
private Timer scheduleTimer;
private Timer speedTimer;
/** /**
* @param body * @param body
* @param session * @param session
...@@ -111,7 +115,7 @@ public class DownMapService { ...@@ -111,7 +115,7 @@ public class DownMapService {
} }
} }
// 启动一个定时器,每秒刷新下载速度 // 启动一个定时器,每秒刷新下载速度
Timer speedTimer = new Timer(); speedTimer = new Timer();
speedTimer.schedule(new TimerTask() { speedTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
...@@ -123,7 +127,7 @@ public class DownMapService { ...@@ -123,7 +127,7 @@ public class DownMapService {
} }
}, 1000, 1000); }, 1000, 1000);
// 刷新进度 // 刷新进度
Timer scheduleTimer = new Timer(); scheduleTimer = new Timer();
scheduleTimer.schedule(new TimerTask() { scheduleTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
...@@ -149,8 +153,11 @@ public class DownMapService { ...@@ -149,8 +153,11 @@ public class DownMapService {
countSuccessFile++; countSuccessFile++;
System.out.println(result); System.out.println(result);
} }
// 完成标志
this.finished = true;
// 结束定时器 // 结束定时器
speedTimer.cancel(); speedTimer.cancel();
scheduleTimer.cancel();
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
// 封装执行结果 // 封装执行结果
// 失败的个数 // 失败的个数
...@@ -159,8 +166,6 @@ public class DownMapService { ...@@ -159,8 +166,6 @@ public class DownMapService {
resBody.put("totalTime", (end - start) / 1000 + " s"); resBody.put("totalTime", (end - start) / 1000 + " s");
// 总文件大小 // 总文件大小
resBody.put("totalSize", CLStringUtil.getDownTotalSize()); resBody.put("totalSize", CLStringUtil.getDownTotalSize());
// 完成标志
this.finished = true;
log.info("falidNum > "+errResults.size()); log.info("falidNum > "+errResults.size());
log.info("totalTime > "+(end - start) / 1000 + " s"); log.info("totalTime > "+(end - start) / 1000 + " s");
log.info("totalSize > "+CLStringUtil.getDownTotalSize()); log.info("totalSize > "+CLStringUtil.getDownTotalSize());
......
...@@ -44,6 +44,10 @@ public class DownLoadMapWebSocket { ...@@ -44,6 +44,10 @@ public class DownLoadMapWebSocket {
private String name; // 当前连接的客户端的用户名 private String name; // 当前连接的客户端的用户名
private Timer scheduleTimer;
private Timer speedTimer;
/* /*
* @description: websocket无法通过@Autowried注入Service,需要通过如下方式进行解决 * @description: websocket无法通过@Autowried注入Service,需要通过如下方式进行解决
**/ **/
...@@ -166,14 +170,14 @@ public class DownLoadMapWebSocket { ...@@ -166,14 +170,14 @@ public class DownLoadMapWebSocket {
private void downLoadMap(SocketResultData socketData, Session session) throws Exception { private void downLoadMap(SocketResultData socketData, Session session) throws Exception {
// 创建定时任务向前端发送速度\进度 // 创建定时任务向前端发送速度\进度
Timer speedTimer = new Timer(); speedTimer = new Timer();
// 每隔三秒汇报一次 // 每隔三秒汇报一次
speedTimer.schedule(new TimerTask() { speedTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
// 发送了停止命令或者已经完成 // 发送了停止命令或者已经完成
if (DownMapService.stoped || DownMapService.finished) { if (DownMapService.stoped || DownMapService.finished) {
speedTimer.cancel(); speedTimer.purge();
}else { }else {
// 发送速度 // 发送速度
SocketResultData speed = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, "下载速度" + DownMapService.speed + "张/s", null); SocketResultData speed = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, "下载速度" + DownMapService.speed + "张/s", null);
...@@ -182,7 +186,7 @@ public class DownLoadMapWebSocket { ...@@ -182,7 +186,7 @@ public class DownLoadMapWebSocket {
} }
}, 1000, 1000); }, 1000, 1000);
// 创建定时任务向前端发送进度 // 创建定时任务向前端发送进度
Timer scheduleTimer = new Timer(); scheduleTimer = new Timer();
// 每隔0.2秒汇报一次 // 每隔0.2秒汇报一次
scheduleTimer.schedule(new TimerTask() { scheduleTimer.schedule(new TimerTask() {
@Override @Override
......
# 下载地图的保存路径 # 下载地图的保存路径
file.basepath=E:/em-downloadmap file.basepath=G:/em-map
# 项目的发布地址 # 项目的发布地址
file.mapImgPath=E:/em-downloadmap file.mapImgPath=G:/em-map
# 下载地图的地址 # 下载地图的地址
#天地图服务器t0-t8间选一个 #天地图服务器t0-t8间选一个
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论