io.grpc.stub.ServerCalls.UnaryMethod Java Examples

The following examples show how to use io.grpc.stub.ServerCalls.UnaryMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HelloJsonServer.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public ServerServiceDefinition bindService() {
  return io.grpc.ServerServiceDefinition
      .builder(GreeterGrpc.getServiceDescriptor().getName())
      .addMethod(HelloJsonClient.HelloJsonStub.METHOD_SAY_HELLO,
          asyncUnaryCall(
              new UnaryMethod<HelloRequest, HelloReply>() {
                @Override
                public void invoke(
                    HelloRequest request, StreamObserver<HelloReply> responseObserver) {
                  sayHello(request, responseObserver);
                }
              }))
      .build();
}
 
Example #2
Source File: HelloJsonServer.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public ServerServiceDefinition bindService() {
  return io.grpc.ServerServiceDefinition
      .builder(GreeterGrpc.getServiceDescriptor().getName())
      .addMethod(HelloJsonClient.HelloJsonStub.METHOD_SAY_HELLO,
          asyncUnaryCall(
              new UnaryMethod<HelloRequest, HelloReply>() {
                @Override
                public void invoke(
                    HelloRequest request, StreamObserver<HelloReply> responseObserver) {
                  sayHello(request, responseObserver);
                }
              }))
      .build();
}
 
Example #3
Source File: ClientCallsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void blockingUnaryCall2_interruptedWaitsForOnClose() throws Exception {
  Integer req = 2;

  class NoopUnaryMethod implements UnaryMethod<Integer, Integer> {
    ServerCallStreamObserver<Integer> observer;

    @Override public void invoke(Integer request, StreamObserver<Integer> responseObserver) {
      observer = (ServerCallStreamObserver<Integer>) responseObserver;
    }
  }

  NoopUnaryMethod methodImpl = new NoopUnaryMethod();
  server = InProcessServerBuilder.forName("noop").directExecutor()
      .addService(ServerServiceDefinition.builder("some")
          .addMethod(UNARY_METHOD, ServerCalls.asyncUnaryCall(methodImpl))
          .build())
      .build().start();

  InterruptInterceptor interceptor = new InterruptInterceptor();
  channel = InProcessChannelBuilder.forName("noop")
      .directExecutor()
      .intercept(interceptor)
      .build();
  try {
    ClientCalls.blockingUnaryCall(channel, UNARY_METHOD, CallOptions.DEFAULT, req);
    fail();
  } catch (StatusRuntimeException ex) {
    assertTrue(Thread.interrupted());
    assertTrue("interrupted", ex.getCause() instanceof InterruptedException);
  }
  assertTrue("onCloseCalled", interceptor.onCloseCalled);
  assertTrue("context not cancelled", methodImpl.observer.isCancelled());
}
 
Example #4
Source File: TestServer.java    From concurrency-limits with Apache License 2.0 4 votes vote down vote up
private TestServer(final Builder builder) throws IOException {
    this.semaphore = new Semaphore(builder.concurrency, true);
    
    ServerCallHandler<String, String> handler = ServerCalls.asyncUnaryCall(new UnaryMethod<String, String>() {
        volatile int segment = 0;
        
        {
            Executors.newSingleThreadExecutor().execute(() -> {
                while (true) {
                    Segment s = builder.segments.get(0);
                    Uninterruptibles.sleepUninterruptibly(s.duration(), TimeUnit.NANOSECONDS);
                    segment = segment++ % builder.segments.size();
                }
            });
        }
        
        @Override
        public void invoke(String req, StreamObserver<String> observer) {
            try {
                long delay = builder.segments.get(0).latency();
                semaphore.acquire();
                TimeUnit.MILLISECONDS.sleep(delay);
                observer.onNext("response");
                observer.onCompleted();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                observer.onError(Status.UNKNOWN.asRuntimeException());
            } finally {
                semaphore.release();
            }
        }
    });
    
    this.server = NettyServerBuilder.forPort(0)
            .addService(ServerInterceptors.intercept(ServerServiceDefinition.builder("service")
                    .addMethod(METHOD_DESCRIPTOR, handler)  // Rate = Limit / Latency = 2 / 0.02 = 100
                    .build(), ConcurrencyLimitServerInterceptor.newBuilder(builder.limiter)
                    .build()
                ))
            .build()
            .start();
}