提交 785077ca authored 作者: 张航's avatar 张航

修复了Socket连接占用的问题(数据只能一条一条发送,不能两条同时发送)

上级 8b147486
...@@ -82,22 +82,19 @@ public class SocketResultData implements Serializable { ...@@ -82,22 +82,19 @@ public class SocketResultData implements Serializable {
//public static void main(String[] args) throws InterruptedException { //public static void main(String[] args) throws InterruptedException {
// //
// Timer timer = new Timer(); // testTask("Task 1");
// timer.schedule(new TimerTask() { // testTask("Task 2");
// @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);
//} //}
private static void testTask(String name) throws InterruptedException {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println(name);
}
}, 0, 500);
Thread.sleep(2000);
timer.cancel();
}
} }
...@@ -154,7 +154,7 @@ public class DownMapService { ...@@ -154,7 +154,7 @@ public class DownMapService {
System.out.println(result); System.out.println(result);
} }
// 完成标志 // 完成标志
this.finished = true; DownMapService.finished = true;
// 结束定时器 // 结束定时器
speedTimer.cancel(); speedTimer.cancel();
scheduleTimer.cancel(); scheduleTimer.cancel();
......
...@@ -170,21 +170,6 @@ public class DownLoadMapWebSocket { ...@@ -170,21 +170,6 @@ public class DownLoadMapWebSocket {
private void downLoadMap(SocketResultData socketData, Session session) throws Exception { private void downLoadMap(SocketResultData socketData, Session session) throws Exception {
// 创建定时任务向前端发送速度\进度 // 创建定时任务向前端发送速度\进度
speedTimer = new Timer();
// 每隔三秒汇报一次
speedTimer.schedule(new TimerTask() {
@Override
public void run() {
// 发送了停止命令或者已经完成
if (DownMapService.stoped || DownMapService.finished) {
speedTimer.purge();
}else {
// 发送速度
SocketResultData speed = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, "下载速度" + DownMapService.speed + "张/s", null);
AppointSending(JSONUtil.toJsonStr(speed));
}
}
}, 1000, 1000);
// 创建定时任务向前端发送进度 // 创建定时任务向前端发送进度
scheduleTimer = new Timer(); scheduleTimer = new Timer();
// 每隔0.2秒汇报一次 // 每隔0.2秒汇报一次
...@@ -203,7 +188,23 @@ public class DownLoadMapWebSocket { ...@@ -203,7 +188,23 @@ public class DownLoadMapWebSocket {
AppointSending(JSONUtil.toJsonStr(schedule)); AppointSending(JSONUtil.toJsonStr(schedule));
} }
} }
}, 200, 200); }, 1000, 1000);
speedTimer = new Timer();
// 每隔三秒汇报一次
speedTimer.schedule(new TimerTask() {
@Override
public void run() {
// 发送了停止命令或者已经完成
if (DownMapService.stoped || DownMapService.finished) {
speedTimer.purge();
}else {
// 发送速度
SocketResultData speed = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, "下载速度" + DownMapService.speed + "张/s", null);
AppointSending(JSONUtil.toJsonStr(speed));
}
}
}, 2000, 2000);
// 发送执行结果(这里是异步阻塞的,会等待downLoad执行完成) // 发送执行结果(这里是异步阻塞的,会等待downLoad执行完成)
Map<String, Object> resBody = downMapService.downLoad(socketData.getBody(), session); Map<String, Object> resBody = downMapService.downLoad(socketData.getBody(), session);
...@@ -211,6 +212,7 @@ public class DownLoadMapWebSocket { ...@@ -211,6 +212,7 @@ public class DownLoadMapWebSocket {
",总用时" + resBody.get("totalTime") + ",失败个数" + resBody.get("falidNum") + "(可选择非覆盖重新下载!)"; ",总用时" + resBody.get("totalTime") + ",失败个数" + resBody.get("falidNum") + "(可选择非覆盖重新下载!)";
SocketResultData resultData = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, msg, null); SocketResultData resultData = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, msg, null);
AppointSending(JSONUtil.toJsonStr(resultData)); AppointSending(JSONUtil.toJsonStr(resultData));
DownMapService.isBusy = false;
// 通知下载完成 // 通知下载完成
AppointSending(JSONUtil.toJsonStr(new SocketResultData(OperationTypeEnum.DOWNLOAD_FINISHED))); AppointSending(JSONUtil.toJsonStr(new SocketResultData(OperationTypeEnum.DOWNLOAD_FINISHED)));
} }
...@@ -235,8 +237,10 @@ public class DownLoadMapWebSocket { ...@@ -235,8 +237,10 @@ public class DownLoadMapWebSocket {
**/ **/
private void AppointSending(String message) { private void AppointSending(String message) {
try { try {
synchronized (session){
session.getBasicRemote().sendText(message); session.getBasicRemote().sendText(message);
} catch (IOException e) { }
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
......
...@@ -7,6 +7,6 @@ http.connectTimeout=60000 ...@@ -7,6 +7,6 @@ http.connectTimeout=60000
#从连接池中获取到连接的最长时间 #从连接池中获取到连接的最长时间
http.connectionRequestTimeout=6000000 http.connectionRequestTimeout=6000000
#数据传输的最长时间 #数据传输的最长时间
http.socketTimeout=12000000 http.socketTimeout=1200000
#提交请求前测试连接是否可用 #提交请求前测试连接是否可用
http.staleConnectionCheckEnabled=true http.staleConnectionCheckEnabled=true
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"> <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>EM MapDownLoader</title> <title>GM MapDownLoader</title>
<!-- jquery --> <!-- jquery -->
<script src="thirdLib/jq/jquery-3.3.1.min.js"></script> <script src="thirdLib/jq/jquery-3.3.1.min.js"></script>
...@@ -135,15 +135,16 @@ ...@@ -135,15 +135,16 @@
if (result.type == 3) { // 更新下载进度 if (result.type == 3) { // 更新下载进度
var schedule = result.message; var schedule = result.message;
console.log(schedule);
if (schedule > 100){ if (schedule > 100){
schedule = 100; schedule = 99;
} }
console.log(schedule);
$("#progressBar").width(schedule + "%"); $("#progressBar").width(schedule + "%");
$("#progressBar").html(schedule + "%"); $("#progressBar").html(schedule + "%");
} }
if (result.type == 4) { // 下载完成,隐藏进度条 if (result.type == 4) { // 下载完成,隐藏进度条
// $("#progressDiv").hide(); $("#progressBar").width(100 + "%");
$("#progressBar").html(100 + "%");
} }
if (result.type == 6) { // 连接信息 if (result.type == 6) { // 连接信息
$("#filePath").val(result.message); $("#filePath").val(result.message);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论