Skip to content

Commit eab3606

Browse files
committed
Support functional invocation
1 parent d9e6f26 commit eab3606

File tree

18 files changed

+387
-84
lines changed

18 files changed

+387
-84
lines changed

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

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import io.github.lvyahui8.spring.aggregate.config.RuntimeSettings;
44
import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
55
import io.github.lvyahui8.spring.aggregate.facade.impl.DataBeanAggregateQueryFacadeImpl;
6-
import io.github.lvyahui8.spring.aggregate.model.*;
6+
import io.github.lvyahui8.spring.aggregate.model.DataProvideDefinition;
77
import io.github.lvyahui8.spring.aggregate.repository.DataProviderRepository;
88
import io.github.lvyahui8.spring.aggregate.repository.impl.DataProviderRepositoryImpl;
99
import io.github.lvyahui8.spring.aggregate.service.DataBeanAggregateQueryService;
1010
import io.github.lvyahui8.spring.aggregate.service.impl.DataBeanAggregateQueryServiceImpl;
11-
import io.github.lvyahui8.spring.annotation.DataConsumer;
11+
import io.github.lvyahui8.spring.aggregate.util.DefinitionUtils;
1212
import io.github.lvyahui8.spring.annotation.DataProvider;
13-
import io.github.lvyahui8.spring.annotation.InvokeParameter;
14-
import io.github.lvyahui8.spring.enums.ExceptionProcessingMethod;
1513
import lombok.extern.slf4j.Slf4j;
1614
import org.reflections.Reflections;
1715
import org.reflections.scanners.MethodAnnotationsScanner;
@@ -30,9 +28,6 @@
3028

3129
import java.lang.reflect.Method;
3230
import java.lang.reflect.Modifier;
33-
import java.lang.reflect.Parameter;
34-
import java.util.ArrayList;
35-
import java.util.List;
3631
import java.util.Set;
3732
import java.util.concurrent.ExecutorService;
3833
import java.util.concurrent.LinkedBlockingDeque;
@@ -99,63 +94,25 @@ private void scanProviders(DataProviderRepository repository) {
9994
}
10095

10196
private void dealProvideMethod(DataProviderRepository repository, Method method) {
102-
DataProvideDefinition provider = new DataProvideDefinition();
10397
DataProvider beanProvider = AnnotationUtils.findAnnotation(method, DataProvider.class);
10498
@SuppressWarnings("ConstantConditions")
10599
String dataId = beanProvider.id();
106100
Assert.isTrue(Modifier.isPublic(method.getModifiers()),"data provider method must be public");
107101
Assert.isTrue(! StringUtils.isEmpty(dataId),"data id must be not null!");
102+
DataProvideDefinition provider = DefinitionUtils.getProvideDefinition(method);
108103
provider.setId(dataId);
109-
provider.setMethod(method);
110104
provider.setIdempotent(beanProvider.idempotent());
111105
provider.setTimeout(beanProvider.timeout() > 0 ? beanProvider.timeout() : properties.getDefaultTimeout());
112-
Parameter[] parameters = provider.getMethod().getParameters();
113-
List<MethodArg> methodArgs = new ArrayList<>(method.getParameterCount());
114-
provider.setDepends(new ArrayList<>(method.getParameterCount()));
115-
provider.setParams(new ArrayList<>(method.getParameterCount()));
116-
for (Parameter parameter : parameters) {
117-
dealMethodParameter(provider, methodArgs, parameter);
118-
}
119-
provider.setMethodArgs(methodArgs);
120106
repository.put(dataId,provider);
121107
}
122108

