DownloadFileJob.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.jiayue.client.job;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.http.HttpRequest;
  4. import cn.hutool.http.HttpUtil;
  5. import cn.hutool.json.JSONArray;
  6. import cn.hutool.json.JSONObject;
  7. import cn.hutool.json.JSONUtil;
  8. import com.jiayue.client.entity.FileCreateLog;
  9. import com.jiayue.client.service.QueryDataService;
  10. import com.jiayue.client.util.FileUtil;
  11. import com.jiayue.client.util.Scpclient;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
  14. import org.springframework.core.io.PathResource;
  15. import org.springframework.scheduling.annotation.Scheduled;
  16. import org.springframework.stereotype.Service;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Properties;
  22. /**
  23. * 青海集中功率预测从云平台下载气象文件数据:
  24. * 1.每天凌晨5点执行,下载当天气象文件传给功率预测服务器
  25. */
  26. @Service
  27. @Slf4j
  28. public class DownloadFileJob {
  29. protected final static Properties properties;
  30. static {
  31. YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
  32. yaml.setResources(new PathResource("./focus-client.yml"));
  33. properties = yaml.getObject();
  34. }
  35. public final QueryDataService queryDataService;
  36. //获取下载到文件,从临时目录移动到目标目录
  37. public final List<String> filedownLoadList = new ArrayList<>();
  38. public final List<String> isDownFileId = new ArrayList<>();
  39. public DownloadFileJob(QueryDataService queryDataService) {
  40. this.queryDataService = queryDataService;
  41. }
  42. /**
  43. * 每天凌晨2点执行
  44. */
  45. @Scheduled(cron = "0 0/5 * * * ?")
  46. public void download() {
  47. log.info("上午文件下载定时任务开始");
  48. List<Map<String, Object>> stations = queryDataService.getStations();
  49. for (Map<String, Object> station : stations) {
  50. try {
  51. String stationCode = Convert.toStr(station.get("C_STATION_CODE"));
  52. log.info("开始执行定时任务:http下载场站:{} 的文件。", stationCode);
  53. String body = HttpRequest.get(properties.getProperty("v3cloud.filelogUrl") + stationCode).execute().body();
  54. JSONObject json = JSONUtil.parseObj(body);
  55. String code = json.get("code").toString();
  56. String data = json.get("data").toString();
  57. if (code.equals("0") && data.length() > 0) {
  58. JSONArray array = JSONUtil.parseArray(data);
  59. List<FileCreateLog> list = array.toList(FileCreateLog.class);
  60. String path = FileUtil.judeDirExists(properties.getProperty("local.downFilePath"));
  61. for (FileCreateLog fileCreateLog : list) {
  62. if (isDownFileId.contains(fileCreateLog.getId())) {
  63. log.info("文件:{}已下载。", fileCreateLog.getFileName());
  64. continue;
  65. }
  66. log.info("开始下载文件:{}。", fileCreateLog.getFileName());
  67. String url = properties.getProperty("v3cloud.fileDownUrl") + fileCreateLog.getId();
  68. String targetPath = path + File.separatorChar + stationCode + File.separatorChar + "new" + File.separatorChar + fileCreateLog.getFileName();
  69. long size = HttpUtil.downloadFile(url, targetPath);
  70. if (size > 0) {
  71. isDownFileId.add(fileCreateLog.getId());
  72. log.debug("成功下载文件:{}", targetPath);
  73. filedownLoadList.add(targetPath);
  74. }
  75. }
  76. }
  77. if (!filedownLoadList.isEmpty()) {
  78. log.debug("开始移动下载文件到功率预测,共:{} 个文件。", filedownLoadList.size());
  79. for (String filePath : filedownLoadList) {
  80. if ("flase".equals(properties.getProperty("remote.ip"))) {
  81. log.warn("备机配置为空,不需要传文件到备机上。");
  82. break;
  83. }
  84. Scpclient scpclient = Scpclient.getInstance(properties.getProperty("remote.ip"),
  85. Convert.toInt(properties.getProperty("remote.port")), properties.getProperty("remote.user"),
  86. properties.getProperty("remote.pwd"));
  87. scpclient.putFile(filePath, properties.getProperty("remote.path") + stationCode + "/new/");
  88. log.info("移动文件:{} 到远程服务器 {} 成功", filePath, properties.getProperty("remote.path") + stationCode + "/new/");
  89. }
  90. log.debug("所有文件移动完成,清空文件临时filedownLoadList");
  91. filedownLoadList.clear();
  92. } else {
  93. log.info("没有新文件下载,不需要移动到远程功率预测服务器。");
  94. }
  95. } catch (Exception e) {
  96. log.error("文件下载失败:{}", e);
  97. }
  98. }
  99. }
  100. }