Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
ldmap-download
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
王健
ldmap-download
Commits
6e6a0286
提交
6e6a0286
authored
4月 15, 2020
作者:
张航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化了线程池的功能(之前资源占用率太高)
上级
785077ca
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
65 行增加
和
65 行删除
+65
-65
SocketResultData.java
...va/com/comleader/ldmapdownload/bean/SocketResultData.java
+2
-20
DownMapService.java
...a/com/comleader/ldmapdownload/service/DownMapService.java
+41
-28
DownLoadMapWebSocket.java
.../comleader/ldmapdownload/socket/DownLoadMapWebSocket.java
+5
-3
CLStringUtil.java
...n/java/com/comleader/ldmapdownload/util/CLStringUtil.java
+15
-12
download-map.properties
src/main/resources/config/download-map.properties
+2
-2
没有找到文件。
src/main/java/com/comleader/ldmapdownload/bean/SocketResultData.java
浏览文件 @
6e6a0286
...
@@ -3,9 +3,8 @@ package com.comleader.ldmapdownload.bean;
...
@@ -3,9 +3,8 @@ package com.comleader.ldmapdownload.bean;
import
com.comleader.ldmapdownload.util.OperationTypeEnum
;
import
com.comleader.ldmapdownload.util.OperationTypeEnum
;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
java.util.Map
;
import
java.util.*
;
import
java.util.Timer
;
import
java.util.concurrent.Future
;
import
java.util.TimerTask
;
/**
/**
* @ClassName ResultData
* @ClassName ResultData
...
@@ -80,21 +79,4 @@ public class SocketResultData implements Serializable {
...
@@ -80,21 +79,4 @@ public class SocketResultData implements Serializable {
this
.
body
=
body
;
this
.
body
=
body
;
}
}
//public static void main(String[] args) throws InterruptedException {
//
// testTask("Task 1");
// testTask("Task 2");
//}
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
();
}
}
}
src/main/java/com/comleader/ldmapdownload/service/DownMapService.java
浏览文件 @
6e6a0286
...
@@ -34,7 +34,7 @@ public class DownMapService {
...
@@ -34,7 +34,7 @@ public class DownMapService {
// 执行任务的线程池
// 执行任务的线程池
private
ExecutorService
es
;
private
ExecutorService
es
;
private
CompletionService
<
String
>
completionService
;
private
CompletionService
<
Integer
>
completionService
;
//private volatile Map<String, String> errResults = new HashMap<>();
//private volatile Map<String, String> errResults = new HashMap<>();
private
List
<
String
>
errResults
=
new
CopyOnWriteArrayList
<>();
private
List
<
String
>
errResults
=
new
CopyOnWriteArrayList
<>();
...
@@ -79,7 +79,7 @@ public class DownMapService {
...
@@ -79,7 +79,7 @@ public class DownMapService {
//记录返回结果
//记录返回结果
Map
<
String
,
Object
>
resBody
=
new
HashMap
<>();
Map
<
String
,
Object
>
resBody
=
new
HashMap
<>();
//记录异步结果
//记录异步结果
List
<
Future
<
String
>>
futures
=
new
ArrayList
<>();
List
<
Future
<
Integer
>>
futures
=
new
ArrayList
<>();
// 记录总用时
// 记录总用时
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
...
@@ -106,20 +106,18 @@ public class DownMapService {
...
@@ -106,20 +106,18 @@ public class DownMapService {
int
minX
=
CLStringUtil
.
getOSMTileXFromLongitude
(
minLng
,
z
);
int
minX
=
CLStringUtil
.
getOSMTileXFromLongitude
(
minLng
,
z
);
int
maxX
=
CLStringUtil
.
getOSMTileXFromLongitude
(
maxLng
,
z
);
int
maxX
=
CLStringUtil
.
getOSMTileXFromLongitude
(
maxLng
,
z
);
for
(
int
x
=
minX
;
x
<=
maxX
;
x
++)
{
// Y轴
for
(
int
x
=
minX
;
x
<=
maxX
;
x
++)
{
// Y轴
for
(
int
y
=
minY
;
y
<=
maxY
;
y
++)
{
// X轴
// 多线程异步执行下载
// 多线程异步执行下载
Future
<
String
>
resultFulture
=
completionService
.
submit
(
new
DownMapCallable
(
z
,
x
,
y
));
Future
<
Integer
>
resultFulture
=
completionService
.
submit
(
new
DownMapCallable
(
z
,
x
,
minY
,
maxY
));
// 加入集合中
// 加入集合中
futures
.
add
(
resultFulture
);
futures
.
add
(
resultFulture
);
}
}
}
}
}
// 启动一个定时器,每秒刷新下载速度
// 启动一个定时器,每秒刷新下载速度
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
||
!
session
.
isOpen
()
)
{
speedTimer
.
cancel
();
speedTimer
.
cancel
();
return
;
return
;
}
}
...
@@ -128,31 +126,37 @@ public class DownMapService {
...
@@ -128,31 +126,37 @@ public class DownMapService {
},
1000
,
1000
);
},
1000
,
1000
);
// 刷新进度
// 刷新进度
scheduleTimer
=
new
Timer
();
scheduleTimer
=
new
Timer
();
StringBuffer
stringBuffer
=
new
StringBuffer
();
scheduleTimer
.
schedule
(
new
TimerTask
()
{
scheduleTimer
.
schedule
(
new
TimerTask
()
{
@Override
@Override
public
void
run
()
{
public
void
run
()
{
if
(
DownMapService
.
stoped
||
DownMapService
.
finished
)
{
if
(
DownMapService
.
stoped
||
DownMapService
.
finished
||
!
session
.
isOpen
()
)
{
scheduleTimer
.
cancel
();
scheduleTimer
.
cancel
();
return
;
return
;
}
}
DownMapService
.
schedule
=
countSuccessFile
*
100
/
readyCountFile
;
// 下载的进度
DownMapService
.
schedule
=
countSuccessFile
*
100
/
readyCountFile
;
// 下载的进度
stringBuffer
.
setLength
(
0
);
// 重新拼接进度
stringBuffer
.
append
(
"\r当前进度:"
+
DownMapService
.
schedule
+
"%\t"
);
for
(
int
i
=
0
;
i
<
DownMapService
.
schedule
;
i
++)
{
stringBuffer
.
append
(
"]"
);
}
stringBuffer
.
append
(
countSuccessFile
+
"/"
+
readyCountFile
);
System
.
out
.
print
(
stringBuffer
);
}
}
},
200
,
200
);
},
200
,
200
);
// 主线程阻塞等待执行完成
// 主线程阻塞等待执行完成
for
(
Future
<
String
>
future
:
futures
)
{
for
(
Future
<
Integer
>
future
:
futures
)
{
if
(
DownMapService
.
stoped
||
!
session
.
isOpen
())
{
if
(
DownMapService
.
stoped
||
!
session
.
isOpen
())
{
DownMapService
.
finished
=
true
;
DownMapService
.
finished
=
true
;
// 如果发布了取消任务,则取消任务
// 如果发布了取消任务,则取消任务
stopDownLoad
(
futures
);
stopDownLoad
(
futures
);
break
;
break
;
}
}
Future
<
String
>
take
=
completionService
.
take
();
Future
<
Integer
>
take
=
completionService
.
take
();
String
result
=
take
.
get
();
Integer
result
=
take
.
get
();
speed
++;
// 累计到下载进度上
countSuccessFile
++;
System
.
out
.
println
(
result
);
}
}
// 完成标志
// 完成标志
DownMapService
.
finished
=
true
;
DownMapService
.
finished
=
true
;
// 结束定时器
// 结束定时器
...
@@ -166,14 +170,18 @@ public class DownMapService {
...
@@ -166,14 +170,18 @@ 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
());
log
.
info
(
"falidNum > "
+
errResults
.
size
());
log
.
info
(
"FalidList > "
);
log
.
info
(
"totalTime > "
+(
end
-
start
)
/
1000
+
" s"
);
for
(
String
errResult
:
errResults
)
{
log
.
info
(
"totalSize > "
+
CLStringUtil
.
getDownTotalSize
());
System
.
out
.
println
(
errResult
+
" DownLoadFaild"
);
}
log
.
info
(
"falidNum > "
+
errResults
.
size
());
log
.
info
(
"totalTime > "
+
(
end
-
start
)
/
1000
+
" s"
);
log
.
info
(
"totalSize > "
+
CLStringUtil
.
getDownTotalSize
());
return
resBody
;
return
resBody
;
}
}
private
void
stopDownLoad
(
List
<
Future
<
String
>>
futures
)
{
private
void
stopDownLoad
(
List
<
Future
<
Integer
>>
futures
)
{
for
(
Future
<
String
>
future
:
futures
)
{
for
(
Future
<
Integer
>
future
:
futures
)
{
if
(!
future
.
isDone
())
{
if
(!
future
.
isDone
())
{
future
.
cancel
(
false
);
future
.
cancel
(
false
);
}
}
...
@@ -221,26 +229,29 @@ public class DownMapService {
...
@@ -221,26 +229,29 @@ public class DownMapService {
* @author: zhanghang
* @author: zhanghang
* @date: 2020/4/7
* @date: 2020/4/7
**/
**/
class
DownMapCallable
implements
Callable
<
String
>
{
class
DownMapCallable
implements
Callable
<
Integer
>,
Cloneable
{
private
int
z
;
private
int
z
;
private
int
x
;
private
int
x
;
private
int
y
;
private
int
minY
;
private
int
maxY
;
public
DownMapCallable
(
int
z
,
int
x
,
int
y
)
{
public
DownMapCallable
(
int
z
,
int
x
,
int
minY
,
int
maxY
)
{
this
.
z
=
z
;
this
.
z
=
z
;
this
.
x
=
x
;
this
.
x
=
x
;
this
.
y
=
y
;
this
.
minY
=
minY
;
this
.
maxY
=
maxY
;
}
}
// 1: 下载完成 0: 地图已经下载过 2: 异常
@Override
@Override
public
String
call
()
throws
Exception
{
public
Integer
call
()
throws
Exception
{
String
imgUrl
=
null
;
String
imgUrl
=
null
;
File
file
=
null
;
File
file
=
null
;
for
(
int
y
=
minY
;
y
<=
maxY
;
y
++)
{
try
{
try
{
if
(
DownMapService
.
stoped
)
{
// 停止下载的命令
if
(
DownMapService
.
stoped
)
{
// 停止下载的命令
DownMapService
.
finished
=
true
;
DownMapService
.
finished
=
true
;
return
imgUrl
+
" Loaded"
;
}
}
//高德地图(6:影像,7:矢量,8:影像路网)
//高德地图(6:影像,7:矢量,8:影像路网)
imgUrl
=
CLStringUtil
.
getImgUrl
(
z
,
x
,
y
);
imgUrl
=
CLStringUtil
.
getImgUrl
(
z
,
x
,
y
);
...
@@ -249,17 +260,19 @@ public class DownMapService {
...
@@ -249,17 +260,19 @@ public class DownMapService {
// 开始下载地图
// 开始下载地图
if
(
file
!=
null
)
{
if
(
file
!=
null
)
{
HttpUtil
.
downImageByGet
(
imgUrl
,
file
);
HttpUtil
.
downImageByGet
(
imgUrl
,
file
);
return
imgUrl
+
" Success"
;
}
}
return
imgUrl
+
" Loaded"
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
if
(
file
!=
null
&&
file
.
exists
())
{
if
(
file
!=
null
&&
file
.
exists
())
{
file
.
delete
();
file
.
delete
();
}
}
errResults
.
add
(
"Failed: "
+
imgUrl
+
" ErrorMsg >> "
+
e
.
getMessage
());
errResults
.
add
(
imgUrl
);
return
imgUrl
+
" Down Failed"
;
log
.
info
(
e
.
getMessage
());
}
speed
++;
// 累计到下载进度上
countSuccessFile
++;
}
}
return
1
;
}
}
}
}
...
...
src/main/java/com/comleader/ldmapdownload/socket/DownLoadMapWebSocket.java
浏览文件 @
6e6a0286
...
@@ -96,6 +96,8 @@ public class DownLoadMapWebSocket {
...
@@ -96,6 +96,8 @@ public class DownLoadMapWebSocket {
@OnClose
@OnClose
public
void
OnClose
()
{
public
void
OnClose
()
{
DownMapService
.
stoped
=
true
;
DownMapService
.
stoped
=
true
;
DownMapService
.
finished
=
true
;
DownMapService
.
isBusy
=
false
;
log
.
info
(
"[WebSocket] 退出成功"
);
log
.
info
(
"[WebSocket] 退出成功"
);
}
}
...
@@ -177,7 +179,7 @@ public class DownLoadMapWebSocket {
...
@@ -177,7 +179,7 @@ public class DownLoadMapWebSocket {
@Override
@Override
public
void
run
()
{
public
void
run
()
{
// 发送了停止命令或者已经完成
// 发送了停止命令或者已经完成
if
(
DownMapService
.
stoped
||
DownMapService
.
finished
)
{
if
(
DownMapService
.
stoped
||
DownMapService
.
finished
||
!
session
.
isOpen
()
)
{
DownMapService
.
isBusy
=
false
;
DownMapService
.
isBusy
=
false
;
SocketResultData
schedule
=
new
SocketResultData
(
OperationTypeEnum
.
DOWNLOAD_SCHEDULE
,
100
+
""
,
null
);
SocketResultData
schedule
=
new
SocketResultData
(
OperationTypeEnum
.
DOWNLOAD_SCHEDULE
,
100
+
""
,
null
);
AppointSending
(
JSONUtil
.
toJsonStr
(
schedule
));
AppointSending
(
JSONUtil
.
toJsonStr
(
schedule
));
...
@@ -195,8 +197,8 @@ public class DownLoadMapWebSocket {
...
@@ -195,8 +197,8 @@ public class DownLoadMapWebSocket {
@Override
@Override
public
void
run
()
{
public
void
run
()
{
// 发送了停止命令或者已经完成
// 发送了停止命令或者已经完成
if
(
DownMapService
.
stoped
||
DownMapService
.
finished
)
{
if
(
DownMapService
.
stoped
||
DownMapService
.
finished
||
!
session
.
isOpen
()
)
{
speedTimer
.
purge
();
speedTimer
.
cancel
();
}
else
{
}
else
{
// 发送速度
// 发送速度
SocketResultData
speed
=
new
SocketResultData
(
OperationTypeEnum
.
SYS_SUCCESS
,
"下载速度"
+
DownMapService
.
speed
+
"张/s"
,
null
);
SocketResultData
speed
=
new
SocketResultData
(
OperationTypeEnum
.
SYS_SUCCESS
,
"下载速度"
+
DownMapService
.
speed
+
"张/s"
,
null
);
...
...
src/main/java/com/comleader/ldmapdownload/util/CLStringUtil.java
浏览文件 @
6e6a0286
...
@@ -147,16 +147,16 @@ public class CLStringUtil {
...
@@ -147,16 +147,16 @@ public class CLStringUtil {
public
static
String
getDownTotalSize
()
{
public
static
String
getDownTotalSize
()
{
double
size
=
HttpUtil
.
totalSize
/
1024.0
;
double
size
=
HttpUtil
.
totalSize
/
1024.0
;
if
(
size
<
1024
)
{
if
(
size
<
1024
)
{
return
new
DecimalFormat
(
"#
,
##"
).
format
(
size
)
+
"K"
;
return
new
DecimalFormat
(
"#
.
##"
).
format
(
size
)
+
"K"
;
}
else
if
(
size
<
1024
*
1024
)
{
}
else
if
(
size
<
1024
*
1024
)
{
size
=
size
/
1024.0
;
size
=
size
/
1024.0
;
return
new
DecimalFormat
(
"#
,
##"
).
format
(
size
)
+
"M"
;
return
new
DecimalFormat
(
"#
.
##"
).
format
(
size
)
+
"M"
;
}
else
if
(
size
<
1024
*
1024
*
1024
)
{
}
else
if
(
size
<
1024
*
1024
*
1024
)
{
size
=
size
/
1024.0
/
1024.0
;
size
=
size
/
1024.0
/
1024.0
;
return
new
DecimalFormat
(
"#
,
##"
).
format
(
size
)
+
"G"
;
return
new
DecimalFormat
(
"#
.
##"
).
format
(
size
)
+
"G"
;
}
else
{
}
else
{
size
=
size
/
1024.0
/
1024.0
/
1024.0
;
size
=
size
/
1024.0
/
1024.0
/
1024.0
;
return
new
DecimalFormat
(
"#
,
##"
).
format
(
size
)
+
"T"
;
return
new
DecimalFormat
(
"#
.
##"
).
format
(
size
)
+
"T"
;
}
}
}
}
...
@@ -196,15 +196,18 @@ public class CLStringUtil {
...
@@ -196,15 +196,18 @@ public class CLStringUtil {
**/
**/
public
static
String
countFileSize
(
int
countFileNum
)
{
public
static
String
countFileSize
(
int
countFileNum
)
{
// 平均每个图片20K
// 平均每个图片20K
long
countSize
=
countFileNum
*
20
;
double
size
=
countFileNum
*
20
;
if
(
countSize
<
1024
)
{
if
(
size
<
1024
)
{
return
countSize
+
"K"
;
return
new
DecimalFormat
(
"#.##"
).
format
(
size
)
+
"K"
;
}
else
if
(
countSize
<
1024
*
1024
)
{
}
else
if
(
size
<
1024
*
1024
)
{
countSize
=
countSize
/
1024
;
size
=
size
/
1024.0
;
return
countSize
+
"M"
;
return
new
DecimalFormat
(
"#.##"
).
format
(
size
)
+
"M"
;
}
else
if
(
size
<
1024
*
1024
*
1024
)
{
size
=
size
/
1024.0
/
1024.0
;
return
new
DecimalFormat
(
"#.##"
).
format
(
size
)
+
"G"
;
}
else
{
}
else
{
countSize
=
countSize
/
1024
/
1024
;
size
=
size
/
1024.0
/
1024.0
/
1024.0
;
return
countSize
+
"G
"
;
return
new
DecimalFormat
(
"#.##"
).
format
(
size
)
+
"T
"
;
}
}
}
}
...
...
src/main/resources/config/download-map.properties
浏览文件 @
6e6a0286
# 下载地图的保存路径
# 下载地图的保存路径
file.basepath
=
G
:/em-map
file.basepath
=
H
:/em-map
# 项目的发布地址
# 项目的发布地址
file.mapImgPath
=
G
:/em-map
file.mapImgPath
=
H
:/em-map
# 下载地图的地址
# 下载地图的地址
#天地图服务器t0-t8间选一个
#天地图服务器t0-t8间选一个
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论