Browse Source

更新线程模型

三刀 2 years ago
parent
commit
c882c08256

+ 17 - 5
frameworks/Java/smart-socket/src/main/java/org/smartboot/http/Bootstrap.java

@@ -19,11 +19,19 @@ import org.smartboot.http.server.handler.HttpRouteHandler;
 
 import javax.sql.DataSource;
 import java.io.IOException;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 public class Bootstrap {
     static byte[] body = "Hello, World!".getBytes();
 
     public static void main(String[] args) {
+        ExecutorService executorService = new ThreadPoolExecutor(1, 1,
+                0L, TimeUnit.MILLISECONDS,
+                new ArrayBlockingQueue<>(128), new ThreadPoolExecutor.CallerRunsPolicy());
         HttpRouteHandler routeHandle = new HttpRouteHandler();
         routeHandle
                 .route("/plaintext", new HttpServerHandler() {
@@ -37,12 +45,16 @@ public class Bootstrap {
                     }
                 })
                 .route("/json", new HttpServerHandler() {
-
                     @Override
-                    public void handle(HttpRequest request, HttpResponse response) throws IOException {
-
-                        response.setContentType("application/json");
-                        JsonUtil.writeJsonBytes(response, new Message("Hello, World!"));
+                    public void handle(HttpRequest request, HttpResponse response, CompletableFuture<Object> completableFuture) throws IOException {
+                        executorService.execute(() -> {
+                            try {
+                                response.setContentType("application/json");
+                                JsonUtil.writeJsonBytes(response, new Message("Hello, World!"));
+                            } finally {
+                                completableFuture.complete(null);
+                            }
+                        });
                     }
                 });
         initDB(routeHandle);

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

@@ -35,7 +35,7 @@ public class Bootstrap {
         jsonServletInfo.addMapping("/json");
         applicationRuntime.getDeploymentInfo().addServlet(jsonServletInfo);
         containerRuntime.addRuntime(applicationRuntime);
-        containerRuntime.start();
+
         int cpuNum = Runtime.getRuntime().availableProcessors();
         // 定义服务器接受的消息类型以及各类消息对应的处理器
         HttpBootstrap bootstrap = new HttpBootstrap();
@@ -46,6 +46,7 @@ public class Bootstrap {
                 .writeBufferSize(1024 * 4)
                 .readMemoryPool(16384 * 1024 * 4)
                 .writeMemoryPool(10 * 1024 * 1024 * cpuNum, cpuNum);
+        containerRuntime.start(bootstrap.configuration());
         bootstrap.setPort(8080)
                 .httpHandler(new HttpServerHandler() {
                     @Override