Skip to content

Commit 10b85fe

Browse files
committed
Use Vertx generated stubs instead of gRPC.io
Closes #1
1 parent edb65ab commit 10b85fe

File tree

3 files changed

+96
-24
lines changed

3 files changed

+96
-24
lines changed

pom.xml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
<groupId>io.vertx</groupId>
3939
<artifactId>vertx-grpc-server</artifactId>
4040
</dependency>
41-
<dependency>
42-
<groupId>io.vertx</groupId>
43-
<artifactId>vertx-grpcio-server</artifactId>
44-
</dependency>
4541
<dependency>
4642
<groupId>com.google.protobuf</groupId>
4743
<artifactId>protobuf-java</artifactId>
@@ -85,16 +81,7 @@
8581
</configuration>
8682
<executions>
8783
<execution>
88-
<id>compile</id>
89-
<configuration>
90-
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
91-
</configuration>
92-
<goals>
93-
<goal>compile</goal>
94-
</goals>
95-
</execution>
96-
<execution>
97-
<id>compile-grpc-java</id>
84+
<id>compile-java</id>
9885
<configuration>
9986
<pluginId>grpc-java</pluginId>
10087
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
@@ -112,6 +99,7 @@
11299
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
113100
</configuration>
114101
<goals>
102+
<goal>compile</goal>
115103
<goal>compile-custom</goal>
116104
</goals>
117105
</execution>

src/main/java/io/vertx/howtos/grpcweb/ServerVerticle.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
package io.vertx.howtos.grpcweb;
22

3-
import io.grpc.stub.StreamObserver;
43
import io.vertx.core.Future;
54
import io.vertx.core.VerticleBase;
65
import io.vertx.core.Vertx;
76
import io.vertx.ext.web.Router;
87
import io.vertx.ext.web.handler.StaticHandler;
9-
import io.vertx.grpcio.server.GrpcIoServer;
10-
import io.vertx.grpcio.server.GrpcIoServiceBridge;
8+
import io.vertx.grpc.server.GrpcServer;
119

1210
public class ServerVerticle extends VerticleBase {
1311

1412
@Override
1513
public Future<?> start() {
1614
// tag::grpcServer[]
17-
GreeterGrpc.GreeterImplBase service = new GreeterGrpc.GreeterImplBase() {
15+
VertxGreeterGrpcServer.GreeterApi stub = new VertxGreeterGrpcServer.GreeterApi() {
1816
@Override
19-
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
20-
responseObserver.onNext(HelloReply.newBuilder().setMessage("Hello " + request.getName()).build());
21-
responseObserver.onCompleted();
17+
public Future<HelloReply> sayHello(HelloRequest request) {
18+
return Future.succeededFuture(HelloReply.newBuilder().setMessage("Hello " + request.getName()).build());
2219
}
2320
};
2421

25-
GrpcIoServer grpcServer = GrpcIoServer.server(vertx);
26-
GrpcIoServiceBridge serverStub = GrpcIoServiceBridge.bridge(service);
27-
serverStub.bind(grpcServer);
22+
GrpcServer grpcServer = GrpcServer.server(vertx);
23+
stub.bindAll(grpcServer);
2824
// end::grpcServer[]
2925

3026
// tag::routerAndServer[]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.vertx.howtos.grpcweb;
2+
3+
import io.vertx.core.Future;
4+
import io.vertx.core.Promise;
5+
import io.vertx.core.Handler;
6+
import io.vertx.core.http.HttpServerRequest;
7+
import io.vertx.core.streams.ReadStream;
8+
import io.vertx.core.streams.WriteStream;
9+
import io.vertx.grpc.common.GrpcStatus;
10+
import io.vertx.grpc.common.ServiceName;
11+
import io.vertx.grpc.common.ServiceMethod;
12+
import io.vertx.grpc.common.GrpcReadStream;
13+
import io.vertx.grpc.common.GrpcWriteStream;
14+
import io.vertx.grpc.common.GrpcMessageDecoder;
15+
import io.vertx.grpc.common.GrpcMessageEncoder;
16+
import io.vertx.grpc.server.GrpcServerResponse;
17+
import io.vertx.grpc.server.GrpcServer;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
public class VertxGreeterGrpcServer {
23+
24+
public static final ServiceMethod<io.vertx.howtos.grpcweb.HelloRequest, io.vertx.howtos.grpcweb.HelloReply> SayHello = ServiceMethod.server(
25+
ServiceName.create("helloworld", "Greeter"),
26+
"SayHello",
27+
GrpcMessageEncoder.encoder(),
28+
GrpcMessageDecoder.decoder(io.vertx.howtos.grpcweb.HelloRequest.parser()));
29+
public static final ServiceMethod<io.vertx.howtos.grpcweb.HelloRequest, io.vertx.howtos.grpcweb.HelloReply> SayHello_JSON = ServiceMethod.server(
30+
ServiceName.create("helloworld", "Greeter"),
31+
"SayHello",
32+
GrpcMessageEncoder.json(),
33+
GrpcMessageDecoder.json(() -> io.vertx.howtos.grpcweb.HelloRequest.newBuilder()));
34+
35+
public static class GreeterApi {
36+
37+
public Future<io.vertx.howtos.grpcweb.HelloReply> sayHello(io.vertx.howtos.grpcweb.HelloRequest request) {
38+
throw new UnsupportedOperationException("Not implemented");
39+
}
40+
public void sayHello(io.vertx.howtos.grpcweb.HelloRequest request, Promise<io.vertx.howtos.grpcweb.HelloReply> response) {
41+
sayHello(request)
42+
.onSuccess(msg -> response.complete(msg))
43+
.onFailure(error -> response.fail(error));
44+
}
45+
46+
public final void handle_sayHello(io.vertx.grpc.server.GrpcServerRequest<io.vertx.howtos.grpcweb.HelloRequest, io.vertx.howtos.grpcweb.HelloReply> request) {
47+
Promise<io.vertx.howtos.grpcweb.HelloReply> promise = Promise.promise();
48+
request.handler(msg -> {
49+
try {
50+
sayHello(msg, promise);
51+
} catch (RuntimeException err) {
52+
promise.tryFail(err);
53+
}
54+
});
55+
promise.future()
56+
.onFailure(err -> request.response().status(GrpcStatus.INTERNAL).end())
57+
.onSuccess(resp -> request.response().end(resp));
58+
}
59+
public GreeterApi bind_sayHello(GrpcServer server) {
60+
return bind_sayHello(server, io.vertx.grpc.common.WireFormat.PROTOBUF);
61+
}
62+
public GreeterApi bind_sayHello(GrpcServer server, io.vertx.grpc.common.WireFormat format) {
63+
ServiceMethod<io.vertx.howtos.grpcweb.HelloRequest,io.vertx.howtos.grpcweb.HelloReply> serviceMethod;
64+
switch(format) {
65+
case PROTOBUF:
66+
serviceMethod = SayHello;
67+
break;
68+
case JSON:
69+
serviceMethod = SayHello_JSON;
70+
break;
71+
default:
72+
throw new AssertionError();
73+
}
74+
server.callHandler(serviceMethod, this::handle_sayHello);
75+
return this;
76+
}
77+
78+
public final GreeterApi bindAll(GrpcServer server) {
79+
bind_sayHello(server);
80+
return this;
81+
}
82+
83+
public final GreeterApi bindAll(GrpcServer server, io.vertx.grpc.common.WireFormat format) {
84+
bind_sayHello(server, format);
85+
return this;
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)