diff --git a/frameworks/Java/smart-socket/config.toml b/frameworks/Java/smart-socket/config.toml index fbebc43a6a3..54e7e13d9d7 100644 --- a/frameworks/Java/smart-socket/config.toml +++ b/frameworks/Java/smart-socket/config.toml @@ -1,12 +1,12 @@ [framework] -name = "smart-socket" +name = "feat" [main] urls.plaintext = "/plaintext" urls.json = "/json" -urls.db = "/db" -urls.query = "/queries?queries=" -urls.update = "/updates?queries=" +#urls.db = "/db" +#urls.query = "/queries?queries=" +#urls.update = "/updates?queries=" approach = "Realistic" classification = "Platform" database = "Postgres" @@ -15,7 +15,7 @@ os = "Linux" orm = "Raw" platform = "smartboot" webserver = "None" -versus = "smart-socket" +versus = "feat" [smart-servlet] urls.plaintext = "/plaintext" diff --git a/frameworks/Java/smart-socket/pom.xml b/frameworks/Java/smart-socket/pom.xml index afdeea27141..4fe3e7a155f 100644 --- a/frameworks/Java/smart-socket/pom.xml +++ b/frameworks/Java/smart-socket/pom.xml @@ -11,7 +11,7 @@ 21 21 2.17.1 - 2.7 + 2.8 5.0.0 0.9.23 diff --git a/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/Bootstrap.java b/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/Bootstrap.java index 4875d68c8d8..396b6d02900 100755 --- a/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/Bootstrap.java +++ b/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/Bootstrap.java @@ -8,16 +8,12 @@ package org.smartboot.http; -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; import org.smartboot.Message; +import tech.smartboot.feat.core.Feat; +import tech.smartboot.feat.core.common.enums.HeaderValueEnum; +import tech.smartboot.feat.core.server.HttpHandler; import tech.smartboot.feat.core.server.HttpRequest; import tech.smartboot.feat.core.server.HttpResponse; -import tech.smartboot.feat.core.server.HttpServer; -import tech.smartboot.feat.core.server.HttpServerHandler; -import tech.smartboot.feat.core.server.handler.HttpRouteHandler; - -import javax.sql.DataSource; public class Bootstrap { static byte[] body = "Hello, World!".getBytes(); @@ -25,45 +21,42 @@ public class Bootstrap { public static void main(String[] args) { int cpuNum = Runtime.getRuntime().availableProcessors(); // 定义服务器接受的消息类型以及各类消息对应的处理器 - HttpServer bootstrap = new HttpServer(); - bootstrap.configuration() - .threadNum(cpuNum + 1) - .headerLimiter(0) - .readBufferSize(1024 * 4) - .writeBufferSize(1024 * 4); - bootstrap.httpHandler(new HttpServerHandler() { - @Override - public void handle(HttpRequest request, HttpResponse response) throws Throwable { - if ("/plaintext".equals(request.getRequestURI())) { - response.setContentLength(body.length); - response.setContentType("text/plain; charset=UTF-8"); - response.write(body); - } else if ("/json".equals(request.getRequestURI())) { - response.setContentType("application/json"); - JsonUtil.writeJsonBytes(response, new Message("Hello, World!")); - } + Feat.createHttpServer(options -> { + options.threadNum(cpuNum + 1) + .headerLimiter(0) + .readBufferSize(1024 * 4) + .writeBufferSize(1024 * 4); + }).httpHandler(request -> { + HttpResponse response = request.getResponse(); + if ("/plaintext".equals(request.getRequestURI())) { + response.setContentLength(body.length); + response.setContentType(HeaderValueEnum.ContentType.TEXT_PLAIN_UTF8); + response.write(body); + } else if ("/json".equals(request.getRequestURI())) { + response.setContentType("application/json"); + JsonUtil.writeJsonBytes(response, new Message("Hello, World!")); } - }).setPort(8080).start(); + }).listen(8080); } - private static void initDB(HttpRouteHandler routeHandle) { - try { - Class.forName("org.postgresql.Driver"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - HikariConfig config = new HikariConfig(); - config.setJdbcUrl("jdbc:postgresql://tfb-database:5432/hello_world"); - config.setUsername("benchmarkdbuser"); - config.setPassword("benchmarkdbpass"); - config.setMaximumPoolSize(64); - config.addDataSourceProperty("cachePrepStmts", "true"); - config.addDataSourceProperty("prepStmtCacheSize", "250"); - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); - DataSource dataSource = new HikariDataSource(config); - routeHandle.route("/db", new SingleQueryHandler(dataSource)) - .route("/queries", new MultipleQueriesHandler(dataSource)) - .route("/updates", new UpdateHandler(dataSource)); -// .route("/fortunes", new FortunesHandler(dataSource)); - } +// private static void initDB(HttpRouteHandler routeHandle) { +// try { +// Class.forName("org.postgresql.Driver"); +// } catch (ClassNotFoundException e) { +// e.printStackTrace(); +// } +// HikariConfig config = new HikariConfig(); +// config.setJdbcUrl("jdbc:postgresql://tfb-database:5432/hello_world"); +// config.setUsername("benchmarkdbuser"); +// config.setPassword("benchmarkdbpass"); +// config.setMaximumPoolSize(64); +// config.addDataSourceProperty("cachePrepStmts", "true"); +// config.addDataSourceProperty("prepStmtCacheSize", "250"); +// config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); +// DataSource dataSource = new HikariDataSource(config); +// routeHandle.route("/db", new SingleQueryHandler(dataSource)) +// .route("/queries", new MultipleQueriesHandler(dataSource)) +// .route("/updates", new UpdateHandler(dataSource)); +//// .route("/fortunes", new FortunesHandler(dataSource)); +// } } diff --git a/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/MultipleQueriesHandler.java b/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/MultipleQueriesHandler.java index ac4807a9284..f9b6b4faa5b 100644 --- a/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/MultipleQueriesHandler.java +++ b/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/MultipleQueriesHandler.java @@ -4,7 +4,7 @@ import tech.smartboot.feat.core.common.utils.NumberUtils; import tech.smartboot.feat.core.server.HttpRequest; import tech.smartboot.feat.core.server.HttpResponse; -import tech.smartboot.feat.core.server.HttpServerHandler; +import tech.smartboot.feat.core.server.handler.BaseHttpHandler; import javax.sql.DataSource; import java.io.IOException; @@ -19,7 +19,7 @@ * @author 三刀 * @version V1.0 , 2020/6/16 */ -public class MultipleQueriesHandler extends HttpServerHandler { +public class MultipleQueriesHandler extends BaseHttpHandler { private DataSource dataSource; public MultipleQueriesHandler(DataSource dataSource) { @@ -27,7 +27,8 @@ public MultipleQueriesHandler(DataSource dataSource) { } @Override - public void handle(HttpRequest httpRequest, HttpResponse response, CompletableFuture completableFuture) throws IOException { + public void handle(HttpRequest httpRequest, CompletableFuture completableFuture) throws IOException { + HttpResponse response = httpRequest.getResponse(); Thread.startVirtualThread(() -> { try { int queries = Math.min(Math.max(NumberUtils.toInt(httpRequest.getParameter("queries"), 1), 1), 500); diff --git a/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/SingleQueryHandler.java b/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/SingleQueryHandler.java index 52a96b9ca57..12898f62b7d 100644 --- a/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/SingleQueryHandler.java +++ b/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/SingleQueryHandler.java @@ -3,7 +3,7 @@ import tech.smartboot.feat.core.server.HttpRequest; import tech.smartboot.feat.core.server.HttpResponse; -import tech.smartboot.feat.core.server.HttpServerHandler; +import tech.smartboot.feat.core.server.handler.BaseHttpHandler; import javax.sql.DataSource; import java.io.IOException; @@ -18,7 +18,7 @@ * @author 三刀 * @version V1.0 , 2020/6/16 */ -public class SingleQueryHandler extends HttpServerHandler { +public class SingleQueryHandler extends BaseHttpHandler { private DataSource dataSource; public SingleQueryHandler(DataSource dataSource) { @@ -26,7 +26,8 @@ public SingleQueryHandler(DataSource dataSource) { } @Override - public void handle(HttpRequest httpRequest, HttpResponse response, CompletableFuture completableFuture) throws IOException { + public void handle(HttpRequest httpRequest, CompletableFuture completableFuture) throws IOException { + HttpResponse response = httpRequest.getResponse(); Thread.startVirtualThread(() -> { try { World world = new World(); diff --git a/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/UpdateHandler.java b/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/UpdateHandler.java index 3217adea343..b2b37a5f424 100644 --- a/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/UpdateHandler.java +++ b/frameworks/Java/smart-socket/src/main/java/org/smartboot/http/UpdateHandler.java @@ -2,9 +2,9 @@ import tech.smartboot.feat.core.common.utils.NumberUtils; +import tech.smartboot.feat.core.server.HttpHandler; import tech.smartboot.feat.core.server.HttpRequest; import tech.smartboot.feat.core.server.HttpResponse; -import tech.smartboot.feat.core.server.HttpServerHandler; import javax.sql.DataSource; import java.io.IOException; @@ -19,7 +19,7 @@ * @author 三刀 * @version V1.0 , 2020/6/16 */ -public class UpdateHandler extends HttpServerHandler { +public class UpdateHandler implements HttpHandler { private DataSource dataSource; public UpdateHandler(DataSource dataSource) { @@ -27,8 +27,9 @@ public UpdateHandler(DataSource dataSource) { } @Override - public void handle(HttpRequest httpRequest, HttpResponse response) throws IOException { - int queries = Math.min(Math.max(NumberUtils.toInt(httpRequest.getParameter("queries"), 1), 1), 500); + public void handle(HttpRequest request) throws IOException { + HttpResponse response = request.getResponse(); + int queries = Math.min(Math.max(NumberUtils.toInt(request.getParameter("queries"), 1), 1), 500); World[] worlds = new World[queries]; StringJoiner updateSql = new StringJoiner( ", ",