Commit 5fa436dc authored by Xuxiaodong's avatar Xuxiaodong

添加godas中下载ftp文件的业务逻辑代码

parent 27084400
...@@ -3,12 +3,10 @@ package com.jianju.di.task; ...@@ -3,12 +3,10 @@ package com.jianju.di.task;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils; 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.Manager.PostGISMannager; import com.jianju.di.Manager.PostGISMannager;
import com.jianju.di.entity.Di; import com.jianju.di.entity.Di;
import com.jianju.di.utils.FtpUtil; import com.jianju.di.utils.FtpUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.*; import okhttp3.*;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -16,11 +14,7 @@ import org.springframework.scheduling.annotation.Scheduled; ...@@ -16,11 +14,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
...@@ -45,11 +39,14 @@ public class PullFileTask { ...@@ -45,11 +39,14 @@ public class PullFileTask {
private String userName; private String userName;
@Value("${ftp.password}") @Value("${ftp.password}")
private String password; private String password;
@Value("${ftp.remotepath}")
private String remotePath;
@Value("${ftp.localpath}") @Value("${ftp.localpath}")
private String localPath; private String localPath;
@Value("${ftp.olrremotepath}")
private String olrRemotePath;
@Value("${ftp.godasremotepath")
private String godasRemotePath;
/** /**
* 数据库注入参数 * 数据库注入参数
*/ */
...@@ -63,37 +60,110 @@ public class PullFileTask { ...@@ -63,37 +60,110 @@ public class PullFileTask {
private String dbPassword; private String dbPassword;
@Value("${postgis.dbname}") @Value("${postgis.dbname}")
private String 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 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");
//每日23点执行; 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);
}
}
//每日20:10执行;
@Scheduled(cron ="0 10 20 * * ?" ) @Scheduled(cron ="0 10 20 * * ?" )
//@Scheduled(fixedRate = 10000) //@Scheduled(fixedRate = 10000)
//@PostConstruct //@PostConstruct
public void PullFileAndSendDi() throws Exception { public void pullOlrFileAndSendDi() throws Exception {
FtpUtil.FtpConfig ftpConfig = new FtpUtil.FtpConfig().setHost(host).setPort(port).setUserName(userName).setPassword(password);
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
c.setTime(new Date()); c.setTime(new Date());
c.add(Calendar.DATE, -2); c.add(Calendar.DATE, -2);
String fileName = "#"+dateFormat.format(c.getTime()); String fileName = "#"+dateFormat.format(c.getTime());
log.info("开始下载----"+fileName); log.info("开始下载----"+fileName);
long beforeDown = System.currentTimeMillis(); long beforeDown = System.currentTimeMillis();
boolean download = FtpUtil.download(ftpConfig, remotePath, fileName, localPath, fileName); boolean download = FtpUtil.download(ftpConfig, olrRemotePath, fileName, localPath, fileName);
long afterDown; long afterDown;
if(download) if(download)
{ {
log.info("下载成功"+fileName); log.info("下载成功"+fileName);
PostGISMannager postGIS = new PostGISMannager(dbHost, dbPort, PostGISMannager postGIS = new PostGISMannager(dbHost, dbPort,
dbName, tbName, dbUserName, dbPassword); dbName, olrTbName, dbUserName, dbPassword);
postGIS.createOlrDay(tbName); postGIS.createOlrDay(olrTbName);
if(postGIS.insertOlrToPG(tbName, localPath + fileName)>0){ if(postGIS.insertOlrToPG(olrTbName, localPath + fileName)>0){
// 发送Di // 发送Di
afterDown = System.currentTimeMillis(); afterDown = System.currentTimeMillis();
Date start = new Date(beforeDown); Date start = new Date(beforeDown);
...@@ -136,15 +206,15 @@ public class PullFileTask { ...@@ -136,15 +206,15 @@ public class PullFileTask {
di.setName("OLR数据入库"); di.setName("OLR数据入库");
di.setOccur_time(System.currentTimeMillis()); di.setOccur_time(System.currentTimeMillis());
di.setMessage("文件级"); di.setMessage("文件级");
di.setType("RT.CTS.STATION.DI"); di.setType("RT.DPC.STATION.DI");
fields.setDATA_TYPE("K.0436.0001.9999"); fields.setDATA_TYPE("K.0436.0001.9999");
fields.setDATA_TYPE_1("K.0436.0001.9999"); fields.setDATA_TYPE_1("K.0436.0001.9999");
fields.setRECEIVE("CTS"); fields.setRECEIVE("CTS");
fields.setSEND("userid001"); fields.setSEND("STDB");
fields.setTRAN_TIME(new Date()); fields.setTRAN_TIME(new Date());
fields.setDATA_TIME(DATA_TIME); //文件名时间 fields.setDATA_TIME(DATA_TIME); //文件名时间
fields.setDATA_FLOW("BDMAIN"); fields.setDATA_FLOW("BDMAIN");
fields.setSYSTEM("SDO"); fields.setSYSTEM("DPC");
fields.setPROCESS_LINK("1"); fields.setPROCESS_LINK("1");
fields.setPROCESS_START_TIME(PROCESS_START_TIME); //下载数据之前记录的时间 fields.setPROCESS_START_TIME(PROCESS_START_TIME); //下载数据之前记录的时间
fields.setPROCESS_END_TIME(PROCESS_END_TIME); //入库完成时间 或者失败的时间 fields.setPROCESS_END_TIME(PROCESS_END_TIME); //入库完成时间 或者失败的时间
...@@ -161,23 +231,27 @@ public class PullFileTask { ...@@ -161,23 +231,27 @@ public class PullFileTask {
} }
@PostConstruct /**
* 对定时任务之前缺失的数据入库,但不发送di
* @throws Exception
*/
// @PostConstruct
public void a() throws Exception { public void a() throws Exception {
FtpUtil.FtpConfig ftpConfig = new FtpUtil.FtpConfig().setHost(host).setPort(port).setUserName(userName).setPassword(password); FtpUtil.FtpConfig ftpConfig = new FtpUtil.FtpConfig().setHost(host).setPort(port).setUserName(userName).setPassword(password);
Date dateStart = dateFormat.parse("20200325"); Date dateStart = dateFormat.parse("20200325");
Date dateEnd = dateFormat.parse("20200325"); Date dateEnd = dateFormat.parse("20200325");
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
PostGISMannager postGIS = new PostGISMannager(dbHost, dbPort, PostGISMannager postGIS = new PostGISMannager(dbHost, dbPort,
dbName, tbName, dbUserName, dbPassword); dbName, olrTbName, dbUserName, dbPassword);
while (dateStart.compareTo(dateEnd)<=0) { while (dateStart.compareTo(dateEnd)<=0) {
String fileName = "#" + dateFormat.format(dateStart.getTime()); String fileName = "#" + dateFormat.format(dateStart.getTime());
log.info("开始下载----" + fileName); log.info("开始下载----" + fileName);
boolean download = FtpUtil.download(ftpConfig, remotePath, fileName, localPath, fileName); boolean download = FtpUtil.download(ftpConfig, olrRemotePath, fileName, localPath, fileName);
if (download) { if (download) {
log.info("下载成功-----"+ fileName); log.info("下载成功-----"+ fileName);
postGIS.createOlrDay(tbName); postGIS.createOlrDay(olrTbName);
if (postGIS.insertOlrToPG(tbName, localPath + fileName) > 0) { if (postGIS.insertOlrToPG(olrTbName, localPath + fileName) > 0) {
log.info(fileName + "=======入库成功"); log.info(fileName + "=======入库成功");
c.setTime(dateStart); c.setTime(dateStart);
c.add(c.DATE, 1);//把dateBegin加上1天然后重新赋值给date1 c.add(c.DATE, 1);//把dateBegin加上1天然后重新赋值给date1
...@@ -194,12 +268,16 @@ public class PullFileTask { ...@@ -194,12 +268,16 @@ public class PullFileTask {
} }
} }
// @PostConstruct // @PostConstruct
public void b() public void testDownload()
{ {
FtpUtil.FtpConfig ftpConfig = new FtpUtil.FtpConfig().setHost(host).setPort(port).setUserName(userName).setPassword(password); FtpUtil.FtpConfig ftpConfig = new FtpUtil.FtpConfig().setHost(host).setPort(port).setUserName(userName).setPassword(password);
boolean download = FtpUtil.download(ftpConfig, remotePath, "#20200324", localPath, "#20200324"); boolean download = FtpUtil.download(ftpConfig, olrRemotePath, "#20200324", localPath, "#20200324");
System.out.println(download+"asdasd"); System.out.println(download);
}
public static void main(String[] args) {
} }
......
...@@ -7,7 +7,8 @@ ftp: ...@@ -7,7 +7,8 @@ ftp:
port: 21 port: 21
username: ncc username: ncc
password: ncc123 password: ncc123
remotepath: /NOAA/NCEP/noaa18_olr/ olrremotepath: /NOAA/NCEP/noaa18_olr/
godasremotepath: /NOAA/NCEP/GODAS/pentad/
localpath: /home/jianju/di/tempdata localpath: /home/jianju/di/tempdata
postgis: postgis:
host: 10.40.16.38 host: 10.40.16.38
...@@ -15,7 +16,8 @@ postgis: ...@@ -15,7 +16,8 @@ postgis:
username: postgres username: postgres
password: jianju@123@pg password: jianju@123@pg
dbname: test dbname: test
tbname: noaadata olrtbname: noaadata
godastbname: 待定
logging: logging:
file: file:
path: /home/jianju/di/logs path: /home/jianju/di/logs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment