fix: thread interrupt handling + dbinfo null safety
This commit is contained in:
@@ -10,3 +10,4 @@ gen
|
|||||||
target
|
target
|
||||||
*.log
|
*.log
|
||||||
logs
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -63,16 +63,18 @@ public class DataServiceController {
|
|||||||
return Result.success(new ServiceVo(res));
|
return Result.success(new ServiceVo(res));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/stopService")
|
@GetMapping("/stopService")
|
||||||
public Result<ServiceVo> stopService() {
|
public Result<ServiceVo> stopService() {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
if(thread != null) {
|
if(thread != null) {
|
||||||
thread.interrupt();
|
thread.interrupt();
|
||||||
try {
|
try {
|
||||||
thread.join();
|
thread.join(5000); // 5秒超时
|
||||||
|
if (thread.isAlive()) {
|
||||||
|
res = -1; // 超时未停止
|
||||||
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// result = e.getMessage();
|
Thread.currentThread().interrupt();
|
||||||
e.printStackTrace();
|
|
||||||
res = -1;
|
res = -1;
|
||||||
}
|
}
|
||||||
thread = null;
|
thread = null;
|
||||||
@@ -197,18 +199,23 @@ public class DataServiceController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/dbinfo")
|
@GetMapping("/dbinfo")
|
||||||
public Result<DBInfoVo> getDBInfo(){
|
public Result<DBInfoVo> getDBInfo() {
|
||||||
DBInfoVo dbInfo = new DBInfoVo();
|
DBInfoVo dbInfo = new DBInfoVo();
|
||||||
|
|
||||||
dbInfo.setGameNum( (int)gameService.count() );
|
dbInfo.setGameNum((int) gameService.count());
|
||||||
dbInfo.setTeamNum( (int)teamService.count() );
|
dbInfo.setTeamNum((int) teamService.count());
|
||||||
dbInfo.setMatchNum(matchService.getMatchCount());
|
dbInfo.setMatchNum(matchService.getMatchCount());
|
||||||
|
|
||||||
Map<String, Object> map = matchService.getMap(new QueryWrapper<Match>().select("min(time) as dtMin, max(time) as dtMax"));
|
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");
|
||||||
dbInfo.setYearStart( dt.getYear() );
|
Number dtMax = (Number) map.get("dtMax");
|
||||||
dt = LocalDateTime.ofEpochSecond((Integer) map.get("dtMax"), 0, ZoneOffset.ofHours(8));
|
if (dtMin == null || dtMax == null) {
|
||||||
dbInfo.setYearEnd( dt.getYear() );
|
return Result.failed("DB time range unavailable");
|
||||||
|
}
|
||||||
|
LocalDateTime dt = LocalDateTime.ofEpochSecond(dtMin.longValue(), 0, ZoneOffset.ofHours(8));
|
||||||
|
dbInfo.setYearStart(dt.getYear());
|
||||||
|
dt = LocalDateTime.ofEpochSecond(dtMax.longValue(), 0, ZoneOffset.ofHours(8));
|
||||||
|
dbInfo.setYearEnd(dt.getYear());
|
||||||
|
|
||||||
return Result.success(dbInfo);
|
return Result.success(dbInfo);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -452,7 +452,7 @@ public class DataThreadService extends Thread {
|
|||||||
pastDataProcess();
|
pastDataProcess();
|
||||||
// predictDataProcess();
|
// predictDataProcess();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// System.out.println(getName() + " InterruptedException: " + e.getMessage());
|
Thread.currentThread().interrupt();
|
||||||
break;
|
break;
|
||||||
} catch (SocketTimeoutException | ConnectException | ParseException | HttpStatusException e ){
|
} catch (SocketTimeoutException | ConnectException | ParseException | HttpStatusException e ){
|
||||||
// tm = LocalTime.ofSecondOfDay(LocalTime.now().toSecondOfDay()).format(DateTimeFormatter.ISO_TIME);
|
// tm = LocalTime.ofSecondOfDay(LocalTime.now().toSecondOfDay()).format(DateTimeFormatter.ISO_TIME);
|
||||||
@@ -466,12 +466,12 @@ public class DataThreadService extends Thread {
|
|||||||
// break;
|
// break;
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try {
|
||||||
tm = LocalTime.ofSecondOfDay(LocalTime.now().toSecondOfDay()).format(DateTimeFormatter.ISO_TIME);
|
tm = LocalTime.ofSecondOfDay(LocalTime.now().toSecondOfDay()).format(DateTimeFormatter.ISO_TIME);
|
||||||
System.out.println("[" + tm + "] " + getName() + " sleep " + tmWait + "ms");
|
System.out.println("[" + tm + "] " + getName() + " sleep " + tmWait + "ms");
|
||||||
Thread.sleep(tmWait);
|
Thread.sleep(tmWait);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// System.out.println(getName() + " InterruptedException: " + e.getMessage());
|
Thread.currentThread().interrupt();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user