123-
private void dealMethodParameter(DataProvideDefinition provideDefinition, List<MethodArg> methodArgs, Parameter parameter) {
124-
DataConsumer dataConsumer = AnnotationUtils.findAnnotation(parameter, DataConsumer.class);
125-
InvokeParameter invokeParameter = AnnotationUtils.findAnnotation(parameter,InvokeParameter.class);
126-
Assert.isTrue(dataConsumer != null || invokeParameter != null,
127-
"Parameters must be added @InvokeParameter or @DataConsumer annotation");
128-
MethodArg methodArg = new MethodArg();
129-
if(dataConsumer != null) {
130-
String dataId = dataConsumer.id();
131-
Assert.isTrue(! StringUtils.isEmpty(dataId),"data id must be not null!");
132-
methodArg.setAnnotationKey(dataId);
133-
methodArg.setDependType(DependType.OTHER_MODEL);
134-
DataConsumeDefinition dataConsumeDefinition = new DataConsumeDefinition();
135-
dataConsumeDefinition.setClazz(parameter.getType());
136-
dataConsumeDefinition.setId(dataId);
137-
if(! dataConsumer.exceptionProcessingMethod().equals(ExceptionProcessingMethod.BY_DEFAULT)) {
138-
dataConsumeDefinition.setIgnoreException(
139-
dataConsumer.exceptionProcessingMethod().equals(ExceptionProcessingMethod.IGNORE)
140-
);
141-
}
142-
provideDefinition.getDepends().add(dataConsumeDefinition);
143-
} else {
144-
methodArg.setAnnotationKey(invokeParameter.value());
145-
methodArg.setDependType(DependType.INVOKE_PARAM);
146-
InvokeParameterDefinition parameterDefinition = new InvokeParameterDefinition();
147-
parameterDefinition.setKey(invokeParameter.value());
148-
provideDefinition.getParams().add(parameterDefinition);
149-
}
150-
methodArg.setParameter(parameter);
151-
methodArgs.add(methodArg);
152-
}
153109

154110
private RuntimeSettings createRuntimeSettings() {
155111
RuntimeSettings runtimeSettings = new RuntimeSettings();
156112
runtimeSettings.setEnableLogging(properties.getEnableLogging() != null
157113
? properties.getEnableLogging() : false);
158114
runtimeSettings.setIgnoreException(properties.isIgnoreException());
115+
runtimeSettings.setTimeout(properties.getDefaultTimeout());
159116
return runtimeSettings;
160117
}
161118

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
@@ -10,4 +10,5 @@
1010
public class RuntimeSettings {
1111
private boolean enableLogging;
1212
private boolean ignoreException;
13+
private Long timeout;
1314
}

spring-boot-data-aggregator-core/src/main/java/io/github/lvyahui8/spring/aggregate/facade/DataBeanAggregateQueryFacade.java

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

3+
import io.github.lvyahui8.spring.aggregate.func.MultipleArgumentsFunction;
4+
35
import java.lang.reflect.InvocationTargetException;
46
import java.util.Map;
57

@@ -27,4 +29,39 @@ public interface DataBeanAggregateQueryFacade {
2729
*/
2830
<T> T get(String id, Map<String,Object> invokeParams, Class<T> clazz)
2931
throws InterruptedException, IllegalAccessException, InvocationTargetException;
32+
33+
/**
34+
* Used to query data. It has the following three functions
35+
* 1. Automatic analysis dependence
36+
* 2. Parallel access dependency
37+
* 3. Automatic injection
38+
*
39+
* @param invokeParams Fixed parameters that need to be passed in the query process
40+
* @param multipleArgumentsFunction
41+
* @param <T> Return value type
42+
* @return Return value
43+
* @throws InterruptedException If the thread is interrupted, this exception will be thrown
44+
* @throws IllegalAccessException Thrown if the data provider cannot be executed
45+
* @throws InvocationTargetException If the data provider throws an unhandled exception, this exception will be thrown
46+
*/
47+
<T> T get(Map<String,Object> invokeParams, MultipleArgumentsFunction<T> multipleArgumentsFunction)
48+
throws InterruptedException, IllegalAccessException, InvocationTargetException;
49+
50+
/**
51+
* Used to query data. It has the following three functions
52+
* 1. Automatic analysis dependence
53+
* 2. Parallel access dependency
54+
* 3. Automatic injection
55+
*
56+
* @param invokeParams Fixed parameters that need to be passed in the query process
57+
* @param multipleArgumentsFunction
58+
* @param timeout Timeout
59+
* @param <T> Return value type
60+
* @return Return value
61+
* @throws InterruptedException If the thread is interrupted, this exception will be thrown
62+
* @throws IllegalAccessException Thrown if the data provider cannot be executed
63+
* @throws InvocationTargetException If the data provider throws an unhandled exception, this exception will be thrown
64+
*/
65+
<T> T get(Map<String,Object> invokeParams, MultipleArgumentsFunction<T> multipleArgumentsFunction,Long timeout)
66+
throws InterruptedException, IllegalAccessException, InvocationTargetException;
3067
}

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package io.github.lvyahui8.spring.aggregate.facade.impl;
22

33
import io.github.lvyahui8.spring.aggregate.consts.AggregatorConstant;
4-
import io.github.lvyahui8.spring.aggregate.service.DataBeanAggregateQueryService;
54
import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
5+
import io.github.lvyahui8.spring.aggregate.func.MultipleArgumentsFunction;
6+
import io.github.lvyahui8.spring.aggregate.model.DataProvideDefinition;
7+
import io.github.lvyahui8.spring.aggregate.service.DataBeanAggregateQueryService;
8+
import io.github.lvyahui8.spring.aggregate.util.DefinitionUtils;
69
import org.springframework.util.Assert;
710

811
import java.lang.reflect.InvocationTargetException;
12+
import java.lang.reflect.Method;
13+
import java.lang.reflect.Modifier;
914
import java.util.Collections;
1015
import java.util.Map;
1116
import java.util.concurrent.ConcurrentHashMap;
1217

18+
1319
/**
1420
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
1521
* @since 2019/6/1 0:22
@@ -32,4 +38,50 @@ public <T> T get(String id, Map<String,Object> invokeParams, Class<T> clazz) thr
3238
return dataBeanAggregateQueryService.get(id,invokeParams,clazz,
3339
new ConcurrentHashMap<>(AggregatorConstant.DEFAULT_INITIAL_CAPACITY));
3440
}
41+
42+
@Override
43+
public <T> T get(Map<String, Object> invokeParams, MultipleArgumentsFunction<T> multipleArgumentsFunction) throws InterruptedException, IllegalAccessException, InvocationTargetException {
44+
return get(invokeParams,multipleArgumentsFunction,null);
45+
}
46+
47+
@Override
48+
public <T> T get(Map<String, Object> invokeParams, MultipleArgumentsFunction<T> multipleArgumentsFunction, Long timeout) throws InterruptedException, IllegalAccessException, InvocationTargetException{
49+
Method[] methods = multipleArgumentsFunction.getClass().getMethods();
50+
Method applyMethod = null;
51+
if(invokeParams == null) {
52+
invokeParams = Collections.emptyMap();
53+
}
54+
55+
for (Method method : methods) {
56+
if(! Modifier.isStatic(method.getModifiers()) && ! method.isDefault()) {
57+
applyMethod = method;
58+
break;
59+
}
60+
}
61+
62+
if(applyMethod == null) {
63+
throw new IllegalAccessException(multipleArgumentsFunction.getClass().getName());
64+
}
65+
66+
67+
DataProvideDefinition provider = DefinitionUtils.getProvideDefinition(applyMethod);
68+
provider.setTimeout(timeout);
69+
provider.setTarget(multipleArgumentsFunction);
70+
71+
boolean accessible = applyMethod.isAccessible();
72+
if(! accessible) {
73+
applyMethod.setAccessible(true);
74+
}
75+
try {
76+
@SuppressWarnings("unchecked")
77+
T ret = (T) dataBeanAggregateQueryService.get(provider, invokeParams, applyMethod.getReturnType(),
78+
new ConcurrentHashMap<>(AggregatorConstant.DEFAULT_INITIAL_CAPACITY));
79+
80+
return ret;
81+
} finally {
82+
if(! accessible) {
83+
applyMethod.setAccessible(accessible);
84+
}
85+
}
86+
}
3587
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.lvyahui8.spring.aggregate.func;
2+
3+
/**
4+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
5+
* @since 2019/7/13 23:19
6+
*/
7+
@FunctionalInterface
8+
public interface Function2<T,U,R> extends MultipleArgumentsFunction<R> {
9+
/**
10+
* support two parameters
11+
*
12+
* @param t param 1
13+
* @param u param 2
14+
* @return return value
15+
*/
16+
R apply(T t, U u);
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.lvyahui8.spring.aggregate.func;
2+
3+
/**
4+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
5+
* @since 2019/7/13 23:21
6+
*/
7+
@FunctionalInterface
8+
public interface Function3<T,U,V,R> extends MultipleArgumentsFunction<R> {
9+
/**
10+
* support three parameters
11+
*
12+
* @param t param 1
13+
* @param u param 2
14+
* @param v param 3
15+
* @return return value
16+
*/
17+
R apply(T t, U u,V v);
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.lvyahui8.spring.aggregate.func;
2+
3+
/**
4+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
5+
* @since 2019/7/14 14:23
6+
*/
7+
@FunctionalInterface
8+
public interface Function4<T,U,V,W,R> extends MultipleArgumentsFunction<R> {
9+
/**
10+
* support four parameters
11+
*
12+
* @param t param 1
13+
* @param u param 2
14+
* @param v param 3
15+
* @param w param 4
16+
* @return return value
17+
*/
18+
R apply(T t, U u, V v ,W w);
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.lvyahui8.spring.aggregate.func;
2+
3+
/**
4+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
5+
* @since 2019/7/14 14:23
6+
*/
7+
@FunctionalInterface
8+
public interface Function5<T,U,V,W,X,R> extends MultipleArgumentsFunction<R> {
9+
/**
10+
* support four parameters
11+
*
12+
* @param t param 1
13+
* @param u param 2
14+
* @param v param 3
15+
* @param w param 4
16+
* @param x param 5
17+
* @return return value
18+
*/
19+
R apply(T t, U u, V v ,W w,X x);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.lvyahui8.spring.aggregate.func;
2+
3+
/**
4+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
5+
* @since 2019/7/13 23:18
6+
*/
7+
public interface MultipleArgumentsFunction<R> {
8+
9+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class DataProvideDefinition {
1414
private String id;
1515
private Method method;
16+
private Object target;
1617
private Long timeout;
1718
private List<DataConsumeDefinition> depends;
1819
private List<InvokeParameterDefinition> params;

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package io.github.lvyahui8.spring.aggregate.service;
22

3+
import io.github.lvyahui8.spring.aggregate.model.DataConsumeDefinition;
4+
import io.github.lvyahui8.spring.aggregate.model.DataProvideDefinition;
35
import io.github.lvyahui8.spring.aggregate.model.InvokeSignature;
46

57
import java.lang.reflect.InvocationTargetException;
8+
import java.util.List;
69
import java.util.Map;
710

811
/**
@@ -20,7 +23,47 @@ public interface DataBeanAggregateQueryService {
2023
* @param <T> final result type
2124
* @param queryCache Used to cache data during the query
2225
* @return final result
26+
* @throws InterruptedException 中断
27+
* @throws InvocationTargetException 反射异常
28+
* @throws IllegalAccessException 反射异常
2329
*/
2430
<T> T get(String id, Map<String,Object> invokeParams, Class<T> resultType,final Map<InvokeSignature,Object> queryCache)
2531
throws InterruptedException, InvocationTargetException, IllegalAccessException;
32+
33+
34+
/**
35+
* 并发+递归获取并拼装数据
36+
*
37+
* @param provider data Provider
38+
* @param invokeParams query parameters
39+
* @param resultType final result type
40+
* @param <T> final result type
41+
* @param queryCache Used to cache data during the query
42+
* @return final result
43+
* @throws InterruptedException 中断
44+
* @throws InvocationTargetException 反射异常
45+
* @throws IllegalAccessException 反射异常
46+
*/
47+
<T> T get(DataProvideDefinition provider, Map<String,Object> invokeParams, Class<T> resultType, final Map<InvokeSignature,Object> queryCache)
48+
throws InterruptedException, InvocationTargetException, IllegalAccessException;
49+
50+
/**
51+
* 并发获取依赖项
52+
*
53+
* @param invokeParams 查询参数
54+
* @param consumeDefinitions 依赖定义
55+
* @param timeout timeout value
56+
* @param queryCache Used to cache data during the query
57+
* @return depend object map
58+
* @throws InterruptedException 中断
59+
* @throws InvocationTargetException 反射异常
60+
* @throws IllegalAccessException 反射异常
61+
*/
62+
Map<String, Object> getDependObjectMap(Map<String, Object> invokeParams,
63+
List<DataConsumeDefinition> consumeDefinitions,
64+
Long timeout,
65+
Map<InvokeSignature, Object> queryCache)
66+
throws InterruptedException, InvocationTargetException, IllegalAccessException ;
67+
68+
2669
}

0 commit comments

Comments
 (0)