hecheng 3 anos atrás
pai
commit
4006af7ab2

+ 16 - 21
zd-modules/zd-forward/src/main/java/com/zd/forward/listener/StartListener.java

@@ -5,11 +5,12 @@ import com.zd.forward.properties.FireProperties;
 import com.zd.forward.serivce.FireImageService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.CommandLineRunner;
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.io.IOException;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 监听Spring容器启动完成,完成后启动Netty服务器
@@ -23,32 +24,26 @@ public class StartListener implements CommandLineRunner {
     private FireImageService fireImageService;
     @Resource
     private FireProperties fireProperties;
+
     @Resource
-    private ThreadPoolTaskExecutor taskExecutor;
+    private ScheduledExecutorService scheduledExecutorService;
 
     @Override
     public void run(String... args) {
-        String streamUrl = fireProperties.getStreamUrl();
-        if (streamUrl == null) {
-            log.error("=========调用产生异常:未配置流媒体地址============");
-            return;
-        }
-        //定时调度,无法使用scheduledExecutorService完成定时调度,scheduledExecutorService默认实现了jdk自动关闭策略,默认会杀死程序,此处手动实现延时调用
-        taskExecutor.execute(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    while (true) {
-                        fireImageService.catchImage();
-                        Thread.sleep(fireProperties.getWaitTime() * 1000L);
-                    }
-                } catch (ServiceException | IOException | InterruptedException e) {
-                    //异常回调,防止系统因异常问题被杀死
-                    log.error("=========调用产生异常:{}============", e.getMessage());
-                    run();
+        scheduledExecutorService.scheduleWithFixedDelay(() -> {
+            try {
+                String streamUrl = fireProperties.getStreamUrl();
+                if (streamUrl == null) {
+                    log.error("=========调用产生异常:未配置流媒体地址============");
+                    return;
                 }
+                fireImageService.catchImage();
+            } catch (ServiceException | IOException e) {
+                //异常回调,防止系统因异常问题被杀死
+                log.error("=========调用产生异常:{}============", e.getMessage());
+                run();
             }
-        });
+        },0,fireProperties.getWaitTime(), TimeUnit.SECONDS);
     }
 
 }

+ 0 - 6
zd-modules/zd-forward/src/main/java/com/zd/forward/serivce/FireImageService.java

@@ -59,12 +59,6 @@ public class FireImageService {
      */
     private static final String IMAGE_FORMAT = "jpg";
 
-    /**
-     * 上传文件存储在本地的根路径
-     */
-    @Value("${file.path:/home/AIPIC}")
-    private String imagePath;
-
     public void catchImage() throws IOException {
         String streamUrl = fireProperties.getStreamUrl();
         if (streamUrl == null) {

+ 13 - 7
zd-modules/zd-forward/src/main/java/com/zd/forward/serivce/LoginService.java

@@ -3,6 +3,7 @@ package com.zd.forward.serivce;
 import com.alibaba.fastjson.JSON;
 import com.zd.common.core.constant.SecurityConstants;
 import com.zd.common.core.domain.R;
+import com.zd.common.core.exception.ServiceException;
 import com.zd.common.security.service.TokenService;
 import com.zd.forward.config.AlgorithmYml;
 import lombok.extern.slf4j.Slf4j;
@@ -15,6 +16,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
+import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
 
 import java.util.HashMap;
@@ -54,13 +56,17 @@ public class LoginService {
         map.put("username", "admin");
         map.put("password", "zd123456..");
         ParameterizedTypeReference<R<Map<String, Object>>> reference = new ParameterizedTypeReference<R<Map<String, Object>>>(){};
-        ResponseEntity<R<Map<String, Object>>> response = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(map), reference);
-        R<Map<String, Object>> parse = response.getBody();
-        if (parse != null) {
-            Assert.isTrue(parse.getCode() == R.SUCCESS, "登录失败:" + parse.getMsg());
-            //拿到token
-            Map<String, Object> data = parse.getData();
-            setToken("Bearer " + data.get("access_token"));
+        try {
+            ResponseEntity<R<Map<String, Object>>> response = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(map), reference);
+            R<Map<String, Object>> parse = response.getBody();
+            if (parse != null) {
+                Assert.isTrue(parse.getCode() == R.SUCCESS, "登录失败:" + parse.getMsg());
+                //拿到token
+                Map<String, Object> data = parse.getData();
+                setToken("Bearer " + data.get("access_token"));
+            }
+        } catch (RestClientException e) {
+            throw new ServiceException(e.getMessage());
         }
     }