Skip to content

Commit 41012e2

Browse files
committed
启用虚拟线程
1 parent 36ad58b commit 41012e2

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

frameworks/Java/smart-socket/src/main/java/org/smartboot/http/MultipleQueriesHandler.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.sql.PreparedStatement;
1212
import java.sql.ResultSet;
1313
import java.sql.SQLException;
14+
import java.util.concurrent.CompletableFuture;
1415
import java.util.concurrent.ThreadLocalRandom;
1516

1617
/**
@@ -25,28 +26,35 @@ public MultipleQueriesHandler(DataSource dataSource) {
2526
}
2627

2728
@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);
4355
}
56+
});
4457

45-
} catch (SQLException throwables) {
46-
throwables.printStackTrace();
47-
}
48-
response.setContentType("application/json");
49-
JsonUtil.writeJsonBytes(response, worlds);
5058
}
5159

5260
protected int getRandomNumber() {

frameworks/Java/smart-socket/src/main/java/org/smartboot/http/SingleQueryHandler.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.sql.PreparedStatement;
1111
import java.sql.ResultSet;
1212
import java.sql.SQLException;
13+
import java.util.concurrent.CompletableFuture;
1314
import java.util.concurrent.ThreadLocalRandom;
1415

1516
/**
@@ -24,20 +25,26 @@ public SingleQueryHandler(DataSource dataSource) {
2425
}
2526

2627
@Override
27-
public void handle(HttpRequest httpRequest, HttpResponse response) throws IOException {
28-
World world = new World();
29-
try (Connection connection = dataSource.getConnection();
30-
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM World WHERE id=?");) {
31-
preparedStatement.setInt(1, getRandomNumber());
32-
ResultSet resultSet = preparedStatement.executeQuery();
33-
resultSet.next();
34-
world.setId(resultSet.getInt(1));
35-
world.setRandomNumber(resultSet.getInt(2));
36-
} catch (SQLException throwables) {
37-
throwables.printStackTrace();
38-
}
39-
response.setContentType("application/json");
40-
JsonUtil.writeJsonBytes(response, world);
28+
public void handle(HttpRequest httpRequest, HttpResponse response, CompletableFuture<Object> completableFuture) throws IOException {
29+
Thread.startVirtualThread(() -> {
30+
try {
31+
World world = new World();
32+
try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM World WHERE id=?");) {
33+
preparedStatement.setInt(1, getRandomNumber());
34+
ResultSet resultSet = preparedStatement.executeQuery();
35+
resultSet.next();
36+
world.setId(resultSet.getInt(1));
37+
world.setRandomNumber(resultSet.getInt(2));
38+
} catch (SQLException throwables) {
39+
throwables.printStackTrace();
40+
}
41+
response.setContentType("application/json");
42+
JsonUtil.writeJsonBytes(response, world);
43+
} finally {
44+
completableFuture.complete(null);
45+
}
46+
});
47+
4148
}
4249

4350
protected int getRandomNumber() {

frameworks/Java/smart-socket/src/main/java/org/smartboot/servlet/Bootstrap.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static void main(String[] args) throws Throwable {
1818
ContainerRuntime containerRuntime = new ContainerRuntime();
1919
// plaintext
2020
ServletContextRuntime applicationRuntime = new ServletContextRuntime(null, Thread.currentThread().getContextClassLoader(), "/");
21+
applicationRuntime.setVendorProvider(response -> {
22+
23+
});
2124
ServletInfo plainTextServletInfo = new ServletInfo();
2225
plainTextServletInfo.setServletName("plaintext");
2326
plainTextServletInfo.setServletClass(HelloWorldServlet.class.getName());

0 commit comments

Comments
 (0)