I am trying to implement gRPC service in my code with condition to use one instance of the server class the code building successfully but i got a runtime error.
How can I fix this error?
Exception in thread "main" java.lang.NullPointerException: bindableService
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897)
at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:120)
at io.grpc.internal.AbstractServerImplBuilder.addService(AbstractServerImplBuilder.java:56)
at com.mypackage.cu.comms.CURequestHandlerService.<init>(Unknown Source)
at com.mypackage.cu.comms.CURequestHandlerService.start(Unknown Source)
at com.mypackage.cu.entities.ControlUnit.initSystem(Unknown Source)
at com.mypackage.cu.entities.ControlUnit.run(Unknown Source)
at com.mypackage.cu.entities.ControlUnit.main(Unknown Source)
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
\\ ..
public class CURequestHandlerService extends CURequestHandlerGrpc.CURequestHandlerImplBase {
private final Server server;
private static CURequestHandlerService INSTANCE = null;
private CURequestHandlerService(int port) {
this.server = ServerBuilder.forPort(port)
.addService(INSTANCE)
.build();
}
public static void start(int port) {
if (INSTANCE == null)
INSTANCE = new CURequestHandlerService(port);
try {
INSTANCE.server.start();
} catch (IOException e) {
System.out.println("Can't start GRPC server!");
}
}
CURequestHandlerService.start(16440);
I am using Ant since it is an old project and i maintaining it. I add all grpc dependencies jars to dependencies directory.
amqp-client-5.7.3.jar
failureaccess-1.0.1.jar
grpc-all-1.27.0.jar
grpc-api-1.27.0.jar
grpc-auth-1.27.0.jar
grpc-context-1.27.0.jar
grpc-core-1.27.0.jar
grpc-netty-1.27.0.jar
grpc-netty-shaded-1.27.0.jar
grpc-protobuf-1.27.0.jar
grpc-protobuf-lite-1.27.0.jar
grpc-stub-1.27.0.jar
grpc-testing-1.27.0.jar
guava-28.2-jre.jar
json-smart-1.2.jar
lettuce-4.3.0.Final-shaded.jar
libthrift-0.9.3.jar
opencensus-api-0.20.0.jar
opencensus-contrib-grpc-metrics-0.20.0.jar
perfmark-api-0.21.0.jar
protobuf-java-3.11.3.jar
protobuf-java-util-3.11.3.jar
protoc-gen-grpc-java-1.27.0-linux-x86_64.exe
slf4j-api-1.7.22.jar
slf4j-simple-1.7.22.jar
I found out the problem it is not a dependency issue after reading the server example in grpc-java repository. I underhanded that you must split the server class from the implementation class which extends form the Grpc generated class.
/**
* Grpc instance class that used to initiate the server and call implementation class
*/
public class GrpcServer {
private final Server server;
private static GrpcServer INSTANCE = null;
private GrpcServer(int port) {
this.server = ServerBuilder.forPort(port)
.addService(new CURequestHandlerService())
.build();
}
public static void start(int port) {
if (INSTANCE == null)
INSTANCE = new GrpcServer(port);
try {
INSTANCE.server.start();
System.out.println("GRPC server started");
} catch (IOException e) {
System.out.println("Can't start GRPC server!");
}
}
}
**
* Request Handler class contains the implementation for all functions initiated in proto files
* The implemented function
* */
public class CURequestHandlerService extends CURequestHandlerGrpc.CURequestHandlerImplBase{
public synchronized void Method1(Messages.Method1Input input,
StreamObserver<Messages.Method1Output> output) {
//...
output.onNext(outputResponse);
output.onCompleted();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.