I want to return a List of strings using gRPC. In my program it seams to
add a string to the repeated value fine, but when I return the value to the
calling program it's empty. Here is my code:
my.proto
syntax = "proto3";
option java_multiple_files = true;
option java_package = my.package.name.testsend";
option java_outer_classname = "TestSendProto";
option objc_class_prefix = "TS";
package testsend;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayHelloAgain (HelloRequest) returns (ArrayHelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
message ArrayHelloReply {
repeated string message2 = 2;
}
////////////////////////////// server /////////////////////////////////
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.io.*;
/**
*
* @author yotimbo
*/
public class yoRecvo {
private static final Logger logger =
Logger.getLogger(yoRecvo.class.getName());
/* The port on which the server should run */
private int port = 50051;
private Server server;
private void start() throws IOException {
server = ServerBuilder.forPort(port)
.addService(new GreeterImpl())
.build()
.start();
logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// Use stderr here since the logger may have been reset by its JVM
shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is
shutting down");
yoRecvo.this.stop();
System.err.println("*** server shut down");
}
});
}
private void stop() {
if (server != null) {
server.shutdown();
}
}
/**
* Await termination on the main thread since the grpc library uses
daemon threads.
*/
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
/**
* Main launches the server from the command line.
*/
public static void main(String[] args) throws IOException,
InterruptedException {
final yoRecvo server = new yoRecvo();
server.start();
server.blockUntilShutdown();
}
private class GreeterImpl extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply>
responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " +
req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
@Override
public void sayHelloAgain(HelloRequest req,
StreamObserver<ArrayHelloReply> responseObserver) {
String str = new String("Yo Dude");
final Logger logger = Logger.getLogger(yoRecvo.class.getName());
ArrayHelloReply arrayHelloReply =
ArrayHelloReply.newBuilder().build();
logger.info("number of elements " +
arrayHelloReply.getMessage2Count());
responseObserver.onNext(arrayHelloReply);
responseObserver.onCompleted();
}
}
}
/////////////////////// client //////////////////////////
import com.google.protobuf.Descriptors;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author yotimbo
*/
public class yoSendo {
private static final Logger logger =
Logger.getLogger(yoSendo.class.getName());
private final ManagedChannel channel; //gRPC class, only needed for
server
private final GreeterGrpc.GreeterBlockingStub blockingStub;
public yoSendo(String host, int port) {
channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext(true)
.build();
blockingStub = GreeterGrpc.newBlockingStub(channel); //Greeter is
from proto service, Grpc must be appended
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
/** Say hello to server. */
public void greet(String name) {
List mylist;
logger.info("Will try to greet " + name + " ...");
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response;
ArrayHelloReply arrayResponse;
try {
response = blockingStub.sayHello(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + response.getMessage());
try {
arrayResponse = blockingStub.sayHelloAgain(request);
logger.info(arrayResponse.getAllFields().toString());
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + arrayResponse.getMessage2Count());
}
public static void main(String[] args) throws Exception{
yoSendo client = new yoSendo("localhost", 50051);
try {
/* Access a service running on the local machine on port 50051 */
String user = "world";
if (args.length > 0) {
user = args[0]; /* Use the arg as the name to greet if provided */
}
client.greet(user);
} finally {
client.shutdown();
}
}
}
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/2b745870-9ab4-4de5-8bdc-e1a3322cb238%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.