11
11
import java .sql .PreparedStatement ;
12
12
import java .sql .ResultSet ;
13
13
import java .sql .SQLException ;
14
+ import java .util .concurrent .CompletableFuture ;
14
15
import java .util .concurrent .ThreadLocalRandom ;
15
16
16
17
/**
@@ -25,28 +26,35 @@ public MultipleQueriesHandler(DataSource dataSource) {
25
26
}
26
27
27
28
@ Override
28
- public void handle (HttpRequest httpRequest , HttpResponse response ) throws IOException {
29
- int queries = Math .min (Math .max (NumberUtils .toInt (httpRequest .getParameter ("queries" ), 1 ), 1 ), 500 );
30
- World [] worlds = new World [queries ];
31
- try (Connection connection = dataSource .getConnection ();
32
- PreparedStatement preparedStatement = connection .prepareStatement ("SELECT * FROM World WHERE id=?" );) {
33
-
34
- for (int i = 0 ; i < queries ; i ++) {
35
- preparedStatement .setInt (1 , getRandomNumber ());
36
- ResultSet resultSet = preparedStatement .executeQuery ();
37
- resultSet .next ();
38
- World world = new World ();
39
- world .setId (resultSet .getInt (1 ));
40
- world .setRandomNumber (resultSet .getInt (2 ));
41
- worlds [i ] = world ;
42
- preparedStatement .clearParameters ();
29
+ public void handle (HttpRequest httpRequest , HttpResponse response , CompletableFuture <Object > completableFuture ) throws IOException {
30
+ Thread .startVirtualThread (() -> {
31
+ try {
32
+ int queries = Math .min (Math .max (NumberUtils .toInt (httpRequest .getParameter ("queries" ), 1 ), 1 ), 500 );
33
+ World [] worlds = new World [queries ];
34
+ try (Connection connection = dataSource .getConnection ();
35
+ PreparedStatement preparedStatement = connection .prepareStatement ("SELECT * FROM World WHERE id=?" );) {
36
+
37
+ for (int i = 0 ; i < queries ; i ++) {
38
+ preparedStatement .setInt (1 , getRandomNumber ());
39
+ ResultSet resultSet = preparedStatement .executeQuery ();
40
+ resultSet .next ();
41
+ World world = new World ();
42
+ world .setId (resultSet .getInt (1 ));
43
+ world .setRandomNumber (resultSet .getInt (2 ));
44
+ worlds [i ] = world ;
45
+ preparedStatement .clearParameters ();
46
+ }
47
+
48
+ } catch (SQLException throwables ) {
49
+ throwables .printStackTrace ();
50
+ }
51
+ response .setContentType ("application/json" );
52
+ JsonUtil .writeJsonBytes (response , worlds );
53
+ } finally {
54
+ completableFuture .complete (null );
43
55
}
56
+ });
44
57
45
- } catch (SQLException throwables ) {
46
- throwables .printStackTrace ();
47
- }
48
- response .setContentType ("application/json" );
49
- JsonUtil .writeJsonBytes (response , worlds );
50
58
}
51
59
52
60
protected int getRandomNumber () {
0 commit comments