diff --git a/examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldClient.java b/examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldClient.java index 230cb57731..2afd6de6b7 100644 --- a/examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldClient.java +++ b/examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldClient.java @@ -31,13 +31,8 @@ package io.grpc.examples.experimental; -import io.grpc.CallOptions; -import io.grpc.Channel; -import io.grpc.ClientCall; -import io.grpc.ClientInterceptor; import io.grpc.Codec; import io.grpc.ManagedChannel; -import io.grpc.MethodDescriptor; import io.grpc.examples.helloworld.GreeterGrpc; import io.grpc.examples.helloworld.HelloRequest; import io.grpc.examples.helloworld.HelloResponse; @@ -66,16 +61,8 @@ public class CompressingHelloWorldClient { public CompressingHelloWorldClient(String host, int port) { channel = NettyChannelBuilder.forAddress(host, port).negotiationType(NegotiationType.PLAINTEXT) - .intercept(new ClientInterceptor() { - @Override - public ClientCall interceptCall( - MethodDescriptor method, CallOptions callOptions, - Channel next) { - return next.newCall(method, callOptions.withCompressor(new Codec.Gzip())); - } - }) .build(); - blockingStub = GreeterGrpc.newBlockingStub(channel); + blockingStub = GreeterGrpc.newBlockingStub(channel).withCompressor(new Codec.Gzip()); } public void shutdown() throws InterruptedException { diff --git a/stub/src/main/java/io/grpc/stub/AbstractStub.java b/stub/src/main/java/io/grpc/stub/AbstractStub.java index 31a16f4515..f6626eb532 100644 --- a/stub/src/main/java/io/grpc/stub/AbstractStub.java +++ b/stub/src/main/java/io/grpc/stub/AbstractStub.java @@ -35,6 +35,8 @@ import io.grpc.CallOptions; import io.grpc.Channel; import io.grpc.ClientInterceptor; import io.grpc.ClientInterceptors; +import io.grpc.Compressor; +import io.grpc.ExperimentalApi; import java.util.concurrent.TimeUnit; @@ -123,6 +125,14 @@ public abstract class AbstractStub> { return build(newChannel, callOptions); } + /** + * Returns a new stub that uses the given compressor. + */ + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/492") + public final S withCompressor(Compressor c) { + return build(channel, callOptions.withCompressor(c)); + } + /** * Returns a new stub that has the given interceptors attached to the underlying channel. */