Browse Source

change thread model (#7719)

* update smart-servlet to 0.1.3-SNAPSHOT

* update aio-enhance to 1.0.3-SNAPSHOT

* smart-servlet bugfix

* bugfix

* update smart-socket to 1.5.6-SNAPSHOT

* remove file

* update aio-enhance to 1.0.4-SNAPSHOT

* 优化代码

* 优化代码

* update smart-socket to 1.5.6

* config threadNum

* update smart-socket to 1.5.7-SNAPSHOT

* 优化代码

* update smart-socket to 1.5.10-SNAPSHOT

* 优化代码

* 优化代码

* 优化代码

* 异常aio-enhance

* 优化代码

* 优化代码

* 优化代码

* remove aio-pro

* remove headerLimiter

* update hikaricp version

* replace json util

* 更新线程模型

* upgrade smart-servlet to 0.1.9-SNAPSHOT
三刀 2 years ago
parent
commit
4551d1aa1e

+ 1 - 1
frameworks/Java/smart-socket/pom.xml

@@ -11,7 +11,7 @@
         <maven.compiler.source>11</maven.compiler.source>
         <maven.compiler.target>11</maven.compiler.target>
         <log4j.version>2.17.1</log4j.version>
-        <smartservlet.version>0.1.7-SNAPSHOT</smartservlet.version>
+        <smartservlet.version>0.1.9-SNAPSHOT</smartservlet.version>
         <hikaricp.version>5.0.0</hikaricp.version>
         <jsoniter.version>0.9.23</jsoniter.version>
     </properties>

+ 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