このSpringBootをDockerで動かす方法
1. https://github.com/Yuzunoha/SpringBootAsync1 の メインブランチをチェックアウトする
2. 直下で make release-up
を実行する(make コマンドが無かったら入れて)。ビルドが始まるのでしばらく待つ
3. curl localhost を実行するとレスポンスが返ってくる
コーディングの注意点
completableFuture.join() をやった時点でスレッドが止まるので、join()は最後にまとめてやらないと非同期にならない。
例えばこんな風にしてはダメ。
1 2 3 |
createCompletableFuture(...).join(); createCompletableFuture(...).join(); createCompletableFuture(...).join(); |
こうするべし
1 2 3 4 5 6 |
var a = createCompletableFuture(...); var b = createCompletableFuture(...); var c = createCompletableFuture(...); a.join(); b.join(); c.join(); |
以下検証コード。Anno0Wrap1の書き方を採用するのがよいと思う。@Async 無しでも非同期になるので。
(AsyncController.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
package com.example.demo; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; import lombok.RequiredArgsConstructor; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequiredArgsConstructor public class AsyncController { private final IAsyncService anno1Wrap1; private final IAsyncService anno0Wrap1; private final IAsyncService anno1Wrap0; private final IAsyncService anno0Wrap0; @RequestMapping("/") public String all() { var s = ""; s += process(anno1Wrap1); s += process(anno0Wrap1); s += process(anno1Wrap0); s += process(anno0Wrap0); return s; } private String process(IAsyncService service) { var startMs = System.currentTimeMillis(); var cf1 = service.method1(); var cf2 = service.method2(); var cf3 = service.method3(); var s = ""; s += cf1.join() + "\n"; s += cf2.join() + "\n"; s += cf3.join() + "\n"; s += lapseMs(startMs) + "ms" + "\n\n"; return s; } private long lapseMs(long startMs) { return System.currentTimeMillis() - startMs; } } interface IAsyncService { CompletableFuture<String> method1(); CompletableFuture<String> method2(); CompletableFuture<String> method3(); } @Component class Common { public String getClassMethod(Object o) { String[] a = o.getClass().getName().split("\\."); String className = a[a.length - 1].split("\\$")[0]; String methodName = o.getClass().getEnclosingMethod().getName(); return className + "." + methodName + "()"; } public Supplier<String> sleepSupplier(String s) { return () -> { try { Thread.sleep(100); } catch (InterruptedException e) {} return s; }; } public String sleepProcess(String s) { try { Thread.sleep(100); } catch (InterruptedException e) {} return s; } } @Service @RequiredArgsConstructor class Anno0Wrap0 implements IAsyncService { private final Common common; public CompletableFuture<String> method1() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.completedFuture(common.sleepProcess(s)); } public CompletableFuture<String> method2() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.completedFuture(common.sleepProcess(s)); } public CompletableFuture<String> method3() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.completedFuture(common.sleepProcess(s)); } } @Service @RequiredArgsConstructor class Anno0Wrap1 implements IAsyncService { private final Common common; public CompletableFuture<String> method1() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.supplyAsync(common.sleepSupplier(s)); } public CompletableFuture<String> method2() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.supplyAsync(common.sleepSupplier(s)); } public CompletableFuture<String> method3() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.supplyAsync(common.sleepSupplier(s)); } } @Service @RequiredArgsConstructor class Anno1Wrap0 implements IAsyncService { private final Common common; @Async public CompletableFuture<String> method1() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.completedFuture(common.sleepProcess(s)); } @Async public CompletableFuture<String> method2() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.completedFuture(common.sleepProcess(s)); } @Async public CompletableFuture<String> method3() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.completedFuture(common.sleepProcess(s)); } } @Service @RequiredArgsConstructor class Anno1Wrap1 implements IAsyncService { private final Common common; @Async public CompletableFuture<String> method1() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.supplyAsync(common.sleepSupplier(s)); } @Async public CompletableFuture<String> method2() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.supplyAsync(common.sleepSupplier(s)); } @Async public CompletableFuture<String> method3() { final String s = common.getClassMethod(new Object() {}); return CompletableFuture.supplyAsync(common.sleepSupplier(s)); } } |
(DemoApplication.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } |
curl localhost の結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Anno1Wrap1.method1() Anno1Wrap1.method2() Anno1Wrap1.method3() 108ms Anno0Wrap1.method1() Anno0Wrap1.method2() Anno0Wrap1.method3() 101ms Anno1Wrap0.method1() Anno1Wrap0.method2() Anno1Wrap0.method3() 101ms Anno0Wrap0.method1() Anno0Wrap0.method2() Anno0Wrap0.method3() 302ms |