Java Code Examples for io.grpc.CallOptions#withOption()

The following examples show how to use io.grpc.CallOptions#withOption() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CronetCallOptions.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a copy of {@code callOptions} with {@code annotation} included as one of the Cronet
 * annotation objects. When an RPC is made using a {@link CallOptions} instance returned by this
 * method, the annotation objects will be attached to the underlying Cronet bidirectional stream.
 * When the stream finishes, the user can retrieve the annotation objects via {@link
 * org.chromium.net.RequestFinishedInfo.Listener}.
 *
 * @param annotation the object to attach to the Cronet stream
 */
public static CallOptions withAnnotation(CallOptions callOptions, Object annotation) {
  Collection<Object> existingAnnotations = callOptions.getOption(CRONET_ANNOTATIONS_KEY);
  ArrayList<Object> newAnnotations;
  if (existingAnnotations == null) {
    newAnnotations = new ArrayList<>();
  } else {
    newAnnotations = new ArrayList<>(existingAnnotations);
  }
  newAnnotations.add(annotation);
  return callOptions.withOption(
      CronetCallOptions.CRONET_ANNOTATIONS_KEY, Collections.unmodifiableList(newAnnotations));
}
 
Example 2
Source File: CronetClientStream.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a copy of {@code callOptions} with {@code annotation} included as one of the Cronet
 * annotation objects. When an RPC is made using a {@link CallOptions} instance returned by this
 * method, the annotation objects will be attached to the underlying Cronet bidirectional stream.
 * When the stream finishes, the user can retrieve the annotation objects via {@link
 * org.chromium.net.RequestFinishedInfo.Listener}.
 *
 * @param annotation the object to attach to the Cronet stream
 */
static CallOptions withAnnotation(CallOptions callOptions, Object annotation) {
  Collection<Object> existingAnnotations = callOptions.getOption(CRONET_ANNOTATIONS_KEY);
  ArrayList<Object> newAnnotations;
  if (existingAnnotations == null) {
    newAnnotations = new ArrayList<>();
  } else {
    newAnnotations = new ArrayList<>(existingAnnotations);
  }
  newAnnotations.add(annotation);
  return callOptions.withOption(
      CRONET_ANNOTATIONS_KEY, Collections.unmodifiableList(newAnnotations));
}
 
Example 3
Source File: ClientCacheExampleActivity.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
protected String doInBackground(Object... params) {
  String host = (String) params[0];
  String message = (String) params[1];
  String portStr = (String) params[2];
  boolean useGet = (boolean) params[3];
  boolean noCache = (boolean) params[4];
  boolean onlyIfCached = (boolean) params[5];
  int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
  try {
    channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
    Channel channelToUse =
        ClientInterceptors.intercept(
            channel, SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache));
    HelloRequest request = HelloRequest.newBuilder().setName(message).build();
    HelloReply reply;
    if (useGet) {
      MethodDescriptor<HelloRequest, HelloReply> safeCacheableUnaryCallMethod =
          GreeterGrpc.getSayHelloMethod().toBuilder().setSafe(true).build();
      CallOptions callOptions = CallOptions.DEFAULT;
      if (noCache) {
        callOptions =
            callOptions.withOption(SafeMethodCachingInterceptor.NO_CACHE_CALL_OPTION, true);
      }
      if (onlyIfCached) {
        callOptions =
            callOptions.withOption(
                SafeMethodCachingInterceptor.ONLY_IF_CACHED_CALL_OPTION, true);
      }
      reply =
          ClientCalls.blockingUnaryCall(
              channelToUse, safeCacheableUnaryCallMethod, callOptions, request);
    } else {
      GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channelToUse);
      reply = stub.sayHello(request);
    }
    return reply.getMessage();
  } catch (Exception e) {
    Log.e(TAG, "RPC failed", e);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    pw.flush();
    return String.format("Failed... : %n%s", sw);
  }
}
 
Example 4
Source File: MyGrpcConfigurator.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Override
public CallOptions configureCallOptions(CallOptions options, URL url) {
    return options.withOption(CallOptions.Key.create("key"), "value");
}
 
Example 5
Source File: ClientCacheExampleActivity.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
protected String doInBackground(Object... params) {
  String host = (String) params[0];
  String message = (String) params[1];
  String portStr = (String) params[2];
  boolean useGet = (boolean) params[3];
  boolean noCache = (boolean) params[4];
  boolean onlyIfCached = (boolean) params[5];
  int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
  try {
    channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
    Channel channelToUse =
        ClientInterceptors.intercept(
            channel, SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache));
    HelloRequest request = HelloRequest.newBuilder().setName(message).build();
    HelloReply reply;
    if (useGet) {
      MethodDescriptor<HelloRequest, HelloReply> safeCacheableUnaryCallMethod =
          GreeterGrpc.getSayHelloMethod().toBuilder().setSafe(true).build();
      CallOptions callOptions = CallOptions.DEFAULT;
      if (noCache) {
        callOptions =
            callOptions.withOption(SafeMethodCachingInterceptor.NO_CACHE_CALL_OPTION, true);
      }
      if (onlyIfCached) {
        callOptions =
            callOptions.withOption(
                SafeMethodCachingInterceptor.ONLY_IF_CACHED_CALL_OPTION, true);
      }
      reply =
          ClientCalls.blockingUnaryCall(
              channelToUse, safeCacheableUnaryCallMethod, callOptions, request);
    } else {
      GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channelToUse);
      reply = stub.sayHello(request);
    }
    return reply.getMessage();
  } catch (Exception e) {
    Log.e(TAG, "RPC failed", e);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    pw.flush();
    return String.format("Failed... : %n%s", sw);
  }
}
 
Example 6
Source File: InternalClientCalls.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/** Returns {@link CallOptions} with the corresponding {@link ClientCalls.StubType} set. */
public static CallOptions setStubType(CallOptions callOptions, StubType stubType) {
  return callOptions.withOption(ClientCalls.STUB_TYPE_OPTION, stubType.internalType);
}