9
9
import io .micronaut .http .annotation .Controller ;
10
10
import io .micronaut .http .annotation .Get ;
11
11
import io .micronaut .http .annotation .QueryValue ;
12
+ import io .micronaut .scheduling .TaskExecutors ;
13
+ import jakarta .inject .Named ;
12
14
import views .fortunes ;
13
15
14
16
import java .util .ArrayList ;
15
17
import java .util .Comparator ;
16
18
import java .util .List ;
17
19
import java .util .concurrent .CompletionStage ;
20
+ import java .util .concurrent .Executor ;
18
21
19
22
import static java .util .Comparator .comparing ;
20
23
@@ -24,11 +27,14 @@ public class AsyncBenchmarkController extends AbstractBenchmarkController {
24
27
25
28
private final AsyncWorldRepository worldRepository ;
26
29
private final AsyncFortuneRepository fortuneRepository ;
30
+ private final Executor executor ;
27
31
28
32
public AsyncBenchmarkController (AsyncWorldRepository worldRepository ,
29
- AsyncFortuneRepository fortuneRepository ) {
33
+ AsyncFortuneRepository fortuneRepository ,
34
+ @ Named (TaskExecutors .BLOCKING ) Executor executor ) {
30
35
this .worldRepository = worldRepository ;
31
36
this .fortuneRepository = fortuneRepository ;
37
+ this .executor = executor ;
32
38
}
33
39
34
40
@ Get ("/prepare-data-for-test" )
@@ -39,7 +45,7 @@ public CompletionStage<?> prepareDataForTest() {
39
45
// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#single-database-query
40
46
@ Get ("/db" )
41
47
public CompletionStage <World > db () {
42
- return worldRepository .findById (randomId ());
48
+ return worldRepository .findById (randomId ()). thenApplyAsync ( world -> world , executor ) ;
43
49
}
44
50
45
51
// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#multiple-database-queries
@@ -50,20 +56,20 @@ public CompletionStage<List<World>> queries(@QueryValue String queries) {
50
56
for (int i = 0 ; i < count ; i ++) {
51
57
ids .add (randomId ());
52
58
}
53
- return worldRepository .findByIds (ids );
59
+ return worldRepository .findByIds (ids ). thenApplyAsync ( worlds -> worlds , executor ) ;
54
60
}
55
61
56
62
// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#fortunes
57
63
@ Get (value = "/fortunes" , produces = "text/html;charset=utf-8" )
58
64
public CompletionStage <HttpResponse <String >> fortune () {
59
- return fortuneRepository .findAll ().thenApply (fortuneList -> {
65
+ return fortuneRepository .findAll ().thenApplyAsync (fortuneList -> {
60
66
List <Fortune > all = new ArrayList <>(fortuneList .size () + 1 );
61
67
all .add (new Fortune (0 , "Additional fortune added at request time." ));
62
68
all .addAll (fortuneList );
63
69
all .sort (comparing (Fortune ::message ));
64
70
String body = fortunes .template (all ).render ().toString ();
65
71
return HttpResponse .ok (body ).contentType ("text/html;charset=utf-8" );
66
- });
72
+ }, executor );
67
73
}
68
74
69
75
// https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#database-updates
@@ -74,7 +80,7 @@ public CompletionStage<List<World>> updates(@QueryValue String queries) {
74
80
world .setRandomNumber (randomWorldNumber ());
75
81
}
76
82
worlds .sort (Comparator .comparingInt (World ::getId )); // Avoid deadlock
77
- return worldRepository .updateAll (worlds ).thenApply (ignore -> worlds );
83
+ return worldRepository .updateAll (worlds ).thenApplyAsync (ignore -> worlds , executor );
78
84
});
79
85
}
80
86
0 commit comments