Skip to content

Commit ff312b5

Browse files
authored
Merge pull request #3 from lvyahui8/develop
Develop
2 parents bcf729c + 1241d60 commit ff312b5

File tree

10 files changed

+125
-5
lines changed

10 files changed

+125
-5
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public class PostServiceImpl implements PostService {
9898
```java
9999
@Service
100100
public class UserServiceImpl implements UserService {
101-
102101
@DataProvider("user")
103102
@Override
104103
public User get(@InvokeParameter("userId") Long id) {

README_EN.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ require input parameter `userId`.
101101
```java
102102
@Service
103103
public class UserServiceImpl implements UserService {
104-
105104
    @DataProvider("user")
106105
    @Override
107106
    public User get(@InvokeParameter("userId") Long id) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.lvyahui8.spring.aggregate.concurrent;
2+
3+
/**
4+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
5+
* @since 2019/7/18 21:49
6+
*/
7+
public abstract class AbstractRunnableWrapper implements Runnable {
8+
private Thread rootThread;
9+
10+
@Override
11+
public void run() {
12+
/* copy context to executor service threads */
13+
try {
14+
doRun();
15+
} finally {
16+
/* remove all cope context*/
17+
}
18+
}
19+
20+
/**
21+
* actual run method
22+
*/
23+
public abstract void doRun();
24+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <T> T get(String id, Map<String,Object> invokeParams, Class<T> clazz)
3737
* 3. Automatic injection
3838
*
3939
* @param invokeParams Fixed parameters that need to be passed in the query process
40-
* @param multipleArgumentsFunction
40+
* @param multipleArgumentsFunction Multiple arguments function
4141
* @param <T> Return value type
4242
* @return Return value
4343
* @throws InterruptedException If the thread is interrupted, this exception will be thrown
@@ -54,7 +54,7 @@ <T> T get(Map<String,Object> invokeParams, MultipleArgumentsFunction<T> multiple
5454
* 3. Automatic injection
5555
*
5656
* @param invokeParams Fixed parameters that need to be passed in the query process
57-
* @param multipleArgumentsFunction
57+
* @param multipleArgumentsFunction Multiple arguments function
5858
* @param timeout Timeout
5959
* @param <T> Return value type
6060
* @return Return value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.lvyahui8.spring.example.context;
2+
3+
import io.github.lvyahui8.spring.example.model.User;
4+
5+
/**
6+
* @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com)
7+
* @since 2019/7/18 22:19
8+
*/
9+
public class ExampleAppContext {
10+
/**
11+
* 这里必须使用, InheritableThreadLocal, 只有InheritableThreadLocal会向子线程传递.
12+
*/
13+
private static ThreadLocal<User> LOGGED_USER = new InheritableThreadLocal<>();
14+
15+
public static void setLoggedUser(User user) {
16+
LOGGED_USER.set(user);
17+
}
18+
19+
public static boolean isLogged() {
20+
return LOGGED_USER.get() != null;
21+
}
22+
23+
public static Long getUserId() {
24+
return LOGGED_USER.get().getId();
25+
}
26+
27+
public static String getUsername() {
28+
return LOGGED_USER.get() != null ? LOGGED_USER.get().getUsername() : null;
29+
}
30+
31+
public static void remove() {
32+
LOGGED_USER.remove();
33+
}
34+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,18 @@
99
* @since 2019/6/11 21:31
1010
*/
1111
public interface FollowService {
12+
/**
13+
* xxx
14+
*
15+
* @param userId userId
16+
* @return xx
17+
*/
1218
List<User> getFollowers(Long userId);
19+
20+
/**
21+
* xxx
22+
*
23+
* @return xx
24+
*/
25+
List<User> getLoggedUserFollowers();
1326
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ public interface UserService {
1313
* @return
1414
*/
1515
User get(Long id);
16+
17+
/**
18+
* 获取已登录用户用户名
19+
*
20+
* @return xx
21+
*/
22+
String getLoggedUsername();
1623
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import io.github.lvyahui8.spring.annotation.DataProvider;
44
import io.github.lvyahui8.spring.annotation.InvokeParameter;
5+
import io.github.lvyahui8.spring.example.context.ExampleAppContext;
56
import io.github.lvyahui8.spring.example.model.User;
67
import io.github.lvyahui8.spring.example.service.FollowService;
78
import org.springframework.stereotype.Service;
89

910
import java.util.ArrayList;
11+
import java.util.Collections;
1012
import java.util.List;
1113

1214
/**
@@ -30,4 +32,15 @@ public List<User> getFollowers(@InvokeParameter("userId") Long userId) {
3032
}
3133
return users;
3234
}
35+
36+
@DataProvider("loggedUserFollowers")
37+
@Override
38+
public List<User> getLoggedUserFollowers() {
39+
Long userId = ExampleAppContext.getUserId();
40+
String username = ExampleAppContext.getUsername();
41+
User follower = new User();
42+
follower.setId(userId + 10000L);
43+
follower.setUsername(username + "_follower");
44+
return Collections.singletonList(follower);
45+
}
3346
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.lvyahui8.spring.annotation.DataProvider;
44
import io.github.lvyahui8.spring.annotation.InvokeParameter;
5+
import io.github.lvyahui8.spring.example.context.ExampleAppContext;
56
import io.github.lvyahui8.spring.example.model.User;
67
import io.github.lvyahui8.spring.example.service.UserService;
78
import org.springframework.stereotype.Service;
@@ -29,4 +30,10 @@ public User get(@InvokeParameter("userId") Long id) {
2930
user.setUsername("lvyahui8");
3031
return user;
3132
}
33+
34+
@DataProvider("loggedUsername")
35+
@Override
36+
public String getLoggedUsername() {
37+
return ExampleAppContext.getUsername();
38+
}
3239
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.lvyahui8.spring.aggregate.func.Function2;
55
import io.github.lvyahui8.spring.annotation.DataConsumer;
66
import io.github.lvyahui8.spring.autoconfigure.BeanAggregateProperties;
7+
import io.github.lvyahui8.spring.example.context.ExampleAppContext;
78
import io.github.lvyahui8.spring.example.model.Post;
89
import io.github.lvyahui8.spring.example.model.User;
910
import lombok.extern.slf4j.Slf4j;
@@ -63,7 +64,7 @@ public User apply(@DataConsumer("user") User user, @DataConsumer("posts") List<P
6364
return user;
6465
}
6566
},null);
66-
log.info("query result:{} ",user);
67+
Assert.notNull(user,"user never not be null!");
6768
try {
6869
user = dataBeanAggregateQueryFacade.get(singletonMap, (Function2<User, List<Post>, User>) (user1, posts) -> {
6970
user1.setPosts(posts);
@@ -73,4 +74,27 @@ public User apply(@DataConsumer("user") User user, @DataConsumer("posts") List<P
7374
log.info("don't support lambda!!! eMsg:{}",e.getMessage());
7475
}
7576
}
77+
78+
@Test
79+
public void testInheritableThreadLocals() throws Exception {
80+
try {
81+
User user = new User();
82+
user.setUsername("bob");
83+
user.setId(100000L);
84+
ExampleAppContext.setLoggedUser(user);
85+
dataBeanAggregateQueryFacade.get(null, new Function2<String,List<User>,User>() {
86+
@Override
87+
public User apply(@DataConsumer("loggedUsername") String loggedUsername,
88+
@DataConsumer("loggedUserFollowers") List<User> loggedUserFollowers) {
89+
Assert.notNull(loggedUsername, "loggedUsername must be not null");
90+
Assert.notNull(loggedUserFollowers, "loggedUserFollowers must be not null");
91+
Assert.notNull(ExampleAppContext.getUsername(),"ExampleAppContext.getUsername() must be not null");
92+
log.info("everything is normal~");
93+
return null;
94+
}
95+
});
96+
} finally {
97+
ExampleAppContext.remove();
98+
}
99+
}
76100
}

0 commit comments

Comments
 (0)