Skip to content

Commit

Permalink
Update gRPC examples to use VerticleBase instead of AbstractVerticle
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Oct 19, 2024
1 parent 04cdb7e commit dff767b
Show file tree
Hide file tree
Showing 28 changed files with 244 additions and 217 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.grpc.consumer;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.net.SocketAddress;
import io.vertx.example.grpc.Messages;
import io.vertx.example.grpc.VertxConsumerServiceGrpcClient;
Expand All @@ -12,20 +13,22 @@
/*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
public class Client extends AbstractVerticle {
public class Client extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Client.class.getName()});
}

private GrpcClient client;

@Override
public void start() {
public Future<?> start() {

// Create the client
GrpcClient client = GrpcClient.client(vertx);
client = GrpcClient.client(vertx);

// Call the remote service
client.request(SocketAddress.inetSocketAddress(8080, "localhost"), VertxConsumerServiceGrpcClient.StreamingOutputCall)
return client.request(SocketAddress.inetSocketAddress(8080, "localhost"), VertxConsumerServiceGrpcClient.StreamingOutputCall)
.compose(request -> {
request
.end(Messages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package io.vertx.example.grpc.consumer;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.net.SocketAddress;
import io.vertx.example.grpc.Messages;
import io.vertx.example.grpc.VertxConsumerServiceGrpcClient;
import io.vertx.grpc.client.GrpcClient;

import io.vertx.launcher.application.VertxApplication;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
import java.util.stream.Collectors;

/*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
public class ClientWithStub extends AbstractVerticle {
public class ClientWithStub extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{ClientWithStub.class.getName()});
}

private GrpcClient client;

@Override
public void start() {
public Future<?> start() {

// Create the channel
GrpcClient client = GrpcClient.client(vertx);
client = GrpcClient.client(vertx);

// Get a stub to use for interacting with the remote service
VertxConsumerServiceGrpcClient stub = new VertxConsumerServiceGrpcClient(client, SocketAddress.inetSocketAddress(8080, "localhost"));
Expand All @@ -34,13 +39,13 @@ public void start() {
.build();

// Call the remote service
stub.streamingOutputCall(request)
.onSuccess(response -> {
return stub
.streamingOutputCall(request)
.compose(response -> {
response.handler(msg -> {
System.out.println(new String(msg.getPayload().toByteArray(), StandardCharsets.UTF_8));
}).endHandler(v -> {
System.out.println("Response has ended.");
});
return response.last().map("Response has ended.");
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.vertx.example.grpc.consumer;

import com.google.protobuf.ByteString;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.example.grpc.Messages;
import io.vertx.example.grpc.Messages.PayloadType;
import io.vertx.example.grpc.VertxConsumerServiceGrpcServer;
Expand All @@ -14,14 +15,14 @@
/*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
public class Server extends AbstractVerticle {
public class Server extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Server.class.getName()});
}

@Override
public void start() {
public Future<?> start() {

// Create the server
GrpcServer rpcServer = GrpcServer.server(vertx);
Expand All @@ -40,9 +41,9 @@ public void start() {
});

// start the server
vertx.createHttpServer().requestHandler(rpcServer).listen(8080)
.onFailure(cause -> {
cause.printStackTrace();
});
return vertx
.createHttpServer()
.requestHandler(rpcServer)
.listen(8080);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.vertx.example.grpc.consumer;

import com.google.protobuf.ByteString;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.streams.WriteStream;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.example.grpc.Messages;
import io.vertx.example.grpc.Messages.PayloadType;
import io.vertx.example.grpc.VertxConsumerServiceGrpcServer;
import io.vertx.grpc.common.GrpcWriteStream;
import io.vertx.grpc.server.GrpcServer;
import io.vertx.launcher.application.VertxApplication;

Expand All @@ -15,19 +16,19 @@
/*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
public class ServerWithStub extends AbstractVerticle {
public class ServerWithStub extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{ServerWithStub.class.getName()});
}

@Override
public void start() {
public Future<?> start() {

// The rpc service
VertxConsumerServiceGrpcServer.ConsumerServiceApi service = new VertxConsumerServiceGrpcServer.ConsumerServiceApi() {
@Override
public void streamingOutputCall(Messages.StreamingOutputCallRequest request, WriteStream<Messages.StreamingOutputCallResponse> response) {
public void streamingOutputCall(Messages.StreamingOutputCallRequest request, GrpcWriteStream<Messages.StreamingOutputCallResponse> response) {
final AtomicInteger counter = new AtomicInteger();
vertx.setPeriodic(1000L, t -> {
response.write(Messages.StreamingOutputCallResponse.newBuilder().setPayload(
Expand All @@ -44,12 +45,12 @@ public void streamingOutputCall(Messages.StreamingOutputCallRequest request, Wri
GrpcServer rpcServer = GrpcServer.server(vertx);

// Bind the service
service.bind_streamingOutputCall(rpcServer);
service.bindAll(rpcServer);

// start the server
vertx.createHttpServer().requestHandler(rpcServer).listen(8080)
.onFailure(cause -> {
cause.printStackTrace();
});
return vertx
.createHttpServer()
.requestHandler(rpcServer)
.listen(8080);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.grpc.conversation;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.net.SocketAddress;
import io.vertx.example.grpc.Messages;
import io.vertx.example.grpc.VertxConsumerServiceGrpcClient;
Expand All @@ -10,20 +11,22 @@
/*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
public class Client extends AbstractVerticle {
public class Client extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Client.class.getName()});
}

private GrpcClient client;

@Override
public void start() {
public Future<?> start() {

// Create the client
GrpcClient client = GrpcClient.client(vertx);
client = GrpcClient.client(vertx);

// Call the remote service
client.request(SocketAddress.inetSocketAddress(8080, "localhost"), VertxConsumerServiceGrpcClient.StreamingOutputCall)
return client.request(SocketAddress.inetSocketAddress(8080, "localhost"), VertxConsumerServiceGrpcClient.StreamingOutputCall)
.compose(
request -> {
// start the conversation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.grpc.conversation;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.net.SocketAddress;
import io.vertx.example.grpc.Messages;
import io.vertx.example.grpc.VertxConversationalServiceGrpcClient;
Expand All @@ -10,23 +11,25 @@
/*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
public class ClientWithStub extends AbstractVerticle {
public class ClientWithStub extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{ClientWithStub.class.getName()});
}

private GrpcClient client;

@Override
public void start() {
public Future<?> start() {

// Create the channel
GrpcClient client = GrpcClient.client(vertx);
client = GrpcClient.client(vertx);

// Get a stub to use for interacting with the remote service
VertxConversationalServiceGrpcClient stub = new VertxConversationalServiceGrpcClient(client, SocketAddress.inetSocketAddress(8080, "localhost"));

// Call the remote service
stub.fullDuplexCall(writeStream -> {
return stub.fullDuplexCall(writeStream -> {
// start the conversation
writeStream.write(Messages.StreamingOutputCallRequest.newBuilder().build());
vertx.setTimer(500L, t -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.grpc.conversation;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.example.grpc.Messages;
import io.vertx.example.grpc.VertxConsumerServiceGrpcServer;
import io.vertx.grpc.server.GrpcServer;
Expand All @@ -9,14 +10,14 @@
/*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
public class Server extends AbstractVerticle {
public class Server extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Server.class.getName()});
}

@Override
public void start() {
public Future<?> start() {

// Create the server
GrpcServer rpcServer = GrpcServer.server(vertx);
Expand All @@ -30,9 +31,9 @@ public void start() {
});

// start the server
vertx.createHttpServer().requestHandler(rpcServer).listen(8080)
.onFailure(cause -> {
cause.printStackTrace();
});
return vertx
.createHttpServer()
.requestHandler(rpcServer)
.listen(8080);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package io.vertx.example.grpc.conversation;


import io.vertx.core.AbstractVerticle;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.example.grpc.Messages;
import io.vertx.example.grpc.VertxConversationalServiceGrpcServer;
import io.vertx.grpc.common.GrpcReadStream;
import io.vertx.grpc.common.GrpcWriteStream;
import io.vertx.grpc.server.GrpcServer;
import io.vertx.launcher.application.VertxApplication;

/*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
public class ServerWithStub extends AbstractVerticle {
public class ServerWithStub extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{ServerWithStub.class.getName()});
}

@Override
public void start() {
public Future<?> start() {

// The rpc service
VertxConversationalServiceGrpcServer.ConversationalServiceApi service = new VertxConversationalServiceGrpcServer.ConversationalServiceApi() {
@Override
public void fullDuplexCall(ReadStream<Messages.StreamingOutputCallRequest> request, WriteStream<Messages.StreamingOutputCallResponse> response) {
public void fullDuplexCall(GrpcReadStream<Messages.StreamingOutputCallRequest> request, GrpcWriteStream<Messages.StreamingOutputCallResponse> response) {
request
.handler(req -> {
System.out.println("Server: received request");
Expand All @@ -39,12 +39,12 @@ public void fullDuplexCall(ReadStream<Messages.StreamingOutputCallRequest> reque
GrpcServer rpcServer = GrpcServer.server(vertx);

// Bind the service
service.bind_fullDuplexCall(rpcServer);
service.bindAll(rpcServer);

// start the server
vertx.createHttpServer().requestHandler(rpcServer).listen(8080)
.onFailure(cause -> {
cause.printStackTrace();
});
return vertx
.createHttpServer()
.requestHandler(rpcServer)
.listen(8080);
}
}
Loading

0 comments on commit dff767b

Please sign in to comment.