From 15a84dfa4700f739c52465e0c8fa3b8059d38978 Mon Sep 17 00:00:00 2001 From: gcnyin Date: Tue, 27 Jun 2023 00:01:12 +0800 Subject: [PATCH] [fix] added vertx multiple-core support (#359) --- .../src/main/java/vertx/Main.java | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/java_vertx_grpc_bench/src/main/java/vertx/Main.java b/java_vertx_grpc_bench/src/main/java/vertx/Main.java index 203895e5..5bb5f6c7 100644 --- a/java_vertx_grpc_bench/src/main/java/vertx/Main.java +++ b/java_vertx_grpc_bench/src/main/java/vertx/Main.java @@ -4,6 +4,8 @@ import io.grpc.examples.helloworld.Hello; import io.grpc.examples.helloworld.HelloReply; import io.grpc.examples.helloworld.HelloRequest; +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Promise; import io.vertx.core.Vertx; import io.vertx.core.http.HttpServer; import io.vertx.grpc.server.GrpcServer; @@ -12,16 +14,28 @@ public class Main { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); - GrpcServer grpcServer = GrpcServer.server(vertx); - grpcServer.callHandler(GreeterGrpc.getSayHelloMethod(), request -> { - request.handler(helloRequest -> { - GrpcServerResponse response = request.response(); - Hello hello = helloRequest.getRequest(); - HelloReply reply = HelloReply.newBuilder().setResponse(hello).build(); - response.end(reply); - }); + vertx.nettyEventLoopGroup().forEach(u -> { + vertx.deployVerticle(new ServerVerticle()); }); - HttpServer httpServer = vertx.createHttpServer(); - httpServer.requestHandler(grpcServer).listen(50051); + } + + static class ServerVerticle extends AbstractVerticle { + @Override + public void start(Promise startPromise) throws Exception { + GrpcServer grpcServer = GrpcServer.server(vertx); + grpcServer.callHandler(GreeterGrpc.getSayHelloMethod(), request -> { + request.handler(helloRequest -> { + GrpcServerResponse response = request.response(); + Hello hello = helloRequest.getRequest(); + HelloReply reply = HelloReply.newBuilder().setResponse(hello).build(); + response.end(reply); + }); + }); + HttpServer httpServer = vertx.createHttpServer(); + httpServer.requestHandler(grpcServer).listen(50051).andThen(u -> { + System.out.println("deployed: " + deploymentID()); + startPromise.complete(); + }); + } } }