From f61ea49810afe75ae70c37f8c955b6408141ea62 Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Sat, 6 Feb 2021 06:43:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/IDipperDataAsyncTaskService.java | 2 +- .../impl/DipperDataAsyncTaskServiceImpl.java | 19 ++++++++++++++++--- .../dipperposition/task/ScheduleService.java | 6 +++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/telpo/dipperposition/service/IDipperDataAsyncTaskService.java b/src/main/java/com/telpo/dipperposition/service/IDipperDataAsyncTaskService.java index b60ffeb..5af5cb2 100644 --- a/src/main/java/com/telpo/dipperposition/service/IDipperDataAsyncTaskService.java +++ b/src/main/java/com/telpo/dipperposition/service/IDipperDataAsyncTaskService.java @@ -19,7 +19,7 @@ public interface IDipperDataAsyncTaskService { * 同步任务 * */ - void pullAstEPH(); + void pullAstEPH(int tryTimes) throws InterruptedException; /** diff --git a/src/main/java/com/telpo/dipperposition/service/impl/DipperDataAsyncTaskServiceImpl.java b/src/main/java/com/telpo/dipperposition/service/impl/DipperDataAsyncTaskServiceImpl.java index f034ebe..e955650 100644 --- a/src/main/java/com/telpo/dipperposition/service/impl/DipperDataAsyncTaskServiceImpl.java +++ b/src/main/java/com/telpo/dipperposition/service/impl/DipperDataAsyncTaskServiceImpl.java @@ -10,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import static java.lang.Thread.sleep; + /** * @program: DipperDataAsyncTaskServiceImpl * @description: 获取星历数据。 @@ -31,13 +33,20 @@ public class DipperDataAsyncTaskServiceImpl implements IDipperDataAsyncTaskServi @Override @Async("asyncServiceExecutor") - public void pullAstEPH() { + public void pullAstEPH(int tryTimes) throws InterruptedException { + if (tryTimes==0) { + return; + } else { + tryTimes--; + } // (1) 发送bds获取星历数据 byte[] dipperData = pullEPHFromDipper(); // (2) 获取星历数据 if (dipperData == null) { log.error("获取星历数据错误,取不到星历数据。"); + sleep(10000); + pullAstEPH(tryTimes); } else { // 保存到DB或者缓存 log.info("保存到DB或者缓存"); @@ -65,7 +74,7 @@ public class DipperDataAsyncTaskServiceImpl implements IDipperDataAsyncTaskServi String hexIn = astTimeCmd + checkSum.substring(0,2) + " " + checkSum.substring(2,4); byte[] sendResult = null; - String ackAckCheckRef = "233E010102000421293C"; + //String ackAckCheckRef = "233E010102000421293C"; sendResult = client.sendCmd(hexIn); client.closeConnection(); @@ -77,7 +86,11 @@ public class DipperDataAsyncTaskServiceImpl implements IDipperDataAsyncTaskServi // String dipperData = pullEPHFromDipper(); byte[] dipperData; if (!redisUtil.hasKey(DIPPER_DATA_KEY)) { - this.pullAstEPH(); + try { + this.pullAstEPH(1); + } catch (InterruptedException e) { + log.error("获取星历数据发生异常:", e); + } } dipperData = (byte[])redisUtil.get(DIPPER_DATA_KEY); diff --git a/src/main/java/com/telpo/dipperposition/task/ScheduleService.java b/src/main/java/com/telpo/dipperposition/task/ScheduleService.java index 70d31f1..4a9f49a 100644 --- a/src/main/java/com/telpo/dipperposition/task/ScheduleService.java +++ b/src/main/java/com/telpo/dipperposition/task/ScheduleService.java @@ -31,7 +31,11 @@ public class ScheduleService { log.info("开始星历数据同步!"); // 获取推送失败的记录 try { - dipperDataAsyncTaskService.pullAstEPH(); + // 如果失败,则可以等待10秒再获取1次。 + int tryTimes = 2; + dipperDataAsyncTaskService.pullAstEPH(tryTimes); + } catch (InterruptedException e) { + log.error("获取星历数据重试睡眠发生异常:", e); } catch (Exception e) { log.error("执行定时获取星历数据发生异常:", e); }