当前位置: 首页>>代码示例>>Java>>正文


Java ClientCall.halfClose方法代码示例

本文整理汇总了Java中io.grpc.ClientCall.halfClose方法的典型用法代码示例。如果您正苦于以下问题:Java ClientCall.halfClose方法的具体用法?Java ClientCall.halfClose怎么用?Java ClientCall.halfClose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.grpc.ClientCall的用法示例。


在下文中一共展示了ClientCall.halfClose方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: advancedAsyncCall

import io.grpc.ClientCall; //导入方法依赖的package包/类
/**
 * This is more advanced and does not make use of the stub.  You should not normally need to do
 * this, but here is how you would.
 */
void advancedAsyncCall() {
  ClientCall<HelloRequest, HelloReply> call =
      channel.newCall(GreeterGrpc.getSayHelloMethod(), CallOptions.DEFAULT);

  final CountDownLatch latch = new CountDownLatch(1);

  call.start(new ClientCall.Listener<HelloReply>() {

    @Override
    public void onClose(Status status, Metadata trailers) {
      Verify.verify(status.getCode() == Status.Code.INTERNAL);
      Verify.verify(status.getDescription().contains("Narwhal"));
      // Cause is not transmitted over the wire.
      latch.countDown();
    }
  }, new Metadata());

  call.sendMessage(HelloRequest.newBuilder().setName("Marge").build());
  call.halfClose();

  if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
    throw new RuntimeException("timeout!");
  }
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:29,代码来源:ErrorHandlingClient.java

示例2: greet

import io.grpc.ClientCall; //导入方法依赖的package包/类
/** Say hello to server. */
public void greet(final String name) {
  final ClientCall<HelloRequest, HelloReply> call =
      channel.newCall(GreeterGrpc.METHOD_SAY_HELLO, CallOptions.DEFAULT);

  final CountDownLatch latch = new CountDownLatch(1);

  call.start(new Listener<HelloReply>() {
    @Override
    public void onHeaders(Metadata headers) {
      super.onHeaders(headers);
      String encoding = headers.get(GrpcUtil.MESSAGE_ENCODING_KEY);
      if (encoding == null) {
        throw new RuntimeException("No compression selected!");
      }
    }

    @Override
    public void onMessage(HelloReply message) {
      super.onMessage(message);
      logger.info("Greeting: " + message.getMessage());
      latch.countDown();
    }

    @Override
    public void onClose(Status status, Metadata trailers) {
      latch.countDown();
      if (!status.isOk()) {
        throw status.asRuntimeException();
      }
    }
  }, new Metadata());

  call.setMessageCompression(true);
  call.sendMessage(HelloRequest.newBuilder().setName(name).build());
  call.request(1);
  call.halfClose();

  Uninterruptibles.awaitUninterruptibly(latch, 100, TimeUnit.SECONDS);
}
 
开发者ID:lrtdc,项目名称:book_ldrtc,代码行数:41,代码来源:CompressingHelloWorldClient.java

示例3: advancedAsyncCall

import io.grpc.ClientCall; //导入方法依赖的package包/类
/**
 * This is more advanced and does not make use of the stub.  You should not normally need to do
 * this, but here is how you would.
 */
void advancedAsyncCall() {
  ClientCall<HelloRequest, HelloReply> call =
      channel.newCall(GreeterGrpc.getSayHelloMethod(), CallOptions.DEFAULT);

  final CountDownLatch latch = new CountDownLatch(1);

  call.start(new ClientCall.Listener<HelloReply>() {

    @Override
    public void onClose(Status status, Metadata trailers) {
      Verify.verify(status.getCode() == Status.Code.INTERNAL);
      Verify.verify(trailers.containsKey(DEBUG_INFO_TRAILER_KEY));
      try {
        Verify.verify(trailers.get(DEBUG_INFO_TRAILER_KEY).equals(DEBUG_INFO));
      } catch (IllegalArgumentException e) {
        throw new VerifyException(e);
      }

      latch.countDown();
    }
  }, new Metadata());

  call.sendMessage(HelloRequest.newBuilder().build());
  call.halfClose();

  if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
    throw new RuntimeException("timeout!");
  }
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:34,代码来源:DetailErrorSample.java

示例4: channelStat_callEndSuccess

import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void channelStat_callEndSuccess() throws Exception {
  // set up
  Metadata headers = new Metadata();
  ClientStream mockStream = mock(ClientStream.class);
  createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);

  // Start a call with a call executor
  CallOptions options =
      CallOptions.DEFAULT.withExecutor(executor.getScheduledExecutorService());
  ClientCall<String, Integer> call = channel.newCall(method, options);
  call.start(mockCallListener, headers);

  // Make the transport available
  Subchannel subchannel = helper.createSubchannel(addressGroup, Attributes.EMPTY);
  subchannel.requestConnection();
  MockClientTransportInfo transportInfo = transports.poll();
  ConnectionClientTransport mockTransport = transportInfo.transport;
  ManagedClientTransport.Listener transportListener = transportInfo.listener;
  when(mockTransport.newStream(same(method), same(headers), any(CallOptions.class)))
      .thenReturn(mockStream);
  transportListener.transportReady();
  when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class)))
      .thenReturn(PickResult.withSubchannel(subchannel));
  helper.updateBalancingState(READY, mockPicker);

  executor.runDueTasks();
  verify(mockStream).start(streamListenerCaptor.capture());
  // end set up

  // the actual test
  ClientStreamListener streamListener = streamListenerCaptor.getValue();
  call.halfClose();
  assertEquals(0, getStats(channel).callsSucceeded);
  assertEquals(0, getStats(channel).callsFailed);
  streamListener.closed(Status.OK, new Metadata());
  executor.runDueTasks();
  assertEquals(1, getStats(channel).callsSucceeded);
  assertEquals(0, getStats(channel).callsFailed);
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:41,代码来源:ManagedChannelImplTest.java

