Use stub call options for compression

This commit is contained in:
Carl Mastrangelo 2015-09-09 11:11:19 -07:00
parent f80ca40fb9
commit 399be9ac73
2 changed files with 11 additions and 14 deletions

View File

@ -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 <RequestT, ResponseT> ClientCall<RequestT, ResponseT> interceptCall(
MethodDescriptor<RequestT, ResponseT> 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 {

View File

@ -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<S extends 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.
*/