Spring / Spring gRPC Interview Questions
How do you implement a server-streaming RPC in a Spring @GrpcService?
The generated method signature takes the request plus a StreamObserver<ResponseType> instead of returning a value directly. Inside the method, you call onNext(item) once per item you want to emit, and finish with onCompleted() - or onError(...) if something goes wrong partway through.
@Override public void listOrders(OrderQuery req, StreamObserver<Order> obs) { orderService.findAll(req.getCustomerId()) .forEach(obs::onNext); obs.onCompleted(); }
Spring's usual bean lifecycle and DI are unaffected - the streaming behavior is purely a matter of standard grpc-java StreamObserver semantics inside a class that also happens to be a Spring-managed bean.
More Related questions...