|
15 | 15 | import java.io.File; |
16 | 16 | import java.util.ArrayList; |
17 | 17 | import java.util.List; |
| 18 | +import java.util.concurrent.*; |
18 | 19 |
|
19 | 20 | /** |
20 | 21 | * @author shuzijun |
@@ -80,33 +81,58 @@ public static String formatMarkdown(String content, String host) { |
80 | 81 |
|
81 | 82 | public static List<Solution> getSolutionList(String titleSlug, Project project) { |
82 | 83 | List<Solution> solutionList = new ArrayList<>(); |
83 | | - |
| 84 | + ExecutorService executorService = Executors.newFixedThreadPool(4); |
84 | 85 | try { |
85 | | - HttpResponse response = Graphql.builder().cn(URLUtils.isCn()).operationName("questionSolutionArticles"). |
86 | | - variables("questionSlug", titleSlug).variables("first", 200).variables("skip", 0).variables("orderBy", "DEFAULT").request(); |
87 | | - if (response.getStatusCode() == 200) { |
88 | | - JSONArray edges = JSONObject.parseObject(response.getBody()).getJSONObject("data").getJSONObject("questionSolutionArticles").getJSONArray("edges"); |
89 | | - for (int i = 0; i < edges.size(); i++) { |
90 | | - JSONObject node = edges.getJSONObject(i).getJSONObject("node"); |
91 | | - Solution solution = new Solution(); |
92 | | - solution.setTitle(node.getString("title")); |
93 | | - solution.setSlug(node.getString("slug")); |
94 | | - solution.setSummary(node.getString("summary")); |
| 86 | + int pageCount = 4; |
| 87 | + CountDownLatch latch = new CountDownLatch(pageCount); |
| 88 | + List<Solution>[] results = new List[pageCount]; |
95 | 89 |
|
96 | | - StringBuilder tagsSb = new StringBuilder(); |
97 | | - JSONArray tags = node.getJSONArray("tags"); |
98 | | - for (int j = 0; j < tags.size(); j++) { |
99 | | - tagsSb.append("[").append(tags.getJSONObject(j).getString("name")).append("] "); |
| 90 | + for (int i = 0; i < pageCount; i++) { |
| 91 | + int pageIndex = i; |
| 92 | + int skip = pageIndex * 30; |
| 93 | + executorService.submit(() -> { |
| 94 | + try { |
| 95 | + List<Solution> solutions = new ArrayList<>(); |
| 96 | + HttpResponse response = Graphql.builder().cn(URLUtils.isCn()).operationName("questionSolutionArticles"). |
| 97 | + variables("questionSlug", titleSlug).variables("first", 30).variables("skip", skip).variables("orderBy", "DEFAULT").request(); |
| 98 | + if (response.getStatusCode() == 200) { |
| 99 | + JSONArray edges = JSONObject.parseObject(response.getBody()).getJSONObject("data").getJSONObject("questionSolutionArticles").getJSONArray("edges"); |
| 100 | + for (int j = 0; j < edges.size(); j++) { |
| 101 | + JSONObject node = edges.getJSONObject(j).getJSONObject("node"); |
| 102 | + Solution solution = new Solution(); |
| 103 | + solution.setTitle(node.getString("title")); |
| 104 | + solution.setSlug(node.getString("slug")); |
| 105 | + solution.setSummary(node.getString("summary")); |
| 106 | + |
| 107 | + StringBuilder tagsSb = new StringBuilder(); |
| 108 | + JSONArray tags = node.getJSONArray("tags"); |
| 109 | + for (int k = 0; k < tags.size(); k++) { |
| 110 | + tagsSb.append("[").append(tags.getJSONObject(k).getString("name")).append("] "); |
| 111 | + } |
| 112 | + solution.setTags(tagsSb.toString()); |
| 113 | + solutions.add(solution); |
| 114 | + } |
| 115 | + results[pageIndex] = solutions; |
| 116 | + } else { |
| 117 | + MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("response.code")); |
| 118 | + } |
| 119 | + } finally { |
| 120 | + latch.countDown(); |
100 | 121 | } |
101 | | - solution.setTags(tagsSb.toString()); |
102 | | - solutionList.add(solution); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + latch.await(); |
| 126 | + for (List<Solution> pageResult : results) { |
| 127 | + if (pageResult != null) { |
| 128 | + solutionList.addAll(pageResult); |
103 | 129 | } |
104 | | - } else { |
105 | | - MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("response.code")); |
106 | 130 | } |
107 | 131 | } catch (Exception e) { |
108 | 132 | LogUtils.LOG.error("solutionList acquisition failed", e); |
109 | 133 | MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("response.code")); |
| 134 | + } finally { |
| 135 | + executorService.shutdown(); |
110 | 136 | } |
111 | 137 | return solutionList; |
112 | 138 |
|
|
0 commit comments