示例5: channelStat_callEndSuccess_oob

import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void channelStat_callEndSuccess_oob() throws Exception {
  // set up
  ClientStream mockStream = mock(ClientStream.class);
  createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);

  OobChannel oobChannel = (OobChannel) helper.createOobChannel(addressGroup, "oobauthority");
  FakeClock callExecutor = new FakeClock();
  CallOptions options =
      CallOptions.DEFAULT.withExecutor(callExecutor.getScheduledExecutorService());
  ClientCall<String, Integer> call = oobChannel.newCall(method, options);
  Metadata headers = new Metadata();
  call.start(mockCallListener, headers);

  MockClientTransportInfo transportInfo = transports.poll();
  ConnectionClientTransport mockTransport = transportInfo.transport;
  ManagedClientTransport.Listener transportListener = transportInfo.listener;
  when(mockTransport.newStream(same(method), same(headers), any(CallOptions.class)))
      .thenReturn(mockStream);
  transportListener.transportReady();
  callExecutor.runDueTasks();
  verify(mockStream).start(streamListenerCaptor.capture());
  // end set up

  // the actual test
  ClientStreamListener streamListener = streamListenerCaptor.getValue();
  call.halfClose();
  assertEquals(0, getStats(oobChannel).callsSucceeded);
  assertEquals(0, getStats(oobChannel).callsFailed);
  streamListener.closed(Status.OK, new Metadata());
  callExecutor.runDueTasks();
  // only oob channel stats updated
  assertEquals(1, getStats(oobChannel).callsSucceeded);
  assertEquals(0, getStats(oobChannel).callsFailed);
  assertEquals(0, getStats(channel).callsSucceeded);
  assertEquals(0, getStats(channel).callsFailed);
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:38,代码来源:ManagedChannelImplTest.java

示例6: serverStreamingShouldBeFlowControlled

