本文整理汇总了Java中io.grpc.ClientCall.Listener方法的典型用法代码示例。如果您正苦于以下问题:Java ClientCall.Listener方法的具体用法?Java ClientCall.Listener怎么用?Java ClientCall.Listener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.grpc.ClientCall
的用法示例。
在下文中一共展示了ClientCall.Listener方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newCall
import io.grpc.ClientCall; //导入方法依赖的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);
}
};
}
示例2: clientStreamingCall
import io.grpc.ClientCall; //导入方法依赖的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());
}
示例3: bidiStreamingCall
import io.grpc.ClientCall; //导入方法依赖的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());
}
示例4: unaryBlockingCallSuccess
import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void unaryBlockingCallSuccess() throws Exception {
Integer req = 2;
final String resp = "bar";
final Status status = Status.OK;
final Metadata trailers = new Metadata();
NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
@Override
public void start(ClientCall.Listener<String> listener, Metadata headers) {
listener.onMessage(resp);
listener.onClose(status, trailers);
}
};
String actualResponse = ClientCalls.blockingUnaryCall(call, req);
assertEquals(resp, actualResponse);
}
示例5: unaryFutureCallFailed
import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void unaryFutureCallFailed() throws Exception {
final AtomicReference<ClientCall.Listener<String>> listener =
new AtomicReference<ClientCall.Listener<String>>();
NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
@Override
public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
listener.set(responseListener);
}
};
Integer req = 2;
ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
Metadata trailers = new Metadata();
listener.get().onClose(Status.INTERNAL, trailers);
try {
future.get();
fail("Should fail");
} catch (ExecutionException e) {
Status status = Status.fromThrowable(e);
assertEquals(Status.INTERNAL, status);
Metadata metadata = Status.trailersFromThrowable(e);
assertSame(trailers, metadata);
}
}
示例6: blockingResponseStreamFailed
import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void blockingResponseStreamFailed() throws Exception {
final AtomicReference<ClientCall.Listener<String>> listener =
new AtomicReference<ClientCall.Listener<String>>();
NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
@Override
public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
listener.set(responseListener);
}
};
Integer req = 2;
Iterator<String> iter = ClientCalls.blockingServerStreamingCall(call, req);
Metadata trailers = new Metadata();
listener.get().onClose(Status.INTERNAL, trailers);
try {
iter.next();
fail("Should fail");
} catch (Exception e) {
Status status = Status.fromThrowable(e);
assertEquals(Status.INTERNAL, status);
Metadata metadata = Status.trailersFromThrowable(e);
assertSame(trailers, metadata);
}
}
示例7: startCall
import io.grpc.ClientCall; //导入方法依赖的package包/类
static void startCall(ClientCall<Message, Message> call,
ClientCall.Listener<Message> responseListener, boolean streamingResponse) {
call.start(responseListener, new Metadata());
if (streamingResponse) {
call.request(1);
} else {
call.request(2);
}
}
示例8: wrap
import io.grpc.ClientCall; //导入方法依赖的package包/类
protected <RespT> ClientCall.Listener<RespT> wrap(final ClientCall.Listener<RespT> delegate,
final Context timeContext, final AtomicBoolean decremented) {
return new ClientCall.Listener<RespT>() {
@Override
public void onHeaders(Metadata headers) {
delegate.onHeaders(headers);
}
@Override
public void onMessage(RespT message) {
delegate.onMessage(message);
}
@Override
public void onClose(Status status, Metadata trailers) {
try {
if (trailers != null) {
// Be extra defensive since this is only used for logging
trailers.put(CHANNEL_ID_KEY, Integer.toString(channelId));
}
if (!decremented.getAndSet(true)) {
getStats().ACTIVE_RPC_COUNTER.dec();
}
if (!status.isOk()) {
ClientMetrics.meter(MetricLevel.Info, "grpc.errors." + status.getCode().name())
.mark();
}
delegate.onClose(status, trailers);
} finally {
timeContext.close();
}
}
@Override
public void onReady() {
delegate.onReady();
}
};
}
示例9: MonitoringClientCallListener
import io.grpc.ClientCall; //导入方法依赖的package包/类
MonitoringClientCallListener(
ClientCall.Listener<S> delegate,
ClientMetrics clientMetrics,
GrpcMethod grpcMethod,
Configuration configuration,
Clock clock) {
this.delegate = delegate;
this.clientMetrics = clientMetrics;
this.grpcMethod = grpcMethod;
this.configuration = configuration;
this.clock = clock;
this.startInstant = clock.instant();
}
示例10: serverHeaderDeliveredToClient
import io.grpc.ClientCall; //导入方法依赖的package包/类
@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(grpcServerRule.getChannel())
.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));
}
示例11: cancelInOnMessageShouldInvokeStreamCancel
import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void cancelInOnMessageShouldInvokeStreamCancel() throws Exception {
final ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(
method,
MoreExecutors.directExecutor(),
baseCallOptions,
provider,
deadlineCancellationExecutor,
channelCallTracer);
final Exception cause = new Exception();
ClientCall.Listener<Void> callListener =
new ClientCall.Listener<Void>() {
@Override
public void onMessage(Void message) {
call.cancel("foo", cause);
}
};
call.start(callListener, new Metadata());
call.halfClose();
call.request(1);
verify(stream).start(listenerArgumentCaptor.capture());
ClientStreamListener streamListener = listenerArgumentCaptor.getValue();
streamListener.onReady();
streamListener.headersRead(new Metadata());
streamListener
.messagesAvailable(new SingleMessageProducer(new ByteArrayInputStream(new byte[0])));
verify(stream).cancel(statusCaptor.capture());
Status status = statusCaptor.getValue();
assertEquals(Status.CANCELLED.getCode(), status.getCode());
assertEquals("foo", status.getDescription());
assertSame(cause, status.getCause());
}
示例12: startCall
import io.grpc.ClientCall; //导入方法依赖的package包/类
private static <ReqT, RespT> void startCall(ClientCall<ReqT, RespT> call,
ClientCall.Listener<RespT> responseListener, boolean streamingResponse) {
call.start(responseListener, new Metadata());
if (streamingResponse) {
call.request(1);
} else {
// Initially ask for two responses from flow-control so that if a misbehaving server sends
// more than one responses, we can catch it and fail it in the listener.
call.request(2);
}
}
示例13: unaryFutureCallSuccess
import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void unaryFutureCallSuccess() throws Exception {
final AtomicReference<ClientCall.Listener<String>> listener =
new AtomicReference<ClientCall.Listener<String>>();
final AtomicReference<Integer> message = new AtomicReference<Integer>();
final AtomicReference<Boolean> halfClosed = new AtomicReference<Boolean>();
NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
@Override
public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
listener.set(responseListener);
}
@Override
public void sendMessage(Integer msg) {
message.set(msg);
}
@Override
public void halfClose() {
halfClosed.set(true);
}
};
Integer req = 2;
ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
assertEquals(req, message.get());
assertTrue(halfClosed.get());
listener.get().onMessage("bar");
listener.get().onClose(Status.OK, new Metadata());
assertEquals("bar", future.get());
}
示例14: unaryFutureCallCancelled
import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void unaryFutureCallCancelled() throws Exception {
final AtomicReference<ClientCall.Listener<String>> listener =
new AtomicReference<ClientCall.Listener<String>>();
final AtomicReference<String> cancelMessage = new AtomicReference<String>();
final AtomicReference<Throwable> cancelCause = new AtomicReference<Throwable>();
NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
@Override
public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
listener.set(responseListener);
}
@Override
public void cancel(String message, Throwable cause) {
cancelMessage.set(message);
cancelCause.set(cause);
}
};
Integer req = 2;
ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
future.cancel(true);
assertEquals("GrpcFuture was cancelled", cancelMessage.get());
assertNull(cancelCause.get());
listener.get().onMessage("bar");
listener.get().onClose(Status.OK, new Metadata());
try {
future.get();
fail("Should fail");
} catch (CancellationException e) {
// Exepcted
}
}
示例15: start
import io.grpc.ClientCall; //导入方法依赖的package包/类
@Override
public void start(ClientCall.Listener<S> delegate, Metadata metadata) {
clientMetrics.recordCallStarted();
super.start(new MonitoringClientCallListener<>(
delegate, clientMetrics, grpcMethod, configuration, clock), metadata);
}