GreetingServiceClient.java 3.28 KB
Newer Older
Gabriele Civitarese's avatar
Gabriele Civitarese committed
1 2 3
package grpchelloserver;

import com.example.grpc.GreetingServiceGrpc;
Gabriele Civitarese's avatar
Gabriele Civitarese committed
4
import com.example.grpc.GreetingServiceGrpc.*;
Gabriele Civitarese's avatar
Gabriele Civitarese committed
5
import com.example.grpc.GreetingServiceOuterClass.*;
Gabriele Civitarese's avatar
Gabriele Civitarese committed
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
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.util.concurrent.TimeUnit;

public class GreetingServiceClient {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Trying to call greeting synchronous method:\n");

        synchronousCall();

        System.out.println("\n...Done!");

        System.out.println("--------------");

        System.out.println("Now calling streamGreeting asynchronous method:\n");

        asynchronousStreamCall();

        System.out.println("\n...Done!");


    }

    //calling a synchronous rpc operation
    public static void synchronousCall(){

        //plaintext channel on the address (ip/port) which offers the GreetingService service
        final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8080").usePlaintext(true).build();

        //creating a blocking stub on the channel
Gabriele Civitarese's avatar
Gabriele Civitarese committed
40
        GreetingServiceBlockingStub stub = GreetingServiceGrpc.newBlockingStub(channel);
Gabriele Civitarese's avatar
Gabriele Civitarese committed
41 42

        //creating the HelloResponse object which will be provided as input to the RPC method
Gabriele Civitarese's avatar
Gabriele Civitarese committed
43
        HelloRequest request = HelloRequest.newBuilder().setName("Pippo").build();
Gabriele Civitarese's avatar
Gabriele Civitarese committed
44 45

        //calling the method. it returns an instance of HelloResponse
Gabriele Civitarese's avatar
Gabriele Civitarese committed
46
        HelloResponse response = stub.greeting(request);
Gabriele Civitarese's avatar
Gabriele Civitarese committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

        //printing the answer
        System.out.println(response.getGreeting());

        //closing the channel
        channel.shutdown();

    }

    //calling an asynchronous method based on stream
    public static void asynchronousStreamCall() throws InterruptedException {

        //plaintext channel on the address (ip/port) which offers the GreetingService service
        final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8080").usePlaintext(true).build();

        //creating an asynchronous stub on the channel
Gabriele Civitarese's avatar
Gabriele Civitarese committed
63
        GreetingServiceStub stub = GreetingServiceGrpc.newStub(channel);
Gabriele Civitarese's avatar
Gabriele Civitarese committed
64 65

        //creating the HelloResponse object which will be provided as input to the RPC method
Gabriele Civitarese's avatar
Gabriele Civitarese committed
66
        HelloRequest request = HelloRequest.newBuilder().setName("Pippo").build();
Gabriele Civitarese's avatar
Gabriele Civitarese committed
67 68

        //calling the RPC method. since it is asynchronous, we need to define handlers
Gabriele Civitarese's avatar
Gabriele Civitarese committed
69
        stub.streamGreeting(request, new StreamObserver<HelloResponse>() {
Gabriele Civitarese's avatar
Gabriele Civitarese committed
70 71

            //this hanlder takes care of each item received in the stream
Gabriele Civitarese's avatar
Gabriele Civitarese committed
72
            public void onNext(HelloResponse helloResponse) {
Gabriele Civitarese's avatar
Gabriele Civitarese committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

                //each item is just printed
                System.out.println(helloResponse.getGreeting());

            }

            //if there are some errors, this method will be called
            public void onError(Throwable throwable) {

                System.out.println("Error! "+throwable.getMessage());

            }

            //when the stream is completed (the server called "onCompleted") just close the channel
            public void onCompleted() {

                channel.shutdownNow();

            }

        });

        //you need this. otherwise the method will terminate before that answers from the server are received
        channel.awaitTermination(10, TimeUnit.SECONDS);


    }

}