Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gRPC protobuffers Java cannot set CallOptions for BlockingStub

I have a simple proto file, to create my java classes

syntax = "proto3";

option java_package = "some.project.grpc";
option java_multiple_files = true;

message PingRequest { }
message PingResponse { }

service MyServer {
    rpc Ping(PingRequest) returns (PingResponse);
}

With gradle and the google protobuf plugin (https://github.com/google/protobuf-gradle-plugin) I create my classes with

gradle generateProto

The generated MyServerGrpc has an internal class MyServerBlockingStub that has two constructors:

private MyServerBlockingStub(io.grpc.Channel channel) { ... }
private MyServerBlockingStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { ... }

The MyServerGrpc class does expose the first one as newBlockingStub(io.grpc.Channel channel) but not the second - which I would need.

Like this, I cannot set any call options, such as timeout. I could easily manipulate the generated file to allow me access to the constructor I need, but those changes would be lost on the next generation - so it's not really an option.

Since it seems such an easy fix I was thinking:

  • Am I missing some option that would expose this constructor to me?
  • Is the intention to set the CallOptions object somewhere else that I have not found yet?
like image 977
Cabadath Avatar asked Dec 01 '25 03:12

Cabadath


1 Answers

MyServerGrpc extends AbstractStub. AbstractStub has with* methods mirroring those on CallOptions.

So you want to do something like:

newBlockingStub(yourChannel).withDeadlineAfter(1, TimeUnit.MINUTES)
like image 67
Eric Anderson Avatar answered Dec 02 '25 15:12

Eric Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!