Building my first gRPC + Java server and client

1,861 views
Skip to first unread message

Mikko Kokkonen

unread,
Jan 9, 2017, 8:58:53 AM1/9/17
to grpc.io
Hi,

I didn't get much from the examples of gRPC so I'm trying to build my own Java server and client. So far I have managed to build my own proto file and use protoc to create Java class from it. My proto file is like this:

syntax = "proto3";

option java_package
= "testing";
option java_outer_classname
= "TTTService";

package testing;

// The TTT service definition.
service
Game {
 
// Sends status of game
    rpc
SayStatus (Placexor) returns (GameStatus) {}
}
message
Placexor {
    int32 row
= 1; // row
    int32 column
= 2; // column
    int32 gameid
= 3; // games id
}
message
GameStatus {
   
string iswin = 1;
   
string isturn = 2;
    int32 gameid
= 3;
    int32 success
= 4;
}



It is a server for TicTacToe game so I'm trying to have client to send the row, column, and gamenumbers and server will answer whether it has been succesfully done and if it is players turn etc. I'm currently stuck and cannot figure out how to proceed. I tried to copy code from HelloWorldServer and alter it to get better understanding. My server code looks currently like this:

public class Server {
   
private int port = 1234;
   
private io.grpc.Server server;
   
/**
     * @param args
     * @throws IOException
     * @throws InterruptedException
     */

   
public static void main(String[] args) throws IOException, InterruptedException {
       
final Server server = new Server();
        server
.start();

   
}
   
private void start() throws IOException {
       
//server = ServerBuilder.forPort(port).addService(new Listener()).build().start();
        server
= ServerBuilder.forPort(port).addService(new TTTService()).build().start();
       
System.err.println("Server started and listening in port: "+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");
               
Server.this.stop();
               
System.err.println("*** server shut down");
           
}
       
});
   
}

Which results in null pointer. Have I totally misunderstood everything and how should I continue from this?

Carl Mastrangelo

unread,
Jan 9, 2017, 1:08:53 PM1/9/17
to grpc.io
Calling start is asynchronous. Once you call start, it will immediately return.  After that it will finish up the rest of your start() method, and then return to main, then exit immediately.  You need to make the main wait for the server to shutdown, perhaps by calling awaitTermination() on it.


If you got a NullPointerException, can you add it to this thread?

Mikko Kokkonen

unread,
Jan 9, 2017, 1:25:33 PM1/9/17
to grpc.io
Exception in thread "main" java.lang.NullPointerException
    at io.grpc.internal.InternalHandlerRegistry$Builder.addService(InternalHandlerRegistry.java:60)
    at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:97)
    at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:103)
    at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:57)
    at testing.Server.start(Server.java:31)
    at testing.Server.main(Server.java:26)


As temporary solution I can create infinite while loop to run start forever. Is my proto file created correctly?

Carl Mastrangelo

unread,
Jan 9, 2017, 6:06:23 PM1/9/17
to grpc.io
Can you show the code for TTTService ?  I.e. where you implement the server side of the method?

Mikko Kokkonen

unread,
Jan 9, 2017, 11:23:03 PM1/9/17
to grpc.io


tiistai 10. tammikuuta 2017 1.06.23 UTC+2 Carl Mastrangelo kirjoitti:
Can you show the code for TTTService ?  I.e. where you implement the server side of the method?



The TTTService is what I got from protoc. Otherwise that is all I currently have on the Server implementation. I haven't got it running so no futher implementations yet.

Carl Mastrangelo

unread,
Jan 10, 2017, 4:41:53 PM1/10/17
to grpc.io
TTTService is just a dummy wrapper file, it doesn't contain any real code.  You need to actually implement the server part of your application.  You do that by extending the stubs in the generated code. 

Mikko Kokkonen

unread,
Jan 11, 2017, 12:07:12 AM1/11/17
to grpc.io


tiistai 10. tammikuuta 2017 23.41.53 UTC+2 Carl Mastrangelo kirjoitti:
TTTService is just a dummy wrapper file, it doesn't contain any real code.  You need to actually implement the server part of your application.  You do that by extending the stubs in the generated code. 



That is what I'm trying to ask in here. How can I use that wrapper file? I'm just currently stuck in even starting the service. 

Jorg Heymans

unread,
Jan 11, 2017, 1:03:04 PM1/11/17
to grpc.io
I found this presentation by Ray Tsang very good to get to grips with grpc basics and Spring : https://www.youtube.com/watch?v=xpmFhTMqWhc 

HTH
Jorg

Carl Mastrangelo

unread,
Jan 13, 2017, 4:46:33 PM1/13/17
to grpc.io
The abstract base classes in TTTServices function as a template for your service.  You have to actually implement the methods in TTTService (which, I think should be TTTServiceGrpc.TTTServiceImplBase).  The generated code is only there to help you avoid having to write the boilerplate code.  If you pass in an empty template, the server is going to fail to start because it won't know what to do with incoming requests.  There are many examples* in the grpc java repo that show you what you need to do.

Reply all
Reply to author
Forward
0 new messages