本文整理汇总了Java中io.grpc.CallOptions类的典型用法代码示例。如果您正苦于以下问题:Java CallOptions类的具体用法?Java CallOptions怎么用?Java CallOptions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CallOptions类属于io.grpc包,在下文中一共展示了CallOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: metadataInterceptor
import io.grpc.CallOptions; //导入依赖的package包/类
private ClientInterceptor metadataInterceptor() {
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final io.grpc.MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
throws StatusException {
for (ConfigProto.CallMetadataEntry entry : callConfiguration.getMetadataList()) {
Metadata.Key<String> key = Metadata.Key.of(entry.getName(), Metadata.ASCII_STRING_MARSHALLER);
headers.put(key, entry.getValue());
}
delegate().start(responseListener, headers);
}
};
}
};
return interceptor;
}
示例2: interceptCall
import io.grpc.CallOptions; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions,
final Channel next) {
return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
next.newCall(method, callOptions)) {
@Override
protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
throws StatusException {
Metadata cachedSaved;
URI uri = serviceUri(next, method);
synchronized (this) {
Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
if (mLastMetadata == null || mLastMetadata != latestMetadata) {
mLastMetadata = latestMetadata;
mCached = toHeaders(mLastMetadata);
}
cachedSaved = mCached;
}
headers.merge(cachedSaved);
delegate().start(responseListener, headers);
}
};
}
示例3: noOverwriteWorksCustomKeys
import io.grpc.CallOptions; //导入依赖的package包/类
@Test
public void noOverwriteWorksCustomKeys() {
CallOptions.Key<String> k1 = CallOptions.Key.of("k1", null);
CallOptions.Key<String> k2 = CallOptions.Key.of("k2", null);
CallOptions.Key<String> k3 = CallOptions.Key.of("k3", null);
CallOptions baseOptions = CallOptions.DEFAULT.withOption(k1, "FOO").withOption(k3, "BAZ");
CallOptions defaultOptions = CallOptions.DEFAULT.withOption(k2, "BAR").withOption(k3, "BOP");
DefaultCallOptionsClientInterceptor interceptor = new DefaultCallOptionsClientInterceptor(defaultOptions);
CallOptions patchedOptions = interceptor.patchOptions(baseOptions);
assertThat(patchedOptions.getOption(k1)).isEqualTo("FOO");
assertThat(patchedOptions.getOption(k2)).isEqualTo("BAR");
assertThat(patchedOptions.getOption(k3)).isEqualTo("BAZ");
}
示例4: overwriteWorksCustomKeys
import io.grpc.CallOptions; //导入依赖的package包/类
@Test
public void overwriteWorksCustomKeys() {
CallOptions.Key<String> k1 = CallOptions.Key.of("k1", null);
CallOptions.Key<String> k2 = CallOptions.Key.of("k2", null);
CallOptions.Key<String> k3 = CallOptions.Key.of("k3", null);
CallOptions baseOptions = CallOptions.DEFAULT.withOption(k1, "FOO").withOption(k3, "BAZ");
CallOptions defaultOptions = CallOptions.DEFAULT.withOption(k2, "BAR").withOption(k3, "BOP");
DefaultCallOptionsClientInterceptor interceptor = new DefaultCallOptionsClientInterceptor(defaultOptions)
.overwriteExistingValues();
CallOptions patchedOptions = interceptor.patchOptions(baseOptions);
assertThat(patchedOptions.getOption(k1)).isEqualTo("FOO");
assertThat(patchedOptions.getOption(k2)).isEqualTo("BAR");
assertThat(patchedOptions.getOption(k3)).isEqualTo("BOP");
}
示例5: interceptCall
import io.grpc.CallOptions; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, final Channel next) {
return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
next.newCall(method, callOptions)) {
@Override
protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
throws StatusException {
Metadata cachedSaved;
URI uri = serviceUri(next, method);
synchronized (GoogleCredentialsInterceptor.this) {
Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
if (mLastMetadata == null || mLastMetadata != latestMetadata) {
mLastMetadata = latestMetadata;
mCached = toHeaders(mLastMetadata);
}
cachedSaved = mCached;
}
headers.merge(cachedSaved);
delegate().start(responseListener, headers);
}
};
}
示例6: interceptCall
import io.grpc.CallOptions; //导入依赖的package包/类
@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) {
getToken(next).ifPresent(t -> headers.put(TOKEN, t));
super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onClose(Status status, Metadata trailers) {
if (isInvalidTokenError(status)) {
try {
refreshToken(next);
} catch (Exception e) {
// don't throw any error here.
// rpc will retry on expired auth token.
}
}
super.onClose(status, trailers);
}
}, headers);
}
};
}
示例7: newCall
import io.grpc.CallOptions; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT>
newCall(MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions) {
final Context timerContext = timer.time();
final AtomicBoolean decremented = new AtomicBoolean(false);
return new CheckedForwardingClientCall<ReqT, RespT>(delegate.newCall(methodDescriptor, callOptions)) {
@Override
protected void checkedStart(ClientCall.Listener<RespT> responseListener, Metadata headers)
throws Exception {
ClientCall.Listener<RespT> timingListener = wrap(responseListener, timerContext, decremented);
getStats().ACTIVE_RPC_COUNTER.inc();
getStats().RPC_METER.mark();
delegate().start(timingListener, headers);
}
@Override
public void cancel(String message, Throwable cause) {
if (!decremented.getAndSet(true)) {
getStats().ACTIVE_RPC_COUNTER.dec();
}
super.cancel(message, cause);
}
};
}
示例8: clientStreamingCall
import io.grpc.CallOptions; //导入依赖的package包/类
/**
* Executes a client-streaming call returning a {@link StreamObserver} for the requestMore messages.
*
* @return requestMore stream observer.
*/
public static <ReqT, RespT> Single<RespT> clientStreamingCall(
ClientCall<ReqT, RespT> call,
Flowable<ReqT> requests,
CallOptions options) {
final StreamRequestSender<ReqT> requestSender = new StreamRequestSender<ReqT>(call, getLowWatermark(options), getHighWatermark(options));
SingleResponseReceiver<RespT> responseReceiver = new SingleResponseReceiver<RespT>(call) {
@Override
public void startCall() {
requestSender.startCall();
super.startCall();
}
};
ClientCall.Listener<RespT> delegate = new DelegateClientCallListener<RespT>(requestSender, responseReceiver);
call.start(delegate, new Metadata());
requests.subscribe(requestSender.subscriber());
return Single.wrap(responseReceiver.singleSource());
}
示例9: bidiStreamingCall
import io.grpc.CallOptions; //导入依赖的package包/类
/**
* Executes a bidi-streaming call.
*
* @return requestMore stream observer.
*/
public static <ReqT, RespT> Flowable<RespT> bidiStreamingCall(
ClientCall<ReqT, RespT> call,
Flowable<ReqT> requests,
CallOptions options) {
final StreamRequestSender<ReqT> requestSender = new StreamRequestSender<ReqT>(call, getLowWatermark(options), getHighWatermark(options));
StreamingResponseReceiver<RespT> responseReceiver = new StreamingResponseReceiver<RespT>(call) {
@Override
public void startCall() {
requestSender.startCall();
super.startCall();
}
};
ClientCall.Listener<RespT> delegate = new DelegateClientCallListener<RespT>(requestSender, responseReceiver);
call.start(delegate, new Metadata());
requests.subscribe(requestSender.subscriber());
return Flowable.fromPublisher(responseReceiver.publisher());
}
示例10: testFlowControl
import io.grpc.CallOptions; //导入依赖的package包/类
@Test
public void testFlowControl() {
CallOptions callOptions = CallOptions.DEFAULT
.withOption(GrpcRxOptions.LOW_WATERMARK, 2)
.withOption(GrpcRxOptions.HIGH_WATERMARK, 6);
EchoGrpcRx.EchoStub stub = EchoGrpcRx.newStub(channel, callOptions);
Flowable<EchoReq> requests = Flowable.range(0, 64)
.map(i -> {
logger.info("Emitting request {}", i);
return EchoReq.newBuilder()
.setId(i)
.setValue(bigStr)
.build();
});
TestSubscriber<EchoResp> testSubscriber = new AutoTestSubscriber<>(4);
stub.bidiStreaming(requests)
.subscribe(testSubscriber);
testSubscriber.awaitDone(1, TimeUnit.MINUTES);
testSubscriber.assertComplete();
}
示例11: interceptCall
import io.grpc.CallOptions; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT,RespT> method, CallOptions callOptions, Channel next) {
LOGGER.info("Intercepted " + method.getFullMethodName());
ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
call = new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(call) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
if (apiKey != null && !apiKey.isEmpty()) {
LOGGER.info("Attaching API Key: " + apiKey);
headers.put(API_KEY_HEADER, apiKey);
}
super.start(responseListener, headers);
}
};
return call;
}
示例12: interceptCall
import io.grpc.CallOptions; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT,RespT> method, CallOptions callOptions, Channel next) {
LOGGER.info("Intercepted " + method.getFullMethodName());
ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
call = new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(call) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
if (apiKey != null && !apiKey.isEmpty()) {
LOGGER.info("Attaching API Key: " + apiKey);
headers.put(API_KEY_HEADER, apiKey);
}
if (authToken != null && !authToken.isEmpty()) {
System.out.println("Attaching auth token");
headers.put(AUTHORIZATION_HEADER, "Bearer " + authToken);
}
super.start(responseListener, headers);
}
};
return call;
}
示例13: delayedTransportHoldsOffIdleness
import io.grpc.CallOptions; //导入依赖的package包/类
@Test
public void delayedTransportHoldsOffIdleness() throws Exception {
ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
call.start(mockCallListener, new Metadata());
assertTrue(channel.inUseStateAggregator.isInUse());
// As long as the delayed transport is in-use (by the pending RPC), the channel won't go idle.
timer.forwardTime(IDLE_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
assertTrue(channel.inUseStateAggregator.isInUse());
// Cancelling the only RPC will reset the in-use state.
assertEquals(0, executor.numPendingTasks());
call.cancel("In test", null);
assertEquals(1, executor.runDueTasks());
assertFalse(channel.inUseStateAggregator.isInUse());
// And allow the channel to go idle.
timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
verify(mockLoadBalancer, never()).shutdown();
timer.forwardTime(1, TimeUnit.SECONDS);
verify(mockLoadBalancer).shutdown();
}
示例14: requestWithNoCacheOptionSkipsCache
import io.grpc.CallOptions; //导入依赖的package包/类
@Test
public void requestWithNoCacheOptionSkipsCache() {
HelloReply reply1 =
ClientCalls.blockingUnaryCall(
channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
HelloReply reply2 =
ClientCalls.blockingUnaryCall(
channelToUse,
safeGreeterSayHelloMethod,
CallOptions.DEFAULT.withOption(SafeMethodCachingInterceptor.NO_CACHE_CALL_OPTION, true),
message);
HelloReply reply3 =
ClientCalls.blockingUnaryCall(
channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
assertNotEquals(reply1, reply2);
assertSame(reply1, reply3);
}
示例15: requestWithOnlyIfCachedOption_unavailableIfNotInCache
import io.grpc.CallOptions; //导入依赖的package包/类
@Test
public void requestWithOnlyIfCachedOption_unavailableIfNotInCache() {
try {
ClientCalls.blockingUnaryCall(
channelToUse,
safeGreeterSayHelloMethod,
CallOptions.DEFAULT.withOption(
SafeMethodCachingInterceptor.ONLY_IF_CACHED_CALL_OPTION, true),
message);
fail("Expected call to fail");
} catch (StatusRuntimeException sre) {
assertEquals(Status.UNAVAILABLE.getCode(), sre.getStatus().getCode());
assertEquals(
"Unsatisfiable Request (only-if-cached set, but value not in cache)",
sre.getStatus().getDescription());
}
}