Browse Source

feat: 检查是否长时间没收到报告

weijianghai 9 months ago
parent
commit
f42b1d0d10

+ 2 - 0
src/main/java/com/nokia/sms/SmsServerApplication.java

@@ -2,7 +2,9 @@ package com.nokia.sms;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
+@EnableScheduling
 @SpringBootApplication
 public class SmsServerApplication {
     public static void main(String[] args) {

+ 23 - 0
src/main/java/com/nokia/sms/service/SgipSmsService.java

@@ -10,9 +10,12 @@ import com.nokia.sms.sgip.mt.client.MTClient;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.PostConstruct;
+import java.time.Duration;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
@@ -31,6 +34,14 @@ public class SgipSmsService {
     static final LinkedBlockingQueue<Sms> QUEUE = new LinkedBlockingQueue<>();
     private final SmsDao smsDao;
     private final MTClient mtClient;
+    /**
+     * 最后发短信时间
+     */
+    private LocalDateTime lastSendTime = LocalDateTime.now();
+    /**
+     * 最后收到报告时间
+     */
+    private LocalDateTime lastReportTime = LocalDateTime.now();
 
     /**
      * 最大重试次数
@@ -87,6 +98,7 @@ public class SgipSmsService {
 //            }
             sms.setStatus("已发送到短信网关");
             log.info("短信发送成功: {}", JSON.toJSONString(sms));
+            lastSendTime = LocalDateTime.now();
         } catch (Exception e) {
             sms.setStatus("发送到短信网关失败");
             log.error("短信发送失败, 短信: {}, 错误信息: {}", sms, e.getMessage(), e);
@@ -130,6 +142,7 @@ public class SgipSmsService {
                 try {
                     SmsReport smsReport = SmsReport.take();
                     smsDao.updateSmsReport(smsReport);
+                    lastReportTime = LocalDateTime.now();
                 } catch (InterruptedException e) {
                     Thread.currentThread().interrupt();
                     log.error(e.getMessage(), e);
@@ -154,4 +167,14 @@ public class SgipSmsService {
             log.error("重新入队失败: {}", JSON.toJSONString(sms));
         }
     }
+
+    /**
+     * 检查是否长时间没收到报告
+     */
+    @Scheduled(cron = "0 0 * * * ?")
+    public void checkReportTime() {
+        if (Duration.between(lastSendTime, lastReportTime).toMinutes() > 60) {
+            log.error("超过一小时没有收到报告");
+        }
+    }
 }