Skip to content

Commit 6127cc6

Browse files
committed
optimize
1 parent 3ba5613 commit 6127cc6

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
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)