fix: thread interrupt handling + dbinfo null safety
This commit is contained in:
@@ -10,3 +10,4 @@ gen
|
||||
target
|
||||
*.log
|
||||
logs
|
||||
*.md
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.datao.config;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Documented
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface PassToken {
|
||||
boolean value() default true;
|
||||
}
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
package com.datao.config;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
//import javax.servlet.http.HttpServletRequest;
|
||||
//import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* 添加拦截器
|
||||
*/
|
||||
//@Component
|
||||
public class TokenInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
// System.out.println(String.format("[%s]", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
|
||||
// +" preHandle "+request.getRequestURL());
|
||||
//如果不是映射到方法直接放行
|
||||
if(!(handler instanceof HandlerMethod)){
|
||||
return true;
|
||||
}
|
||||
HandlerMethod handlerMethod = (HandlerMethod)handler;
|
||||
Method method = handlerMethod.getMethod();
|
||||
|
||||
HashMap<String,String> hashMap = new HashMap<>();
|
||||
String token = request.getHeader("Authorization");
|
||||
if((token == null || token.isEmpty())
|
||||
&& method.isAnnotationPresent(PassToken.class)) { //带有PassToken注解的方法不处理
|
||||
return true;
|
||||
}
|
||||
|
||||
// String msg = "";
|
||||
// try {
|
||||
// if(token != null && token!="") {
|
||||
// TokenUtil.verifyToken(token.substring(7));
|
||||
// return true;
|
||||
// }
|
||||
// else{
|
||||
// msg = "token无效";
|
||||
// }
|
||||
// }catch (SignatureVerificationException e){
|
||||
//// e.printStackTrace();
|
||||
// msg = "签名不一致";
|
||||
// }catch (TokenExpiredException e){
|
||||
//// e.printStackTrace();
|
||||
// msg = "令牌过期异常";
|
||||
// }catch (AlgorithmMismatchException e) {
|
||||
//// e.printStackTrace();
|
||||
// msg = "算法不匹配异常";
|
||||
// }catch (InvalidClaimException e){
|
||||
//// e.printStackTrace();
|
||||
// msg = "失效的claim异常";
|
||||
// }catch (Exception e){
|
||||
//// e.printStackTrace();
|
||||
// msg = "token无效";
|
||||
// }
|
||||
|
||||
// AjaxResult result = new AjaxResult(HttpStatus.UNAUTHORIZED.value(),msg);
|
||||
//
|
||||
// String resultJson = JSON.toJSONString(result);
|
||||
// response.setContentType("application/json;charset=utf-8");
|
||||
// response.getWriter().print(resultJson);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||
ModelAndView modelAndView) throws Exception {
|
||||
// System.out.println("postHandle");
|
||||
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler, Exception ex) throws Exception {
|
||||
// System.out.println("afterCompletion");
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.datao.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
//import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
|
||||
//配置拦截事件
|
||||
//@Configuration
|
||||
public class TokenWebMvcConfigurer implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private TokenInterceptor tokenInterceptor;
|
||||
|
||||
/**
|
||||
* 配置跨域请求
|
||||
*
|
||||
* @param registry
|
||||
*/
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
//定义哪些URL接受跨域
|
||||
registry.addMapping("/**")
|
||||
//定义哪些origin可以跨域请求
|
||||
// .allowedOrigins("*")
|
||||
.allowedOriginPatterns("*")
|
||||
//定义接受的跨域请求方法
|
||||
.allowedMethods("POST", "GET", "PUT", "DELETE")
|
||||
.exposedHeaders("Set-Token")
|
||||
.allowCredentials(true)
|
||||
.allowedHeaders("*")
|
||||
.maxAge(3600);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add("/error");
|
||||
// list.add("/Api/**");
|
||||
|
||||
registry.addInterceptor(tokenInterceptor)
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns(list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,10 +69,12 @@ public class DataServiceController {
|
||||
if(thread != null) {
|
||||
thread.interrupt();
|
||||
try {
|
||||
thread.join();
|
||||
thread.join(5000); // 5秒超时
|
||||
if (thread.isAlive()) {
|
||||
res = -1; // 超时未停止
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
// result = e.getMessage();
|
||||
e.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
res = -1;
|
||||
}
|
||||
thread = null;
|
||||
@@ -205,9 +207,14 @@ public class DataServiceController {
|
||||
dbInfo.setMatchNum(matchService.getMatchCount());
|
||||
|
||||
Map<String, Object> map = matchService.getMap(new QueryWrapper<Match>().select("min(time) as dtMin, max(time) as dtMax"));
|
||||
LocalDateTime dt = LocalDateTime.ofEpochSecond((Integer) map.get("dtMin"), 0, ZoneOffset.ofHours(8));
|
||||
Number dtMin = (Number) map.get("dtMin");
|
||||
Number dtMax = (Number) map.get("dtMax");
|
||||
if (dtMin == null || dtMax == null) {
|
||||
return Result.failed("DB time range unavailable");
|
||||
}
|
||||
LocalDateTime dt = LocalDateTime.ofEpochSecond(dtMin.longValue(), 0, ZoneOffset.ofHours(8));
|
||||
dbInfo.setYearStart(dt.getYear());
|
||||
dt = LocalDateTime.ofEpochSecond((Integer) map.get("dtMax"), 0, ZoneOffset.ofHours(8));
|
||||
dt = LocalDateTime.ofEpochSecond(dtMax.longValue(), 0, ZoneOffset.ofHours(8));
|
||||
dbInfo.setYearEnd(dt.getYear());
|
||||
|
||||
return Result.success(dbInfo);
|
||||
|
||||
@@ -452,7 +452,7 @@ public class DataThreadService extends Thread {
|
||||
pastDataProcess();
|
||||
// predictDataProcess();
|
||||
} catch (InterruptedException e) {
|
||||
// System.out.println(getName() + " InterruptedException: " + e.getMessage());
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
} catch (SocketTimeoutException | ConnectException | ParseException | HttpStatusException e ){
|
||||
// tm = LocalTime.ofSecondOfDay(LocalTime.now().toSecondOfDay()).format(DateTimeFormatter.ISO_TIME);
|
||||
@@ -471,7 +471,7 @@ public class DataThreadService extends Thread {
|
||||
System.out.println("[" + tm + "] " + getName() + " sleep " + tmWait + "ms");
|
||||
Thread.sleep(tmWait);
|
||||
} catch (InterruptedException e) {
|
||||
// System.out.println(getName() + " InterruptedException: " + e.getMessage());
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user