GreetingServiceImpl.java 1.53 KB
Newer Older
Michele Fiori's avatar
Michele Fiori committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
package grpchelloserver;

import com.example.grpc.GreetingServiceGrpc.GreetingServiceImplBase;
import com.example.grpc.GreetingServiceOuterClass.*;
import io.grpc.stub.StreamObserver;

public class GreetingServiceImpl extends GreetingServiceImplBase {

    @Override
    public void greeting(HelloRequest request, StreamObserver<HelloResponse> responseObserver){

        //The request is a HelloRequest (defined in the .proto file)
        System.out.println(request);

        //Build the response of type HelloResponse (defined in the .proto file)
        HelloResponse response = HelloResponse.newBuilder().setGreeting("Hello there, "+request.getName()).build();

        //Pass the response to the stream
        responseObserver.onNext(response);

        //Finish the communication
        responseObserver.onCompleted();

    }

    @Override
    public void streamGreeting(HelloRequest request, StreamObserver<HelloResponse> responseObserver){

        System.out.println("Metodo stream chiamato!");

        //The request is a HelloRequest (defined in the .proto file)
        System.out.println(request);

        //Build the response of type HelloResponse (defined in the .proto file)
        HelloResponse response = HelloResponse.newBuilder().setGreeting("Hello there, "+request.getName()).build();

        //Pass the response to the stream
        responseObserver.onNext(response);
        responseObserver.onNext(response);
        responseObserver.onNext(response);

        //Finish the communication
        responseObserver.onCompleted();

    }

}