Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
di
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
续晓东
di
Commits
5fa436dc
Commit
5fa436dc
authored
Apr 02, 2020
by
Xuxiaodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加godas中下载ftp文件的业务逻辑代码
parent
27084400
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
31 deletions
+111
-31
PullFileTask.java
src/main/java/com/jianju/di/task/PullFileTask.java
+107
-29
application.yml
src/main/resources/application.yml
+4
-2
No files found.
src/main/java/com/jianju/di/task/PullFileTask.java
View file @
5fa436dc
...
...
@@ -3,12 +3,10 @@ package com.jianju.di.task;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.util.TypeUtils
;
import
com.fasterxml.jackson.databind.util.JSONPObject
;
//import com.jianju.di.Manager.PostGISMannager;
import
com.jianju.di.Manager.PostGISMannager
;
import
com.jianju.di.entity.Di
;
import
com.jianju.di.utils.FtpUtil
;
import
lombok.Data
;
import
lombok.extern.slf4j.Slf4j
;
import
okhttp3.*
;
import
org.springframework.beans.factory.annotation.Value
;
...
...
@@ -16,11 +14,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PostConstruct
;
import
java.io.IOException
;
import
java.sql.SQLException
;
import
java.text.DateFormat
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Calendar
;
...
...
@@ -45,11 +39,14 @@ public class PullFileTask {
private
String
userName
;
@Value
(
"${ftp.password}"
)
private
String
password
;
@Value
(
"${ftp.remotepath}"
)
private
String
remotePath
;
@Value
(
"${ftp.localpath}"
)
private
String
localPath
;
@Value
(
"${ftp.olrremotepath}"
)
private
String
olrRemotePath
;
@Value
(
"${ftp.godasremotepath"
)
private
String
godasRemotePath
;
/**
* 数据库注入参数
*/
...
...
@@ -63,37 +60,110 @@ public class PullFileTask {
private
String
dbPassword
;
@Value
(
"${postgis.dbname}"
)
private
String
dbName
;
@Value
(
"${postgis.tbname}"
)
private
String
tbName
;
@Value
(
"${postgis.olrtbname}"
)
private
String
olrTbName
;
@Value
(
"${postgis.godastbname}"
)
private
String
godasTbName
;
private
static
final
SimpleDateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyyMMdd"
);
private
static
final
SimpleDateFormat
yearDateFormat
=
new
SimpleDateFormat
(
"yyyy"
);
private
final
FtpUtil
.
FtpConfig
ftpConfig
=
new
FtpUtil
.
FtpConfig
().
setHost
(
host
).
setPort
(
port
).
setUserName
(
userName
).
setPassword
(
password
);
//每隔五天执行一次;
@Scheduled
(
cron
=
"0 0 0 0/5 * ?"
)
public
void
pullGodasFileAndSendDi
()
throws
Exception
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
new
Date
());
// -2 为ftp上传文件延迟的时间
c
.
add
(
Calendar
.
DATE
,
-
2
);
String
year
;
String
fileName
=
null
;
long
beforeDown
=
0
;
long
afterDown
=
0
;
boolean
download
=
false
;
for
(
int
i
=
0
;
i
<=
5
;
i
++)
{
fileName
=
"godas.P."
+
dateFormat
.
format
(
c
.
getTime
())+
".grb"
;
year
=
yearDateFormat
.
format
(
c
.
getTime
());
beforeDown
=
System
.
currentTimeMillis
();
download
=
FtpUtil
.
download
(
ftpConfig
,
godasRemotePath
+
year
+
"/"
,
fileName
,
localPath
,
fileName
);
if
(
download
)
{
log
.
info
(
"下载成功"
+
fileName
);
break
;
}
c
.
add
(
Calendar
.
DATE
,
-
1
);
}
if
(
download
)
{
PostGISMannager
postGIS
=
new
PostGISMannager
(
dbHost
,
dbPort
,
dbName
,
godasTbName
,
dbUserName
,
dbPassword
);
postGIS
.
createTableGDAS
(
godasTbName
);
if
(
postGIS
.
insertGodasToPG
(
godasTbName
,
localPath
+
fileName
)>
0
){
// 发送Di -----------暂时修改到了这里 2020年4月2日19点02分
afterDown
=
System
.
currentTimeMillis
();
Date
start
=
new
Date
(
beforeDown
);
String
diJson
=
sendDi
(
c
.
getTime
(),
new
Date
(
beforeDown
),
new
Date
(
afterDown
),
fileName
,
"1"
);
OkHttpClient
httpClient
=
new
OkHttpClient
();
RequestBody
requestBody
=
RequestBody
.
create
(
MediaType
.
parse
(
"application/json; charset=utf-8"
),
diJson
);
Request
request
=
new
Request
.
Builder
()
.
url
(
"http://127.0.0.1:29999/transfer"
)
.
post
(
requestBody
)
.
build
();
//发送请求获取响应
try
{
Response
response
=
httpClient
.
newCall
(
request
).
execute
();
//判断请求是否成功
if
(
response
.
isSuccessful
()){
//打印服务端返回结果
log
.
info
(
"请求代理返回结果==========:"
+
response
.
body
().
string
());
log
.
info
(
"请求di的Json========="
+
diJson
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
else
{
log
.
info
(
"下载失败"
+
fileName
);
}
}
//每日2
3点
执行;
//每日2
0:10
执行;
@Scheduled
(
cron
=
"0 10 20 * * ?"
)
//@Scheduled(fixedRate = 10000)
//@PostConstruct
public
void
PullFileAndSendDi
()
throws
Exception
{
FtpUtil
.
FtpConfig
ftpConfig
=
new
FtpUtil
.
FtpConfig
().
setHost
(
host
).
setPort
(
port
).
setUserName
(
userName
).
setPassword
(
password
);
public
void
pullOlrFileAndSendDi
()
throws
Exception
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
new
Date
());
c
.
add
(
Calendar
.
DATE
,
-
2
);
String
fileName
=
"#"
+
dateFormat
.
format
(
c
.
getTime
());
log
.
info
(
"开始下载----"
+
fileName
);
long
beforeDown
=
System
.
currentTimeMillis
();
boolean
download
=
FtpUtil
.
download
(
ftpConfig
,
r
emotePath
,
fileName
,
localPath
,
fileName
);
boolean
download
=
FtpUtil
.
download
(
ftpConfig
,
olrR
emotePath
,
fileName
,
localPath
,
fileName
);
long
afterDown
;
if
(
download
)
{
log
.
info
(
"下载成功"
+
fileName
);
PostGISMannager
postGIS
=
new
PostGISMannager
(
dbHost
,
dbPort
,
dbName
,
t
bName
,
dbUserName
,
dbPassword
);
postGIS
.
createOlrDay
(
t
bName
);
dbName
,
olrT
bName
,
dbUserName
,
dbPassword
);
postGIS
.
createOlrDay
(
olrT
bName
);
if
(
postGIS
.
insertOlrToPG
(
t
bName
,
localPath
+
fileName
)>
0
){
if
(
postGIS
.
insertOlrToPG
(
olrT
bName
,
localPath
+
fileName
)>
0
){
// 发送Di
afterDown
=
System
.
currentTimeMillis
();
Date
start
=
new
Date
(
beforeDown
);
...
...
@@ -136,15 +206,15 @@ public class PullFileTask {
di
.
setName
(
"OLR数据入库"
);
di
.
setOccur_time
(
System
.
currentTimeMillis
());
di
.
setMessage
(
"文件级"
);
di
.
setType
(
"RT.
CTS
.STATION.DI"
);
di
.
setType
(
"RT.
DPC
.STATION.DI"
);
fields
.
setDATA_TYPE
(
"K.0436.0001.9999"
);
fields
.
setDATA_TYPE_1
(
"K.0436.0001.9999"
);
fields
.
setRECEIVE
(
"CTS"
);
fields
.
setSEND
(
"
userid001
"
);
fields
.
setSEND
(
"
STDB
"
);
fields
.
setTRAN_TIME
(
new
Date
());
fields
.
setDATA_TIME
(
DATA_TIME
);
//文件名时间
fields
.
setDATA_FLOW
(
"BDMAIN"
);
fields
.
setSYSTEM
(
"
SDO
"
);
fields
.
setSYSTEM
(
"
DPC
"
);
fields
.
setPROCESS_LINK
(
"1"
);
fields
.
setPROCESS_START_TIME
(
PROCESS_START_TIME
);
//下载数据之前记录的时间
fields
.
setPROCESS_END_TIME
(
PROCESS_END_TIME
);
//入库完成时间 或者失败的时间
...
...
@@ -161,23 +231,27 @@ public class PullFileTask {
}
@PostConstruct
/**
* 对定时任务之前缺失的数据入库,但不发送di
* @throws Exception
*/
// @PostConstruct
public
void
a
()
throws
Exception
{
FtpUtil
.
FtpConfig
ftpConfig
=
new
FtpUtil
.
FtpConfig
().
setHost
(
host
).
setPort
(
port
).
setUserName
(
userName
).
setPassword
(
password
);
Date
dateStart
=
dateFormat
.
parse
(
"20200325"
);
Date
dateEnd
=
dateFormat
.
parse
(
"20200325"
);
Calendar
c
=
Calendar
.
getInstance
();
PostGISMannager
postGIS
=
new
PostGISMannager
(
dbHost
,
dbPort
,
dbName
,
t
bName
,
dbUserName
,
dbPassword
);
dbName
,
olrT
bName
,
dbUserName
,
dbPassword
);
while
(
dateStart
.
compareTo
(
dateEnd
)<=
0
)
{
String
fileName
=
"#"
+
dateFormat
.
format
(
dateStart
.
getTime
());
log
.
info
(
"开始下载----"
+
fileName
);
boolean
download
=
FtpUtil
.
download
(
ftpConfig
,
r
emotePath
,
fileName
,
localPath
,
fileName
);
boolean
download
=
FtpUtil
.
download
(
ftpConfig
,
olrR
emotePath
,
fileName
,
localPath
,
fileName
);
if
(
download
)
{
log
.
info
(
"下载成功-----"
+
fileName
);
postGIS
.
createOlrDay
(
t
bName
);
if
(
postGIS
.
insertOlrToPG
(
t
bName
,
localPath
+
fileName
)
>
0
)
{
postGIS
.
createOlrDay
(
olrT
bName
);
if
(
postGIS
.
insertOlrToPG
(
olrT
bName
,
localPath
+
fileName
)
>
0
)
{
log
.
info
(
fileName
+
"=======入库成功"
);
c
.
setTime
(
dateStart
);
c
.
add
(
c
.
DATE
,
1
);
//把dateBegin加上1天然后重新赋值给date1
...
...
@@ -194,12 +268,16 @@ public class PullFileTask {
}
}
// @PostConstruct
public
void
b
()
public
void
testDownload
()
{
FtpUtil
.
FtpConfig
ftpConfig
=
new
FtpUtil
.
FtpConfig
().
setHost
(
host
).
setPort
(
port
).
setUserName
(
userName
).
setPassword
(
password
);
boolean
download
=
FtpUtil
.
download
(
ftpConfig
,
remotePath
,
"#20200324"
,
localPath
,
"#20200324"
);
System
.
out
.
println
(
download
+
"asdasd"
);
boolean
download
=
FtpUtil
.
download
(
ftpConfig
,
olrRemotePath
,
"#20200324"
,
localPath
,
"#20200324"
);
System
.
out
.
println
(
download
);
}
public
static
void
main
(
String
[]
args
)
{
}
...
...
src/main/resources/application.yml
View file @
5fa436dc
...
...
@@ -7,7 +7,8 @@ ftp:
port
:
21
username
:
ncc
password
:
ncc123
remotepath
:
/NOAA/NCEP/noaa18_olr/
olrremotepath
:
/NOAA/NCEP/noaa18_olr/
godasremotepath
:
/NOAA/NCEP/GODAS/pentad/
localpath
:
/home/jianju/di/tempdata
postgis
:
host
:
10.40.16.38
...
...
@@ -15,7 +16,8 @@ postgis:
username
:
postgres
password
:
jianju@123@pg
dbname
:
test
tbname
:
noaadata
olrtbname
:
noaadata
godastbname
:
待定
logging
:
file
:
path
:
/home/jianju/di/logs
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment