Skip to content

Commit ef61097

Browse files
committed
Merge branch 'develop'
2 parents ff312b5 + 6127cc6 commit ef61097

File tree

10 files changed

+219
-9
lines changed

10 files changed

+219
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ private void scanProviders(DataProviderRepository repository) {
8787
Reflections reflections = new Reflections(basePackage, new MethodAnnotationsScanner());
8888
Set<Method> providerMethods = reflections.getMethodsAnnotatedWith(DataProvider.class);
8989
for (Method method : providerMethods) {
90-
dealProvideMethod(repository, method);
90+
dealProviderMethod(repository, method);
9191
}
9292
}
9393
}
9494
}
9595

96-
private void dealProvideMethod(DataProviderRepository repository, Method method) {
96+
private void dealProviderMethod(DataProviderRepository repository, Method method) {
9797
DataProvider beanProvider = AnnotationUtils.findAnnotation(method, DataProvider.class);
9898
@SuppressWarnings("ConstantConditions")
9999
String dataId = beanProvider.id();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
55
* @since 2019/6/28 23:53
66
*/
7-
public interface AggregatorConstant
7+
public interface AggregationConstant
88
{
99
Object EMPTY_MODEL = new Object();
1010
int DEFAULT_INITIAL_CAPACITY = 8;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.lvyahui8.spring.aggregate.context;
2+
3+
import io.github.lvyahui8.spring.aggregate.model.DataProvideDefinition;
4+
import lombok.Data;
5+
6+
import java.util.Map;
7+
8+
/**
9+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
10+
* @since 2019/7/21 22:37
11+
*/
12+
@Data
13+
public class AggregationContext {
14+
/**
15+
* 发起一次递归查询的主调线程
16+
*/
17+
Thread rootThread;
18+
/**
19+
* 根provider
20+
*/
21+
DataProvideDefinition rootProvideDefinition;
22+
/**
23+
* 此次查询生命周期中的缓存
24+
*/
25+
Map<String,Object> cacheMap;
26+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.lvyahui8.spring.aggregate.facade.impl;
22

3-
import io.github.lvyahui8.spring.aggregate.consts.AggregatorConstant;
3+
import io.github.lvyahui8.spring.aggregate.consts.AggregationConstant;
44
import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
55
import io.github.lvyahui8.spring.aggregate.func.MultipleArgumentsFunction;
66
import io.github.lvyahui8.spring.aggregate.model.DataProvideDefinition;
@@ -36,7 +36,7 @@ public <T> T get(String id, Map<String,Object> invokeParams, Class<T> clazz) thr
3636
invokeParams = Collections.emptyMap();
3737
}
3838
return dataBeanAggregateQueryService.get(id,invokeParams,clazz,
39-
new ConcurrentHashMap<>(AggregatorConstant.DEFAULT_INITIAL_CAPACITY));
39+
new ConcurrentHashMap<>(AggregationConstant.DEFAULT_INITIAL_CAPACITY));
4040
}
4141

4242
@Override
@@ -75,7 +75,7 @@ public <T> T get(Map<String, Object> invokeParams, MultipleArgumentsFunction<T>
7575
try {
7676
@SuppressWarnings("unchecked")
7777
T ret = (T) dataBeanAggregateQueryService.get(provider, invokeParams, applyMethod.getReturnType(),
78-
new ConcurrentHashMap<>(AggregatorConstant.DEFAULT_INITIAL_CAPACITY));
78+
new ConcurrentHashMap<>(AggregationConstant.DEFAULT_INITIAL_CAPACITY));
7979

8080
return ret;
8181
} finally {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.lvyahui8.spring.aggregate.interceptor;
2+
3+
import io.github.lvyahui8.spring.aggregate.context.AggregationContext;
4+
import io.github.lvyahui8.spring.aggregate.model.DataProvideDefinition;
5+
6+
/**
7+
* 查询过程中各个生命周期的拦截处理
8+
*
9+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
10+
* @since 2019/7/21 22:28
11+
*/
12+
public interface AbstractAggregateQueryInterceptor {
13+
/**
14+
* 查询正常提交, Context已经创建
15+
*
16+
* @param aggregationContext 查询上下文
17+
* @return 返回为true才继续执行
18+
*/
19+
boolean querySubmitted(AggregationContext aggregationContext) ;
20+
21+
/**
22+
* 每个Provider方法执行前, 将调用此方法. 存在并发调用
23+
*
24+
* @param aggregationContext 查询上下文
25+
* @param provideDefinition 将被执行的Provider
26+
* @return 返回为true才继续执行
27+
*/
28+
boolean queryBefore(AggregationContext aggregationContext, DataProvideDefinition provideDefinition);
29+
30+
/**
31+
* 每个Provider方法执行成功之后, 调用此方法. 存在并发调用
32+
*
33+
* @param aggregationContext 查询上下文
34+
* @param provideDefinition 被执行的Provider
35+
* @param result 查询结果
36+
* @return 返回结果, 如不修改不, 请直接返回参数中的result
37+
*/
38+
Object queryAfter(AggregationContext aggregationContext, DataProvideDefinition provideDefinition, Object result);
39+
40+
/**
41+
* 每个Provider执行时, 如果抛出异常, 将调用此方法. 存在并发调用
42+
*
43+
* @param aggregationContext 查询上下文
44+
* @param provideDefinition 被执行的Provider
45+
* @param e Provider抛出的异常
46+
*/
47+
void exceptionHandle(AggregationContext aggregationContext, DataProvideDefinition provideDefinition, Exception e);
48+
49+
/**
50+
* 一次查询全部完成.
51+
*
52+
* @param aggregationContext 查询上下文
53+
*/
54+
void queryFinished(AggregationContext aggregationContext);
55+
}

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

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

33
import io.github.lvyahui8.spring.aggregate.config.RuntimeSettings;
4-
import io.github.lvyahui8.spring.aggregate.consts.AggregatorConstant;
4+
import io.github.lvyahui8.spring.aggregate.consts.AggregationConstant;
55
import io.github.lvyahui8.spring.aggregate.model.*;
66
import io.github.lvyahui8.spring.aggregate.repository.DataProviderRepository;
77
import io.github.lvyahui8.spring.aggregate.service.DataBeanAggregateQueryService;
@@ -87,11 +87,11 @@ public <T> T get(DataProvideDefinition provider, Map<String, Object> invokeParam
8787
: provider.getTarget(), args);
8888
if(provider.isIdempotent()) {
8989
/* Map 中可能不能放空value */
90-
queryCache.put(invokeSignature,resultModel != null ? resultModel : AggregatorConstant.EMPTY_MODEL);
90+
queryCache.put(invokeSignature,resultModel != null ? resultModel : AggregationConstant.EMPTY_MODEL);
9191
}
9292
}
9393

94-
return resultType.cast(resultModel != AggregatorConstant.EMPTY_MODEL ? resultModel : null);
94+
return resultType.cast(resultModel != AggregationConstant.EMPTY_MODEL ? resultModel : null);
9595
}
9696

9797
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.lvyahui8.spring.example.context;
2+
3+
/**
4+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
5+
* @since 2019/7/22 23:11
6+
*/
7+
public class RequestContext {
8+
private static ThreadLocal<Long> TENANT_ID = new ThreadLocal<>();
9+
10+
public static Long getTenantId() {
11+
return TENANT_ID.get();
12+
}
13+
14+
public static void setTenantId(Long tenantId) {
15+
TENANT_ID.set(tenantId);
16+
}
17+
18+
public static void removeTenantId() {
19+
TENANT_ID.remove();
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.lvyahui8.spring.example.service;
2+
3+
import io.github.lvyahui8.spring.example.model.Category;
4+
import io.github.lvyahui8.spring.example.model.Post;
5+
import io.github.lvyahui8.spring.example.model.User;
6+
7+
import java.util.List;
8+
9+
/**
10+
*
11+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
12+
* @since 2019/7/22 22:25
13+
*/
14+
public interface HomepageService {
15+
/**
16+
* 获取顶部菜单类目
17+
*
18+
* @return 顶部类目
19+
*/
20+
List<Category> topMenu();
21+
22+
/**
23+
* 文章列表
24+
*
25+
* @return xx
26+
*/
27+
List<Post> postList();
28+
29+
/**
30+
* 粉丝数据
31+
* @return xxx
32+
*/
33+
List<User> allFollowers();
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.lvyahui8.spring.example.service.impl;
2+
3+
import io.github.lvyahui8.spring.annotation.DataProvider;
4+
import io.github.lvyahui8.spring.example.context.RequestContext;
5+
import io.github.lvyahui8.spring.example.model.Category;
6+
import io.github.lvyahui8.spring.example.model.Post;
7+
import io.github.lvyahui8.spring.example.model.User;
8+
import io.github.lvyahui8.spring.example.service.HomepageService;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.util.Assert;
11+
12+
import java.util.List;
13+
14+
/**
15+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
16+
* @since 2019/7/22 23:08
17+
*/
18+
@Service
19+
public class HomepageServiceImpl implements HomepageService {
20+
21+
@DataProvider("topMenu")
22+
@Override
23+
public List<Category> topMenu() {
24+
/* will be null */
25+
Long tenantId = RequestContext.getTenantId();
26+
Assert.notNull(tenantId,"tenantId must be not null");
27+
// ... The content hereafter will be omitted.
28+
return null;
29+
}
30+
31+
@DataProvider("postList")
32+
@Override
33+
public List<Post> postList() {
34+
/* will be null */
35+
Long tenantId = RequestContext.getTenantId();
36+
Assert.notNull(tenantId,"tenantId must be not null");
37+
// ... The content hereafter will be omitted.
38+
return null;
39+
}
40+
41+
@DataProvider("allFollowers")
42+
@Override
43+
public List<User> allFollowers() {
44+
/* will be null */
45+
Long tenantId = RequestContext.getTenantId();
46+
Assert.notNull(tenantId,"tenantId must be not null");
47+
// ... The content hereafter will be omitted.
48+
return null;
49+
}
50+
}

spring-boot-data-aggregator-example/src/test/java/io/github/lvyahui8/spring/example/DataBeanAggregateQueryFacadeTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
44
import io.github.lvyahui8.spring.aggregate.func.Function2;
5+
import io.github.lvyahui8.spring.aggregate.func.Function3;
56
import io.github.lvyahui8.spring.annotation.DataConsumer;
67
import io.github.lvyahui8.spring.autoconfigure.BeanAggregateProperties;
78
import io.github.lvyahui8.spring.example.context.ExampleAppContext;
9+
import io.github.lvyahui8.spring.example.context.RequestContext;
10+
import io.github.lvyahui8.spring.example.model.Category;
811
import io.github.lvyahui8.spring.example.model.Post;
912
import io.github.lvyahui8.spring.example.model.User;
1013
import lombok.extern.slf4j.Slf4j;
@@ -97,4 +100,25 @@ public User apply(@DataConsumer("loggedUsername") String loggedUsername,
97100
ExampleAppContext.remove();
98101
}
99102
}
103+
104+
@Test
105+
public void testThreadLocal() throws Exception {
106+
try {
107+
RequestContext.setTenantId(10000L);
108+
Object result = dataBeanAggregateQueryFacade.get(null,
109+
new Function3<List<Category>, List<Post>, List<User>, Object>() {
110+
@Override
111+
public Object apply(
112+
@DataConsumer("topMenu") List<Category> categories,
113+
@DataConsumer("postList") List<Post> posts,
114+
@DataConsumer("allFollowers") List<User> users) {
115+
return new Object[] {
116+
categories,posts,users
117+
};
118+
}
119+
});
120+
} finally {
121+
RequestContext.removeTenantId();
122+
}
123+
}
100124
}

0 commit comments

Comments
 (0)