Skip to content

Commit 2a98a3b

Browse files
committed
实现异常处理, 支持忽略异常或者异常中断, 支持全局配置和consumer级别配置
1 parent 205da08 commit 2a98a3b

File tree

12 files changed

+110
-19
lines changed

12 files changed

+110
-19
lines changed

spring-boot-data-aggregator-autoconfigure/src/main/java/io/github/lvyahui8/spring/annotation/DataConsumer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.lvyahui8.spring.annotation;
22

3+
import io.github.lvyahui8.spring.enums.ExceptionProcessingMethod;
34
import org.springframework.core.annotation.AliasFor;
45

56
import java.lang.annotation.*;
@@ -25,4 +26,10 @@
2526
*/
2627
@AliasFor("id")
2728
String value() default "";
29+
30+
/**
31+
* Exception handling, default by global configuration
32+
*/
33+
ExceptionProcessingMethod exceptionProcessingMethod()
34+
default ExceptionProcessingMethod.BY_DEFAULT;
2835
}

spring-boot-data-aggregator-autoconfigure/src/main/java/io/github/lvyahui8/spring/annotation/DataProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@
3131
* Asynchronous execution method timeout
3232
*/
3333
long timeout() default -1;
34+
3435
}

spring-boot-data-aggregator-autoconfigure/src/main/java/io/github/lvyahui8/spring/autoconfigure/BeanAggregateAutoConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.github.lvyahui8.spring.annotation.DataConsumer;
1111
import io.github.lvyahui8.spring.annotation.DataProvider;
1212
import io.github.lvyahui8.spring.annotation.InvokeParameter;
13+
import io.github.lvyahui8.spring.enums.ExceptionProcessingMethod;
1314
import lombok.extern.slf4j.Slf4j;
1415
import org.reflections.Reflections;
1516
import org.reflections.scanners.MethodAnnotationsScanner;
@@ -122,6 +123,11 @@ private void dealMethodParamter(DataProvideDefination provideDefination, List<Me
122123
DataConsumeDefination dataConsumeDefination = new DataConsumeDefination();
123124
dataConsumeDefination.setClazz(parameter.getType());
124125
dataConsumeDefination.setId(dataId);
126+
if(! dataConsumer.exceptionProcessingMethod().equals(ExceptionProcessingMethod.BY_DEFAULT)) {
127+
dataConsumeDefination.setIgnoreException(
128+
dataConsumer.exceptionProcessingMethod().equals(ExceptionProcessingMethod.IGNORE)
129+
);
130+
}
125131
provideDefination.getDepends().add(dataConsumeDefination);
126132
} else {
127133
methodArg.setAnnotionKey(invokeParameter.value());
@@ -138,6 +144,7 @@ private RuntimeSettings createRuntimeSettings() {
138144
RuntimeSettings runtimeSettings = new RuntimeSettings();
139145
runtimeSettings.setEnableLogging(properties.getEnableLogging() != null
140146
? properties.getEnableLogging() : false);
147+
runtimeSettings.setIgnoreException(properties.isIgnoreException());
141148
return runtimeSettings;
142149
}
143150

spring-boot-data-aggregator-autoconfigure/src/main/java/io/github/lvyahui8/spring/autoconfigure/BeanAggregateProperties.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@ public class BeanAggregateProperties {
1717
/**
1818
* Thread name prefix for asynchronous threads
1919
*/
20-
private String threadPrefix = "aggregateTask-";
20+
private String threadPrefix = "aggregateTask-";
2121
/**
2222
* Thread size of the asynchronous thread pool
2323
*/
24-
private int threadNumber = Runtime.getRuntime().availableProcessors() * 3;
24+
private int threadNumber = Runtime.getRuntime().availableProcessors() * 3;
2525
/**
2626
* The size of the queue that holds the task to be executed
2727
*/
28-
private int queueSize = 1000;
28+
private int queueSize = 1000;
2929
/**
3030
* Set a default timeout for the method of providing data
3131
*/
32-
private Long defaultTimeout = 3000L;
32+
private Long defaultTimeout = 3000L;
3333
/**
3434
* Allow output log
3535
*/
36-
private Boolean enableLogging = true;
36+
private Boolean enableLogging = true;
37+
/**
38+
* Ignore exception thrown by asynchronous execution, method returns null value
39+
*/
40+
private boolean ignoreException = false;
3741
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.lvyahui8.spring.enums;
2+
3+
/**
4+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
5+
* @since 2019/6/15 23:13
6+
*/
7+
public enum ExceptionProcessingMethod {
8+
/**
9+
* Ignore exception thrown by asynchronous execution, method returns null value
10+
*/
11+
IGNORE,
12+
/**
13+
* Follow the default handling of the framework
14+
*/
15+
BY_DEFAULT,
16+
/**
17+
* Abort execution
18+
*/
19+
SUSPEND;
20+
}

spring-boot-data-aggregator-core/src/main/java/io/github/lvyahui8/spring/aggregate/config/RuntimeSettings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
@Data
1010
public class RuntimeSettings {
1111
private boolean enableLogging;
12+
private boolean ignoreException;
1213
}

spring-boot-data-aggregator-core/src/main/java/io/github/lvyahui8/spring/aggregate/model/DataConsumeDefination.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
public class DataConsumeDefination {
1111
private String id;
1212
private Class<?> clazz;
13+
private Boolean ignoreException;
1314
}

spring-boot-data-aggregator-core/src/main/java/io/github/lvyahui8/spring/aggregate/service/impl/DataBeanAgregateQueryServiceImpl.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ public <T> T get(String id, Map<String, Object> invokeParams, Class<T> resultTyp
4949
if(provider.getDepends() != null && ! provider.getDepends().isEmpty()) {
5050
CountDownLatch stopDownLatch = new CountDownLatch(provider.getDepends().size());
5151
Map<String,Future<?>> futureMap = new HashMap<>(provider.getDepends().size());
52+
Map<String,DataConsumeDefination> consumeDefinationMap = new HashMap<>(provider.getDepends().size());
5253
for (DataConsumeDefination depend : provider.getDepends()) {
54+
consumeDefinationMap.put(depend.getId(),depend);
5355
Future<?> future = executorService.submit(() -> {
5456
try {
5557
Object o = get(depend.getId(), invokeParams, depend.getClazz());
5658
return depend.getClazz().cast(o);
57-
} catch (Exception e) {
58-
return null;
59-
}finally {
59+
} finally {
6060
stopDownLatch.countDown();
6161
}
6262
});
@@ -65,11 +65,18 @@ public <T> T get(String id, Map<String, Object> invokeParams, Class<T> resultTyp
6565
stopDownLatch.await(provider.getTimeout(),TimeUnit.MILLISECONDS);
6666
if(! futureMap.isEmpty()){
6767
for (Map.Entry<String,Future<?>> item : futureMap.entrySet()) {
68+
Future<?> future = item.getValue();
69+
Object value = null;
70+
DataConsumeDefination consumeDefination = consumeDefinationMap.get(item.getKey());
6871
try {
69-
dependObjectMap.put(item.getKey(),item.getValue().get());
72+
value = future.get();
7073
} catch (ExecutionException e) {
71-
//
74+
if (consumeDefination.getIgnoreException() != null ? ! consumeDefination.getIgnoreException()
75+
: ! runtimeSettings.isIgnoreException()) {
76+
throwException(e);
77+
}
7278
}
79+
dependObjectMap.put(item.getKey(),value);
7380
}
7481
}
7582
}
@@ -82,16 +89,30 @@ public <T> T get(String id, Map<String, Object> invokeParams, Class<T> resultTyp
8289
} else {
8390
args[i] = invokeParams.get(methodArg.getAnnotionKey());
8491
}
85-
if (! methodArg.getParameter().getType().isAssignableFrom(args[i].getClass())) {
92+
if (args[i] != null && ! methodArg.getParameter().getType().isAssignableFrom(args[i].getClass())) {
8693
throw new IllegalArgumentException("param type not match, param:"
8794
+ methodArg.getParameter().getName());
8895
}
8996
}
97+
try {
98+
return resultType.cast(provider.getMethod()
99+
.invoke(applicationContext.getBean(provider.getMethod().getDeclaringClass()), args));
100+
} finally {
101+
logging(id, startTime, provider);
102+
}
103+
}
90104

91-
T result = resultType.cast(provider.getMethod()
92-
.invoke(applicationContext.getBean(provider.getMethod().getDeclaringClass()), args));
93-
logging(id, startTime, provider);
94-
return result;
105+
private void throwException(ExecutionException e) throws InterruptedException,
106+
InvocationTargetException, IllegalAccessException {
107+
if (e.getCause() instanceof InterruptedException) {
108+
throw (InterruptedException) e.getCause();
109+
} else if (e.getCause() instanceof InvocationTargetException){
110+
throw (InvocationTargetException) e.getCause();
111+
} else if (e.getCause() instanceof IllegalAccessException) {
112+
throw (IllegalAccessException) e.getCause();
113+
} else {
114+
throw (RuntimeException) e.getCause();
115+
}
95116
}
96117

97118
private void logging(String id, long startTime, DataProvideDefination provider) {

spring-boot-data-aggregator-example/src/main/java/io/github/lvyahui8/spring/example/service/impl/PostServiceImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package io.github.lvyahui8.spring.example.service.impl;
22

3-
import io.github.lvyahui8.spring.annotation.DataProvider;
4-
import io.github.lvyahui8.spring.annotation.InvokeParameter;
5-
import io.github.lvyahui8.spring.example.model.Post;
6-
import io.github.lvyahui8.spring.example.service.PostService;
73
import io.github.lvyahui8.spring.annotation.DataProvider;
84
import io.github.lvyahui8.spring.annotation.InvokeParameter;
95
import io.github.lvyahui8.spring.example.model.Post;
106
import io.github.lvyahui8.spring.example.service.PostService;
117
import org.springframework.stereotype.Service;
8+
import org.springframework.util.Assert;
129

1310
import java.util.Collections;
1411
import java.util.List;
@@ -22,6 +19,7 @@ public class PostServiceImpl implements PostService {
2219
@DataProvider(id = "posts")
2320
@Override
2421
public List<Post> getPosts(@InvokeParameter("userId") Long userId) {
22+
Assert.isTrue(userId != null && userId != 0, "userId must be not null and gt 0!");
2523
try {
2624
Thread.sleep(1000L);
2725
} catch (InterruptedException e) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
spring.main.banner-mode=off
22
io.github.lvyahui8.spring.base-packages=io.github.lvyahui8.spring.example
3+
io.github.lvyahui8.spring.ignore-exception=false

0 commit comments

Comments
 (0)