|
| 1 | +package io.github.lvyahui8.spring.example.facade; |
| 2 | + |
| 3 | +import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade; |
| 4 | +import io.github.lvyahui8.spring.example.model.Post; |
| 5 | +import io.github.lvyahui8.spring.example.model.User; |
| 6 | +import io.github.lvyahui8.spring.example.service.FollowService; |
| 7 | +import io.github.lvyahui8.spring.example.service.PostService; |
| 8 | +import io.github.lvyahui8.spring.example.service.UserService; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | + |
| 12 | +import java.lang.reflect.InvocationTargetException; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import java.util.concurrent.*; |
| 16 | + |
| 17 | +/** |
| 18 | + |
| 19 | + * @since 2019/6/11 21:46 |
| 20 | + */ |
| 21 | +@Component |
| 22 | +public class UserQueryFacade { |
| 23 | + @Autowired |
| 24 | + private FollowService followService; |
| 25 | + @Autowired |
| 26 | + private PostService postService; |
| 27 | + @Autowired |
| 28 | + private UserService userService; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade; |
| 32 | + |
| 33 | + public User getUserData(Long userId) { |
| 34 | + User user = userService.get(userId); |
| 35 | + user.setPosts(postService.getPosts(userId)); |
| 36 | + user.setFollowers(followService.getFollowers(userId)); |
| 37 | + return user; |
| 38 | + } |
| 39 | + |
| 40 | + public User getUserDataByParallel(Long userId) throws InterruptedException, ExecutionException { |
| 41 | + ExecutorService executorService = Executors.newFixedThreadPool(3); |
| 42 | + CountDownLatch countDownLatch = new CountDownLatch(3); |
| 43 | + Future<User> userFuture = executorService.submit(() -> { |
| 44 | + try{ |
| 45 | + return userService.get(userId); |
| 46 | + }finally { |
| 47 | + countDownLatch.countDown(); |
| 48 | + } |
| 49 | + }); |
| 50 | + Future<List<Post>> postsFuture = executorService.submit(() -> { |
| 51 | + try{ |
| 52 | + return postService.getPosts(userId); |
| 53 | + }finally { |
| 54 | + countDownLatch.countDown(); |
| 55 | + } |
| 56 | + }); |
| 57 | + Future<List<User>> followersFuture = executorService.submit(() -> { |
| 58 | + try{ |
| 59 | + return followService.getFollowers(userId); |
| 60 | + }finally { |
| 61 | + countDownLatch.countDown(); |
| 62 | + } |
| 63 | + }); |
| 64 | + countDownLatch.await(); |
| 65 | + User user = userFuture.get(); |
| 66 | + user.setFollowers(followersFuture.get()); |
| 67 | + user.setPosts(postsFuture.get()); |
| 68 | + return user; |
| 69 | + } |
| 70 | + |
| 71 | + public User getUserFinal(Long userId) throws InterruptedException, |
| 72 | + IllegalAccessException, InvocationTargetException { |
| 73 | + return dataBeanAggregateQueryFacade.get("userFullData", |
| 74 | + Collections.singletonMap("userId", userId), User.class); |
| 75 | + } |
| 76 | +} |
0 commit comments