import io.grpc.ClientCall; //导入方法依赖的package包/类
public void serverStreamingShouldBeFlowControlled() throws Exception {
  final StreamingOutputCallRequest request = new StreamingOutputCallRequest();
  request.responseType = Messages.PayloadType.COMPRESSABLE;
  request.responseParameters = new ResponseParameters[2];
  request.responseParameters[0] = new ResponseParameters();
  request.responseParameters[0].size = 100000;
  request.responseParameters[1] = new ResponseParameters();
  request.responseParameters[1].size = 100001;
  final StreamingOutputCallResponse[] goldenResponses = new StreamingOutputCallResponse[2];
  goldenResponses[0] = new StreamingOutputCallResponse();
  goldenResponses[0].payload = new Payload();
  goldenResponses[0].payload.type = Messages.PayloadType.COMPRESSABLE;
  goldenResponses[0].payload.body = new byte[100000];
  goldenResponses[1] = new StreamingOutputCallResponse();
  goldenResponses[1].payload = new Payload();
  goldenResponses[1].payload.type = Messages.PayloadType.COMPRESSABLE;
  goldenResponses[1].payload.body = new byte[100001];

  long start = System.nanoTime();

  final ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(10);
  ClientCall<StreamingOutputCallRequest, StreamingOutputCallResponse> call =
      channel.newCall(TestServiceGrpc.getStreamingOutputCallMethod(), CallOptions.DEFAULT);
  call.start(new ClientCall.Listener<StreamingOutputCallResponse>() {
    @Override
    public void onHeaders(Metadata headers) {}

    @Override
    public void onMessage(final StreamingOutputCallResponse message) {
      queue.add(message);
    }

    @Override
    public void onClose(io.grpc.Status status, Metadata trailers) {
      queue.add(status);
    }
  }, new Metadata());
  call.sendMessage(request);
  call.halfClose();

  // Time how long it takes to get the first response.
  call.request(1);
  assertMessageEquals(goldenResponses[0],
      (StreamingOutputCallResponse) queue.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
  long firstCallDuration = System.nanoTime() - start;

  // Without giving additional flow control, make sure that we don't get another response. We wait
  // until we are comfortable the next message isn't coming. We may have very low nanoTime
  // resolution (like on Windows) or be using a testing, in-process transport where message
  // handling is instantaneous. In both cases, firstCallDuration may be 0, so round up sleep time
  // to at least 1ms.
  assertNull(queue.poll(Math.max(firstCallDuration * 4, 1 * 1000 * 1000), TimeUnit.NANOSECONDS));

  // Make sure that everything still completes.
  call.request(1);
  assertMessageEquals(goldenResponses[1],
      (StreamingOutputCallResponse) queue.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
  assertCodeEquals(io.grpc.Status.OK,
      (io.grpc.Status) queue.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:61,代码来源:InteropTester.java

示例7: serverStreamingShouldBeFlowControlled

import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void serverStreamingShouldBeFlowControlled() throws Exception {
  final StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder()
      .setResponseType(COMPRESSABLE)
      .addResponseParameters(ResponseParameters.newBuilder().setSize(100000))
      .addResponseParameters(ResponseParameters.newBuilder().setSize(100001))
      .build();
  final List<StreamingOutputCallResponse> goldenResponses = Arrays.asList(
      StreamingOutputCallResponse.newBuilder()
          .setPayload(Payload.newBuilder()
              .setType(PayloadType.COMPRESSABLE)
              .setBody(ByteString.copyFrom(new byte[100000]))).build(),
      StreamingOutputCallResponse.newBuilder()
          .setPayload(Payload.newBuilder()
              .setType(PayloadType.COMPRESSABLE)
              .setBody(ByteString.copyFrom(new byte[100001]))).build());

  long start = System.nanoTime();

  final ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(10);
  ClientCall<StreamingOutputCallRequest, StreamingOutputCallResponse> call =
      channel.newCall(TestServiceGrpc.getStreamingOutputCallMethod(), CallOptions.DEFAULT);
  call.start(new ClientCall.Listener<StreamingOutputCallResponse>() {
    @Override
    public void onHeaders(Metadata headers) {}

    @Override
    public void onMessage(final StreamingOutputCallResponse message) {
      queue.add(message);
    }

    @Override
    public void onClose(Status status, Metadata trailers) {
      queue.add(status);
    }
  }, new Metadata());
  call.sendMessage(request);
  call.halfClose();

  // Time how long it takes to get the first response.
  call.request(1);
  assertEquals(goldenResponses.get(0),
      queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS));
  long firstCallDuration = System.nanoTime() - start;

  // Without giving additional flow control, make sure that we don't get another response. We wait
  // until we are comfortable the next message isn't coming. We may have very low nanoTime
  // resolution (like on Windows) or be using a testing, in-process transport where message
  // handling is instantaneous. In both cases, firstCallDuration may be 0, so round up sleep time
  // to at least 1ms.
  assertNull(queue.poll(Math.max(firstCallDuration * 4, 1 * 1000 * 1000), TimeUnit.NANOSECONDS));

  // Make sure that everything still completes.
  call.request(1);
  assertEquals(goldenResponses.get(1),
      queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS));
  assertEquals(Status.OK, queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS));
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:59,代码来源:AbstractInteropTest.java

示例8: inprocessTransportManualFlow

import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void inprocessTransportManualFlow() throws Exception {
  final Semaphore semaphore = new Semaphore(1);
  ServerServiceDefinition service = ServerServiceDefinition.builder(
      new ServiceDescriptor("some", STREAMING_METHOD))
      .addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall(
          new ServerCalls.BidiStreamingMethod<Integer, Integer>() {
            int iteration;

            @Override
            public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
              final ServerCallStreamObserver<Integer> serverCallObserver =
                  (ServerCallStreamObserver<Integer>) responseObserver;
              serverCallObserver.setOnReadyHandler(new Runnable() {
                @Override
                public void run() {
                  while (serverCallObserver.isReady()) {
                    serverCallObserver.onNext(iteration);
                  }
                  iteration++;
                  semaphore.release();
                }
              });
              return new ServerCalls.NoopStreamObserver<Integer>() {
                @Override
                public void onCompleted() {
                  serverCallObserver.onCompleted();
                }
              };
            }
          }))
      .build();
  long tag = System.nanoTime();
  InProcessServerBuilder.forName("go-with-the-flow" + tag).addService(service).build().start();
  ManagedChannel channel = InProcessChannelBuilder.forName("go-with-the-flow" + tag).build();
  final ClientCall<Integer, Integer> clientCall = channel.newCall(STREAMING_METHOD,
      CallOptions.DEFAULT);
  final CountDownLatch latch = new CountDownLatch(1);
  final int[] receivedMessages = new int[6];
  clientCall.start(new ClientCall.Listener<Integer>() {
    int index;

    @Override
    public void onMessage(Integer message) {
      receivedMessages[index++] = message;
    }

    @Override
    public void onClose(Status status, Metadata trailers) {
      latch.countDown();
    }
  }, new Metadata());
  semaphore.acquire();
  clientCall.request(1);
  semaphore.acquire();
  clientCall.request(2);
  semaphore.acquire();
  clientCall.request(3);
  clientCall.halfClose();
  latch.await(5, TimeUnit.SECONDS);
  // Very that number of messages produced in each onReady handler call matches the number
  // requested by the client.
  assertArrayEquals(new int[]{0, 1, 1, 2, 2, 2}, receivedMessages);
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:65,代码来源:ServerCallsTest.java


注:本文中的io.grpc.ClientCall.halfClose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。