Search in sources :

Example 61 with Channel

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

the class HeaderServerInterceptorTest method serverHeaderDeliveredToClient.

@Test
public void serverHeaderDeliveredToClient() {
    class SpyingClientInterceptor implements ClientInterceptor {

        ClientCall.Listener<?> spyListener;

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

                @Override
                public void start(Listener<RespT> responseListener, Metadata headers) {
                    spyListener = responseListener = mock(ClientCall.Listener.class, delegatesTo(responseListener));
                    super.start(responseListener, headers);
                }
            };
        }
    }
    SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
    GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel).withInterceptors(clientInterceptor);
    ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
    blockingStub.sayHello(HelloRequest.getDefaultInstance());
    assertNotNull(clientInterceptor.spyListener);
    verify(clientInterceptor.spyListener).onHeaders(metadataCaptor.capture());
    assertEquals("customRespondValue", metadataCaptor.getValue().get(HeaderServerInterceptor.CUSTOM_HEADER_KEY));
}
Also used : GreeterBlockingStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientCall(io.grpc.ClientCall) ClientInterceptor(io.grpc.ClientInterceptor) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) Test(org.junit.Test)

Example 62 with Channel

use of io.grpc.Channel in project jetcd by coreos.

the class ClientConnectionManager method defaultChannelBuilder.

@SuppressWarnings("rawtypes")
ManagedChannelBuilder<?> defaultChannelBuilder(String target) {
    if (target == null) {
        throw new IllegalArgumentException("At least one endpoint should be provided");
    }
    final Vertx vertx = Vertx.vertx();
    final VertxChannelBuilder channelBuilder = VertxChannelBuilder.forTarget(vertx, target);
    if (builder.authority() != null) {
        channelBuilder.overrideAuthority(builder.authority());
    }
    if (builder.maxInboundMessageSize() != null) {
        channelBuilder.maxInboundMessageSize(builder.maxInboundMessageSize());
    }
    if (builder.sslContext() != null) {
        channelBuilder.nettyBuilder().negotiationType(NegotiationType.TLS);
        channelBuilder.nettyBuilder().sslContext(builder.sslContext());
    } else {
        channelBuilder.nettyBuilder().negotiationType(NegotiationType.PLAINTEXT);
    }
    if (builder.keepaliveTime() != null) {
        channelBuilder.keepAliveTime(builder.keepaliveTime().toMillis(), TimeUnit.MILLISECONDS);
    }
    if (builder.keepaliveTimeout() != null) {
        channelBuilder.keepAliveTimeout(builder.keepaliveTimeout().toMillis(), TimeUnit.MILLISECONDS);
    }
    if (builder.keepaliveWithoutCalls() != null) {
        channelBuilder.keepAliveWithoutCalls(builder.keepaliveWithoutCalls());
    }
    if (builder.connectTimeout() != null) {
        channelBuilder.nettyBuilder().withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) builder.connectTimeout().toMillis());
    }
    if (builder.loadBalancerPolicy() != null) {
        channelBuilder.defaultLoadBalancingPolicy(builder.loadBalancerPolicy());
    } else {
        channelBuilder.defaultLoadBalancingPolicy("pick_first");
    }
    if (builder.headers() != null) {
        channelBuilder.intercept(new ClientInterceptor() {

            @Override
            public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
                return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

                    @Override
                    public void start(Listener<RespT> responseListener, Metadata headers) {
                        builder.headers().forEach((BiConsumer<Metadata.Key, Object>) headers::put);
                        super.start(responseListener, headers);
                    }
                };
            }
        });
    }
    if (builder.interceptors() != null) {
        channelBuilder.intercept(builder.interceptors());
    }
    return channelBuilder;
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Vertx(io.vertx.core.Vertx) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientCall(io.grpc.ClientCall) ClientInterceptor(io.grpc.ClientInterceptor) VertxChannelBuilder(io.vertx.grpc.VertxChannelBuilder) BiConsumer(java.util.function.BiConsumer)

Aggregations

Channel (io.grpc.Channel)60 CallOptions (io.grpc.CallOptions)37 Test (org.junit.Test)33 Metadata (io.grpc.Metadata)25 MethodDescriptor (io.grpc.MethodDescriptor)20 ClientInterceptor (io.grpc.ClientInterceptor)19 ManagedChannel (io.grpc.ManagedChannel)17 ClientCall (io.grpc.ClientCall)14 SocketAddress (java.net.SocketAddress)8 ByteString (com.google.protobuf.ByteString)7 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)7 NoopClientCall (io.grpc.internal.NoopClientCall)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Duration (com.google.protobuf.Duration)5 ChannelLogger (io.grpc.ChannelLogger)5 Status (io.grpc.Status)5 ClientStreamTracer (io.grpc.ClientStreamTracer)4 ForwardingClientCall (io.grpc.ForwardingClientCall)4 SimpleForwardingClientCallListener (io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener)4 Subchannel (io.grpc.LoadBalancer.Subchannel)4