Skip to content

Commit

Permalink
[fix] added vertx multiple-core support (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcnyin authored Jun 26, 2023
1 parent d19b41e commit 15a84df
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions java_vertx_grpc_bench/src/main/java/vertx/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<HelloRequest, HelloReply> 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<Void> startPromise) throws Exception {
GrpcServer grpcServer = GrpcServer.server(vertx);
grpcServer.callHandler(GreeterGrpc.getSayHelloMethod(), request -> {
request.handler(helloRequest -> {
GrpcServerResponse<HelloRequest, HelloReply> 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();
});
}
}
}

0 comments on commit 15a84df

Please sign in to comment.