merge
This commit is contained in:
@@ -7,7 +7,6 @@ import org.slf4j.LoggerFactory;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
@@ -39,7 +38,7 @@ public class CompletableFutureDemo {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//出现异常,异常会被限制在执行任务的线程范围内
|
||||
//出现异常,异常会被限制在执行任务的线程的范围内,最终会杀死该线程,而这会导致等待 get 方法返回结果的线程永久地被阻塞。
|
||||
if (1 == 1)
|
||||
throw new RuntimeException("异常");
|
||||
return "world";
|
||||
@@ -48,9 +47,11 @@ public class CompletableFutureDemo {
|
||||
object.put("s1", s1);
|
||||
object.put("s2", s2);
|
||||
return object;
|
||||
}).exceptionally(e -> {//捕获异常
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}).join();
|
||||
})
|
||||
// .exceptionally(e -> {//捕获CompletionException,如有全局异常处理,可省略
|
||||
// throw new RuntimeException(e.getMessage());
|
||||
// })
|
||||
.join();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -67,29 +68,24 @@ public class CompletableFutureDemo {
|
||||
*/
|
||||
public static JSONObject thenAcceptBoth() {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
try {
|
||||
CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "hello";
|
||||
}).thenAcceptBoth(CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "world";
|
||||
}), (s1, s2) -> {
|
||||
jsonObject.put("s1", s1);
|
||||
jsonObject.put("s2", s2);
|
||||
}).get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new RuntimeException("获取数据异常");
|
||||
}
|
||||
CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "hello";
|
||||
}).thenAcceptBoth(CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "world";
|
||||
}), (s1, s2) -> {
|
||||
jsonObject.put("s1", s1);
|
||||
jsonObject.put("s2", s2);
|
||||
}).join();
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@@ -103,36 +99,32 @@ public class CompletableFutureDemo {
|
||||
List<Integer> allResult = new ArrayList<>();
|
||||
List<Integer> s1 = new ArrayList<>();
|
||||
List<Integer> s2 = new ArrayList<>();
|
||||
try {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
s1.add(1);
|
||||
}).runAfterBothAsync(CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
s2.add(1);
|
||||
}), () -> {
|
||||
for (int i = 0; i < s1.size(); i++) {
|
||||
Integer ii = s1.get(i);
|
||||
for (int j = 0; j < s2.size(); j++) {
|
||||
if (j == i) {
|
||||
Integer jj = s2.get(j);
|
||||
allResult.add(ii + jj);
|
||||
break;
|
||||
}
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
s1.add(1);
|
||||
}).runAfterBothAsync(CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
s2.add(1);
|
||||
}), () -> {
|
||||
for (int i = 0; i < s1.size(); i++) {
|
||||
Integer ii = s1.get(i);
|
||||
for (int j = 0; j < s2.size(); j++) {
|
||||
if (j == i) {
|
||||
Integer jj = s2.get(j);
|
||||
allResult.add(ii + jj);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}).get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException("获取数据异常");
|
||||
}
|
||||
}
|
||||
}).join();
|
||||
return allResult;
|
||||
}
|
||||
|
||||
@@ -143,25 +135,21 @@ public class CompletableFutureDemo {
|
||||
*/
|
||||
public static JSONObject acceptEither() {
|
||||
JSONObject object = new JSONObject();
|
||||
try {
|
||||
CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "hello";
|
||||
}).acceptEither(CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "world";
|
||||
}), s -> object.put("s", s)).get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException("获取数据异常");
|
||||
}
|
||||
CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "hello";
|
||||
}).acceptEither(CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "world";
|
||||
}), s -> object.put("s", s)).join();
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.xwbing.demo;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xwbing.exception.BusinessException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
@@ -9,7 +8,6 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -85,13 +83,7 @@ public class LambdaDemo {
|
||||
reduce = optional.get();
|
||||
}
|
||||
//异步回调
|
||||
CompletableFuture<List<JSONObject>> future = CompletableFuture.supplyAsync(LambdaDemo::getList);
|
||||
try {
|
||||
List<JSONObject> sysUsers = future.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new BusinessException("获取数据出错");
|
||||
}
|
||||
List<JSONObject> sysUsers = CompletableFuture.supplyAsync(LambdaDemo::getList).join();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,14 +102,14 @@ public class LambdaDemo {
|
||||
futures[i] = CompletableFuture.runAsync(() -> System.out.println("null"));
|
||||
}
|
||||
}
|
||||
// CompletableFuture.allOf(futures).join();//线程等待,效果等同于get(),不用抛检测异常,不推荐
|
||||
CompletableFuture<Void> completableFuture = CompletableFuture.allOf(futures);
|
||||
try {
|
||||
completableFuture.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
logger.error(e.getMessage());
|
||||
throw new BusinessException("获取数据出错");
|
||||
}
|
||||
CompletableFuture.allOf(futures).join();//线程等待,效果等同于get(),会拋出CompletionException
|
||||
// CompletableFuture<Void> completableFuture = CompletableFuture.allOf(futures);
|
||||
// try {
|
||||
// completableFuture.get();
|
||||
// } catch (InterruptedException | ExecutionException e) {
|
||||
// logger.error(e.getMessage());
|
||||
// throw new BusinessException("获取数据出错");
|
||||
// }
|
||||
return finalList;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.xwbing.demo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
@@ -11,14 +9,7 @@ import java.util.stream.Collectors;
|
||||
* 说明: 测试用
|
||||
*/
|
||||
public class demo {
|
||||
public static void main(String[] args) {
|
||||
long l = System.currentTimeMillis();
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
list = list.stream().map(integer -> integer * 10).collect(Collectors.toList());
|
||||
long l1 = System.currentTimeMillis();
|
||||
System.out.println(l1 - l);
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user