|
@@ -11,6 +11,7 @@ import java.sql.Connection;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
/**
|
|
@@ -25,28 +26,35 @@ public class MultipleQueriesHandler extends HttpServerHandler {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void handle(HttpRequest httpRequest, HttpResponse response) throws IOException {
|
|
|
- int queries = Math.min(Math.max(NumberUtils.toInt(httpRequest.getParameter("queries"), 1), 1), 500);
|
|
|
- World[] worlds = new World[queries];
|
|
|
- try (Connection connection = dataSource.getConnection();
|
|
|
- PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM World WHERE id=?");) {
|
|
|
-
|
|
|
- for (int i = 0; i < queries; i++) {
|
|
|
- preparedStatement.setInt(1, getRandomNumber());
|
|
|
- ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
- resultSet.next();
|
|
|
- World world = new World();
|
|
|
- world.setId(resultSet.getInt(1));
|
|
|
- world.setRandomNumber(resultSet.getInt(2));
|
|
|
- worlds[i] = world;
|
|
|
- preparedStatement.clearParameters();
|
|
|
+ public void handle(HttpRequest httpRequest, HttpResponse response, CompletableFuture<Object> completableFuture) throws IOException {
|
|
|
+ Thread.startVirtualThread(() -> {
|
|
|
+ try {
|
|
|
+ int queries = Math.min(Math.max(NumberUtils.toInt(httpRequest.getParameter("queries"), 1), 1), 500);
|
|
|
+ World[] worlds = new World[queries];
|
|
|
+ try (Connection connection = dataSource.getConnection();
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM World WHERE id=?");) {
|
|
|
+
|
|
|
+ for (int i = 0; i < queries; i++) {
|
|
|
+ preparedStatement.setInt(1, getRandomNumber());
|
|
|
+ ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
+ resultSet.next();
|
|
|
+ World world = new World();
|
|
|
+ world.setId(resultSet.getInt(1));
|
|
|
+ world.setRandomNumber(resultSet.getInt(2));
|
|
|
+ worlds[i] = world;
|
|
|
+ preparedStatement.clearParameters();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (SQLException throwables) {
|
|
|
+ throwables.printStackTrace();
|
|
|
+ }
|
|
|
+ response.setContentType("application/json");
|
|
|
+ JsonUtil.writeJsonBytes(response, worlds);
|
|
|
+ } finally {
|
|
|
+ completableFuture.complete(null);
|
|
|
}
|
|
|
+ });
|
|
|
|
|
|
- } catch (SQLException throwables) {
|
|
|
- throwables.printStackTrace();
|
|
|
- }
|
|
|
- response.setContentType("application/json");
|
|
|
- JsonUtil.writeJsonBytes(response, worlds);
|
|
|
}
|
|
|
|
|
|
protected int getRandomNumber() {
|