|
@@ -8,10 +8,9 @@
|
|
|
|
|
|
package org.smartboot.http;
|
|
|
|
|
|
-import com.jsoniter.output.JsonStream;
|
|
|
-import com.jsoniter.output.JsonStreamPool;
|
|
|
-import com.jsoniter.spi.JsonException;
|
|
|
-import com.jsoniter.spi.Slice;
|
|
|
+import com.zaxxer.hikari.HikariConfig;
|
|
|
+import com.zaxxer.hikari.HikariDataSource;
|
|
|
+import org.smartboot.aio.EnhanceAsynchronousChannelProvider;
|
|
|
import org.smartboot.http.server.HttpMessageProcessor;
|
|
|
import org.smartboot.http.server.HttpRequestProtocol;
|
|
|
import org.smartboot.http.server.Request;
|
|
@@ -24,47 +23,40 @@ import org.smartboot.socket.extension.processor.AbstractMessageProcessor;
|
|
|
import org.smartboot.socket.transport.AioQuickServer;
|
|
|
import org.smartboot.socket.transport.AioSession;
|
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
import java.io.IOException;
|
|
|
|
|
|
public class Bootstrap {
|
|
|
static byte[] body = "Hello, World!".getBytes();
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
-// System.setProperty("sun.nio.ch.maxCompletionHandlersOnStack", "32");
|
|
|
+ System.setProperty("java.nio.channels.spi.AsynchronousChannelProvider", EnhanceAsynchronousChannelProvider.class.getName());
|
|
|
+
|
|
|
HttpRouteHandle routeHandle = new HttpRouteHandle();
|
|
|
- routeHandle.route("/plaintext", new HttpHandle() {
|
|
|
+ routeHandle
|
|
|
+ .route("/plaintext", new HttpHandle() {
|
|
|
|
|
|
|
|
|
- @Override
|
|
|
- public void doHandle(HttpRequest request, HttpResponse response) throws IOException {
|
|
|
- response.setContentLength(body.length);
|
|
|
- response.setContentType("text/plain; charset=UTF-8");
|
|
|
- response.write(body);
|
|
|
- }
|
|
|
- }).route("/json", new HttpHandle() {
|
|
|
+ @Override
|
|
|
+ public void doHandle(HttpRequest request, HttpResponse response) throws IOException {
|
|
|
+ response.setContentLength(body.length);
|
|
|
+ response.setContentType("text/plain; charset=UTF-8");
|
|
|
+ response.write(body);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .route("/json", new HttpHandle() {
|
|
|
|
|
|
- @Override
|
|
|
- public void doHandle(HttpRequest request, HttpResponse response) throws IOException {
|
|
|
+ @Override
|
|
|
+ public void doHandle(HttpRequest request, HttpResponse response) throws IOException {
|
|
|
|
|
|
- response.setContentType("application/json");
|
|
|
- JsonStream stream = JsonStreamPool.borrowJsonStream();
|
|
|
- try {
|
|
|
- stream.reset(null);
|
|
|
- stream.writeVal(Message.class, new Message("Hello, World!"));
|
|
|
- Slice slice = stream.buffer();
|
|
|
- response.setContentLength(slice.tail());
|
|
|
- response.getOutputStream().write(slice.data(), 0, slice.tail());
|
|
|
- } catch (IOException e) {
|
|
|
- throw new JsonException(e);
|
|
|
- } finally {
|
|
|
- JsonStreamPool.returnJsonStream(stream);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ response.setContentType("application/json");
|
|
|
+ JsonUtil.writeJsonBytes(response, new Message("Hello, World!"));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ initDB(routeHandle);
|
|
|
HttpMessageProcessor processor = new HttpMessageProcessor();
|
|
|
processor.pipeline(routeHandle);
|
|
|
http(processor);
|
|
|
-// https(processor);
|
|
|
}
|
|
|
|
|
|
public static void http(final HttpMessageProcessor processor) {
|
|
@@ -102,4 +94,21 @@ public class Bootstrap {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private static void initDB(HttpRouteHandle 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);
|
|
|
+ DataSource dataSource = new HikariDataSource(config);
|
|
|
+ routeHandle.route("/db", new SingleQueryHandler(dataSource))
|
|
|
+ .route("/queries", new MultipleQueriesHandler(dataSource))
|
|
|
+ .route("/updates", new UpdateHandler(dataSource));
|
|
|
+ }
|
|
|
}
|