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


Java ManagedChannel.newCall方法代码示例

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


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

示例1: startStreamingCalls

import io.grpc.ManagedChannel; //导入方法依赖的package包/类
/**
 * Start a continuously executing set of duplex streaming ping-pong calls that will terminate when
 * {@code done.get()} is true. Each completed call will increment the counter by the specified
 * delta which benchmarks can use to measure messages per second or bandwidth.
 */
protected CountDownLatch startStreamingCalls(int callsPerChannel, final AtomicLong counter,
    final AtomicBoolean record, final AtomicBoolean done, final long counterDelta) {
  final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length);
  for (final ManagedChannel channel : channels) {
    for (int i = 0; i < callsPerChannel; i++) {
      final ClientCall<ByteBuf, ByteBuf> streamingCall =
          channel.newCall(pingPongMethod, CALL_OPTIONS);
      final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef =
          new AtomicReference<StreamObserver<ByteBuf>>();
      final AtomicBoolean ignoreMessages = new AtomicBoolean();
      StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall(
          streamingCall,
          new StreamObserver<ByteBuf>() {
            @Override
            public void onNext(ByteBuf value) {
              if (done.get()) {
                if (!ignoreMessages.getAndSet(true)) {
                  requestObserverRef.get().onCompleted();
                }
                return;
              }
              requestObserverRef.get().onNext(request.slice());
              if (record.get()) {
                counter.addAndGet(counterDelta);
              }
              // request is called automatically because the observer implicitly has auto
              // inbound flow control
            }

            @Override
            public void onError(Throwable t) {
              logger.log(Level.WARNING, "call error", t);
              latch.countDown();
            }

            @Override
            public void onCompleted() {
              latch.countDown();
            }
          });
      requestObserverRef.set(requestObserver);
      requestObserver.onNext(request.slice());
      requestObserver.onNext(request.slice());
    }
  }
  return latch;
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:53,代码来源:AbstractBenchmark.java

示例2: startFlowControlledStreamingCalls

import io.grpc.ManagedChannel; //导入方法依赖的package包/类
/**
 * Start a continuously executing set of duplex streaming ping-pong calls that will terminate when
 * {@code done.get()} is true. Each completed call will increment the counter by the specified
 * delta which benchmarks can use to measure messages per second or bandwidth.
 */
protected CountDownLatch startFlowControlledStreamingCalls(int callsPerChannel,
    final AtomicLong counter, final AtomicBoolean record, final AtomicBoolean done,
    final long counterDelta) {
  final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length);
  for (final ManagedChannel channel : channels) {
    for (int i = 0; i < callsPerChannel; i++) {
      final ClientCall<ByteBuf, ByteBuf> streamingCall =
          channel.newCall(flowControlledStreaming, CALL_OPTIONS);
      final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef =
          new AtomicReference<StreamObserver<ByteBuf>>();
      final AtomicBoolean ignoreMessages = new AtomicBoolean();
      StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall(
          streamingCall,
          new StreamObserver<ByteBuf>() {
            @Override
            public void onNext(ByteBuf value) {
              StreamObserver<ByteBuf> obs = requestObserverRef.get();
              if (done.get()) {
                if (!ignoreMessages.getAndSet(true)) {
                  obs.onCompleted();
                }
                return;
              }
              if (record.get()) {
                counter.addAndGet(counterDelta);
              }
              // request is called automatically because the observer implicitly has auto
              // inbound flow control
            }

            @Override
            public void onError(Throwable t) {
              logger.log(Level.WARNING, "call error", t);
              latch.countDown();
            }

            @Override
            public void onCompleted() {
              latch.countDown();
            }
          });
      requestObserverRef.set(requestObserver);

      // Add some outstanding requests to ensure the server is filling the connection
      streamingCall.request(5);
      requestObserver.onNext(request.slice());
    }
  }
  return latch;
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:56,代码来源:AbstractBenchmark.java

示例3: oobTransportDoesNotAffectIdleness

import io.grpc.ManagedChannel; //导入方法依赖的package包/类
@Test
public void oobTransportDoesNotAffectIdleness() {
  // Start a call, which goes to delayed transport
  ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
  call.start(mockCallListener, new Metadata());

  // Verify that we have exited the idle mode
  ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
  verify(mockLoadBalancerFactory).newLoadBalancer(helperCaptor.capture());
  Helper helper = helperCaptor.getValue();

  // Fail the RPC
  SubchannelPicker failingPicker = mock(SubchannelPicker.class);
  when(failingPicker.pickSubchannel(any(PickSubchannelArgs.class)))
      .thenReturn(PickResult.withError(Status.UNAVAILABLE));
  helper.updateBalancingState(TRANSIENT_FAILURE, failingPicker);
  executor.runDueTasks();
  verify(mockCallListener).onClose(same(Status.UNAVAILABLE), any(Metadata.class));

  // ... so that the channel resets its in-use state
  assertFalse(channel.inUseStateAggregator.isInUse());

  // Now make an RPC on an OOB channel
  ManagedChannel oob = helper.createOobChannel(servers.get(0), "oobauthority");
  verify(mockTransportFactory, never())
      .newClientTransport(any(SocketAddress.class), same("oobauthority"), same(USER_AGENT),
          same(NO_PROXY));
  ClientCall<String, Integer> oobCall = oob.newCall(method, CallOptions.DEFAULT);
  oobCall.start(mockCallListener2, new Metadata());
  verify(mockTransportFactory)
      .newClientTransport(any(SocketAddress.class), same("oobauthority"), same(USER_AGENT),
          same(NO_PROXY));
  MockClientTransportInfo oobTransportInfo = newTransports.poll();
  assertEquals(0, newTransports.size());
  // The OOB transport reports in-use state
  oobTransportInfo.listener.transportInUse(true);

  // But it won't stop the channel from going idle
  verify(mockLoadBalancer, never()).shutdown();
  timer.forwardTime(IDLE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
  verify(mockLoadBalancer).shutdown();
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:43,代码来源:ManagedChannelImplIdlenessTest.java

示例4: inprocessTransportManualFlow

import io.grpc.ManagedChannel; //导入方法依赖的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.ManagedChannel.newCall方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。