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


Java ClientCall.sendMessage方法代码示例

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


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

import io.grpc.ClientCall; //导入方法依赖的package包/类
@Override
public void run() {
  while (true) {
    maxOutstanding.acquireUninterruptibly();
    if (shutdown) {
      maxOutstanding.release();
      return;
    }
    final ClientCall<ByteBuf, ByteBuf> call =
        channel.newCall(LoadServer.GENERIC_STREAMING_PING_PONG_METHOD, CallOptions.DEFAULT);
    call.start(new ClientCall.Listener<ByteBuf>() {
      long now = System.nanoTime();

      @Override
      public void onMessage(ByteBuf message) {
        delay(System.nanoTime() - now);
        if (shutdown) {
          call.cancel("Shutting down", null);
          return;
        }
        call.request(1);
        call.sendMessage(genericRequest.slice());
        now = System.nanoTime();
      }

      @Override
      public void onClose(Status status, Metadata trailers) {
        maxOutstanding.release();
        Level level = shutdown ? Level.FINE : Level.INFO;
        if (!status.isOk() && status.getCode() != Status.Code.CANCELLED) {
          log.log(level, "Error in Generic Async Ping-Pong call", status.getCause());
        }
      }
    }, new Metadata());
    call.request(1);
    call.sendMessage(genericRequest.slice());
  }
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:39,代码来源:LoadClient.java

示例5: 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

示例6: 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

示例7: binaryLogTest

import io.grpc.ClientCall; //导入方法依赖的package包/类
@Test
public void binaryLogTest() throws Exception {
  final List<Object> capturedReqs = new ArrayList<Object>();
  final class TracingClientInterceptor implements ClientInterceptor {
    private final List<MethodDescriptor<?, ?>> interceptedMethods =
        new ArrayList<MethodDescriptor<?, ?>>();

    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
      interceptedMethods.add(method);
      return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
        @Override
        public void sendMessage(ReqT message) {
          capturedReqs.add(message);
          super.sendMessage(message);
        }
      };
    }
  }

  TracingClientInterceptor userInterceptor = new TracingClientInterceptor();
  binlogProvider = new BinaryLogProvider() {
    @Nullable
    @Override
    public ServerInterceptor getServerInterceptor(String fullMethodName) {
      return null;
    }

    @Override
    public ClientInterceptor getClientInterceptor(String fullMethodName) {
      return new TracingClientInterceptor();
    }

    @Override
    protected int priority() {
      return 0;
    }
  };
  createChannel(
      new FakeNameResolverFactory(true),
      Collections.<ClientInterceptor>singletonList(userInterceptor));
  ClientCall<String, Integer> call =
      channel.newCall(method, CallOptions.DEFAULT.withDeadlineAfter(0, TimeUnit.NANOSECONDS));
  ClientCall.Listener<Integer> listener = new NoopClientCallListener<Integer>();
  call.start(listener, new Metadata());
  assertEquals(1, executor.runDueTasks());

  String actualRequest = "hello world";
  call.sendMessage(actualRequest);

  // The user supplied interceptor must still operate on the original message types
  assertThat(userInterceptor.interceptedMethods).hasSize(1);
  assertSame(
      method.getRequestMarshaller(),
      userInterceptor.interceptedMethods.get(0).getRequestMarshaller());
  assertSame(
      method.getResponseMarshaller(),
      userInterceptor.interceptedMethods.get(0).getResponseMarshaller());

  // The binlog interceptor must be closest to the transport
  assertThat(capturedReqs).hasSize(2);
  // The InputStream is already spent, so just check its type rather than contents
  assertEquals(actualRequest, capturedReqs.get(0));
  assertThat(capturedReqs.get(1)).isInstanceOf(InputStream.class);
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:67,代码来源:ManagedChannelImplTest.java


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