Search in sources :

Example 71 with ManagedChannel

use of io.grpc.ManagedChannel in project grpc-java by grpc.

the class HelloWorldClientTls method main.

/**
 * Greet server. If provided, the first element of {@code args} is the name to use in the
 * greeting.
 */
public static void main(String[] args) throws Exception {
    if (args.length < 2 || args.length == 4 || args.length > 5) {
        System.out.println("USAGE: HelloWorldClientTls host port [trustCertCollectionFilePath " + "[clientCertChainFilePath clientPrivateKeyFilePath]]\n  Note: clientCertChainFilePath and " + "clientPrivateKeyFilePath are only needed if mutual auth is desired.");
        System.exit(0);
    }
    // If only defaults are necessary, you can use TlsChannelCredentials.create() instead of
    // interacting with the Builder.
    TlsChannelCredentials.Builder tlsBuilder = TlsChannelCredentials.newBuilder();
    switch(args.length) {
        case 5:
            tlsBuilder.keyManager(new File(args[3]), new File(args[4]));
        // fallthrough
        case 3:
            tlsBuilder.trustManager(new File(args[2]));
        // fallthrough
        default:
    }
    String host = args[0];
    int port = Integer.parseInt(args[1]);
    ManagedChannel channel = Grpc.newChannelBuilderForAddress(host, port, tlsBuilder.build()).overrideAuthority("foo.test.google.fr").build();
    try {
        HelloWorldClientTls client = new HelloWorldClientTls(channel);
        client.greet(host);
    } finally {
        channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
    }
}
Also used : TlsChannelCredentials(io.grpc.TlsChannelCredentials) ManagedChannel(io.grpc.ManagedChannel) File(java.io.File)

Example 72 with ManagedChannel

use of io.grpc.ManagedChannel in project grpc-java by grpc.

the class HelloWorldAltsClient method run.

private void run(String[] args) throws InterruptedException {
    parseArgs(args);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    ManagedChannel channel = AltsChannelBuilder.forTarget(serverAddress).executor(executor).build();
    try {
        GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
        HelloReply resp = stub.sayHello(HelloRequest.newBuilder().setName("Waldo").build());
        logger.log(Level.INFO, "Got {0}", resp);
    } finally {
        channel.shutdown();
        channel.awaitTermination(1, TimeUnit.SECONDS);
        // Wait until the channel has terminated, since tasks can be queued after the channel is
        // shutdown.
        executor.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ManagedChannel(io.grpc.ManagedChannel) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 73 with ManagedChannel

use of io.grpc.ManagedChannel in project grpc-java by grpc.

the class RouteGuideClient method main.

/**
 * Issues several different requests and then exits.
 */
public static void main(String[] args) throws InterruptedException {
    String target = "localhost:8980";
    if (args.length > 0) {
        if ("--help".equals(args[0])) {
            System.err.println("Usage: [target]");
            System.err.println("");
            System.err.println("  target  The server to connect to. Defaults to " + target);
            System.exit(1);
        }
        target = args[0];
    }
    List<Feature> features;
    try {
        features = RouteGuideUtil.parseFeatures(RouteGuideUtil.getDefaultFeaturesFile());
    } catch (IOException ex) {
        ex.printStackTrace();
        return;
    }
    ManagedChannel channel = ManagedChannelBuilder.forTarget(target).usePlaintext().build();
    try {
        RouteGuideClient client = new RouteGuideClient(channel);
        // Looking for a valid feature
        client.getFeature(409146138, -746188906);
        // Feature missing.
        client.getFeature(0, 0);
        // Looking for features between 40, -75 and 42, -73.
        client.listFeatures(400000000, -750000000, 420000000, -730000000);
        // Record a few randomly selected points from the features file.
        client.recordRoute(features, 10);
        // Send and receive some notes.
        CountDownLatch finishLatch = client.routeChat();
        if (!finishLatch.await(1, TimeUnit.MINUTES)) {
            client.warning("routeChat can not finish within 1 minutes");
        }
    } finally {
        channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
    }
}
Also used : ManagedChannel(io.grpc.ManagedChannel) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 74 with ManagedChannel

use of io.grpc.ManagedChannel in project grpc-java by grpc.

the class NettyChannelBuilderTest method authorityIsReadable.

@Test
public void authorityIsReadable() throws Exception {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress("original", 1234);
    ManagedChannel b = builder.build();
    try {
        assertEquals("original:1234", b.authority());
    } finally {
        shutdown(b);
    }
}
Also used : ManagedChannel(io.grpc.ManagedChannel) TrackingObjectPoolForTest(io.grpc.netty.NettyTestUtil.TrackingObjectPoolForTest) Test(org.junit.Test)

Example 75 with ManagedChannel

use of io.grpc.ManagedChannel in project grpc-java by grpc.

the class NettyChannelBuilderTest method overrideAuthorityIsReadableHelper.

private void overrideAuthorityIsReadableHelper(NettyChannelBuilder builder, String overrideAuthority) throws Exception {
    builder.overrideAuthority(overrideAuthority);
    ManagedChannel channel = builder.build();
    try {
        assertEquals(overrideAuthority, channel.authority());
    } finally {
        shutdown(channel);
    }
}
Also used : ManagedChannel(io.grpc.ManagedChannel)

Aggregations

ManagedChannel (io.grpc.ManagedChannel)164 Test (org.junit.Test)92 CountDownLatch (java.util.concurrent.CountDownLatch)26 ManagedChannel (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel)26 ArrayList (java.util.ArrayList)20 Metadata (io.grpc.Metadata)18 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)15 ExecutorService (java.util.concurrent.ExecutorService)13 ByteString (com.google.protobuf.ByteString)12 Status (io.grpc.Status)12 StreamObserver (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver)11 StreamObserver (io.grpc.stub.StreamObserver)10 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)10 CallOptions (io.grpc.CallOptions)9 Subchannel (io.grpc.LoadBalancer.Subchannel)9 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)9 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)9 Endpoints (org.apache.beam.model.pipeline.v1.Endpoints)9