Skip to content

Commit e3cc9c8

Browse files
committed
Use counter value as payload body
1 parent 6647064 commit e3cc9c8

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

grpc-examples/src/main/java/io/vertx/example/grpc/consumer/Client.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.vertx.example.grpc.Messages;
77
import io.vertx.example.util.Runner;
88
import io.vertx.grpc.VertxChannelBuilder;
9+
import java.nio.charset.Charset;
910

1011
/*
1112
* @author <a href="mailto:[email protected]">Paulo Lopes</a>
@@ -30,12 +31,16 @@ public void start() throws Exception {
3031
ConsumerServiceGrpc.ConsumerServiceVertxStub stub = ConsumerServiceGrpc.newVertxStub(channel);
3132

3233
// Make a request
33-
Messages.StreamingOutputCallRequest request = Messages.StreamingOutputCallRequest.newBuilder().build();
34+
Messages.StreamingOutputCallRequest request = Messages
35+
.StreamingOutputCallRequest
36+
.newBuilder()
37+
.build();
3438

3539
// Call the remote service
3640
stub.streamingOutputCall(request, stream -> {
3741
stream.handler(response -> {
38-
System.out.println(response.getPayload().getType().getNumber());
42+
System.out
43+
.println(new String(response.getPayload().toByteArray(), Charset.forName("UTF-8")));
3944
}).endHandler(v -> {
4045
System.out.println("Response has ended.");
4146
});

grpc-examples/src/main/java/io/vertx/example/grpc/consumer/Server.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package io.vertx.example.grpc.consumer;
22

3+
import com.google.protobuf.ByteString;
34
import io.vertx.core.AbstractVerticle;
4-
import io.vertx.example.grpc.*;
5+
import io.vertx.example.grpc.ConsumerServiceGrpc;
6+
import io.vertx.example.grpc.Messages;
7+
import io.vertx.example.grpc.Messages.PayloadType;
58
import io.vertx.example.util.Runner;
69
import io.vertx.grpc.GrpcWriteStream;
710
import io.vertx.grpc.VertxServer;
811
import io.vertx.grpc.VertxServerBuilder;
9-
12+
import java.nio.charset.Charset;
1013
import java.util.concurrent.atomic.AtomicInteger;
1114

1215
/*
@@ -23,15 +26,24 @@ public static void main(String[] args) {
2326
public void start() throws Exception {
2427

2528
// The rcp service
26-
ConsumerServiceGrpc.ConsumerServiceVertxImplBase service = new ConsumerServiceGrpc.ConsumerServiceVertxImplBase() {
27-
@Override
28-
public void streamingOutputCall(Messages.StreamingOutputCallRequest request, GrpcWriteStream<Messages.StreamingOutputCallResponse> response) {
29-
final AtomicInteger counter = new AtomicInteger();
30-
vertx.setPeriodic(1000L, t -> {
31-
response.write(Messages.StreamingOutputCallResponse.newBuilder().setPayload(Messages.Payload.newBuilder().setTypeValue(counter.incrementAndGet())).build());
32-
});
33-
}
34-
};
29+
ConsumerServiceGrpc.ConsumerServiceVertxImplBase service =
30+
new ConsumerServiceGrpc.ConsumerServiceVertxImplBase() {
31+
@Override
32+
public void streamingOutputCall(
33+
Messages.StreamingOutputCallRequest request,
34+
GrpcWriteStream<Messages.StreamingOutputCallResponse> response
35+
) {
36+
final AtomicInteger counter = new AtomicInteger();
37+
vertx.setPeriodic(1000L, t -> {
38+
response.write(Messages.StreamingOutputCallResponse.newBuilder().setPayload(
39+
Messages.Payload.newBuilder()
40+
.setTypeValue(PayloadType.COMPRESSABLE.getNumber())
41+
.setBody(ByteString.copyFrom(
42+
String.valueOf(counter.incrementAndGet()), Charset.forName("UTF-8")))
43+
).build());
44+
});
45+
}
46+
};
3547

3648
// Create the server
3749
VertxServer rpcServer = VertxServerBuilder

0 commit comments

Comments
 (0)