This commit is contained in:
xiangwb
2018-02-08 10:57:55 +08:00
parent 43e935b2d5
commit c0ff746d57
6 changed files with 131 additions and 106 deletions
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.xwbing.annotation.LogInfo;
import com.xwbing.domain.entity.ExpressInfo;
import com.xwbing.domain.entity.vo.ExpressInfoVo;
import com.xwbing.exception.BusinessException;
import com.xwbing.redis.RedisService;
import com.xwbing.service.other.CookieSessionService;
import com.xwbing.service.other.ExpressDeliveryService;
@@ -23,6 +24,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* 说明: 测试控制层
@@ -129,4 +131,36 @@ public class TestControl {
public JSONObject cookie(HttpServletRequest request, HttpServletResponse response) {
return JSONObjResult.toJSONObj(cookieSessionService.cookie(response, request));
}
@LogInfo("completableFuture")
@GetMapping("completableFuture")
public JSONObject completableFuture() {
return JSONObjResult.toJSONObj(thenCombine(), "");
}
private JSONObject thenCombine() {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}).thenCombine(CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//出现异常,异常会被限制在执行任务的线程范围内
if (1 == 1)
throw new RuntimeException("545456");
return "world";
}), (s1, s2) -> {
JSONObject object = new JSONObject();
object.put("s1", s1);
object.put("s2", s2);
return object;
}).join();
}
}
@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.stream.Collectors;
/**
@@ -43,11 +44,30 @@ public class GlobalExceptionHandler {
// 返回给页面200状态码
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public JSONObject handlerGuideException(BusinessException ex) {
public JSONObject handlerBusinessException(BusinessException ex) {
logger.error(ex.getMessage());
return JSONObjResult.toJSONObj(ex.getMessage());
}
/**
* CompletableFuture完成结果或任务过程中出现的异常
*
* @param ex
* @return
*/
@ExceptionHandler(value = CompletionException.class)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public JSONObject handlerCompletionException(CompletionException ex) {
Throwable cause = ex.getCause();
String errorMessages = cause.getMessage();
logger.error(errorMessages);
String detail = cause.toString();
if (!detail.contains("BusinessException"))
errorMessages = "异步获取数据出错";
return JSONObjResult.toJSONObj(errorMessages);
}
/**
* 表单检验(validator) 异常
*
@@ -42,7 +42,7 @@ public class LoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String servletPath = request.getServletPath();
if (!set.contains(servletPath) && !servletPath.contains("login")) {
if (!set.contains(servletPath) && !servletPath.contains("login") && !servletPath.contains("test")) {
// HttpSession session = request.getSession();
// if (session.getAttribute(CommonConstant.CURRENT_USER)!=null) {
// return true;