Java Code Examples for io.grpc.ServerServiceDefinition#Builder

The following examples show how to use io.grpc.ServerServiceDefinition#Builder . 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: TripleServer.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
private ServerServiceDefinition buildSofaServiceDef(GenericServiceImpl genericService,
                                                    ProviderConfig providerConfig, Invoker instance) {
    ServerServiceDefinition templateDefinition = genericService.bindService();
    ServerCallHandler<Request, Response> templateHandler = (ServerCallHandler<Request, Response>) templateDefinition
        .getMethods().iterator().next().getServerCallHandler();
    List<MethodDescriptor<Request, Response>> methodDescriptor = getMethodDescriptor(providerConfig);
    List<ServerMethodDefinition<Request, Response>> methodDefs = getMethodDefinitions(templateHandler,
        methodDescriptor);
    ServerServiceDefinition.Builder builder = ServerServiceDefinition.builder(getServiceDescriptor(
        templateDefinition, providerConfig, methodDescriptor));
    for (ServerMethodDefinition<Request, Response> methodDef : methodDefs) {
        builder.addMethod(methodDef);

    }
    return builder.build();

}
 
Example 2
Source File: HandlerRegistryBenchmark.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Set up the registry.
 */
@Setup(Level.Trial)
public void setup() throws Exception {
  registry = new MutableHandlerRegistry();
  fullMethodNames = new ArrayList<>(serviceCount * methodCountPerService);
  for (int serviceIndex = 0; serviceIndex < serviceCount; ++serviceIndex) {
    String serviceName = randomString();
    ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(serviceName);
    for (int methodIndex = 0; methodIndex < methodCountPerService; ++methodIndex) {
      String methodName = randomString();

      MethodDescriptor<Void, Void> methodDescriptor = MethodDescriptor.<Void, Void>newBuilder()
          .setType(MethodDescriptor.MethodType.UNKNOWN)
          .setFullMethodName(MethodDescriptor.generateFullMethodName(serviceName, methodName))
          .setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
          .setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
          .build();
      serviceBuilder.addMethod(methodDescriptor,
          new ServerCallHandler<Void, Void>() {
            @Override
            public Listener<Void> startCall(ServerCall<Void, Void> call,
                Metadata headers) {
              return null;
            }
          });
      fullMethodNames.add(methodDescriptor.getFullMethodName());
    }
    registry.addService(serviceBuilder.build());
  }
}
 
Example 3
Source File: GrpcToReactorServerBuilder.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
ServerServiceDefinition build() {
    MethodHandlersBuilder<CONTEXT, REACT_SERVICE> handlersBuilder = new MethodHandlersBuilder<>(reactorService, serviceDescriptor, contextType, contextResolver, reactorDetailedFallbackClass);

    ServerServiceDefinition.Builder builder = ServerServiceDefinition.builder(serviceDescriptor);
    handlersBuilder.getUnaryMethodHandlers().forEach(handler -> {
        builder.addMethod(handler.getMethodDescriptor(), asyncUnaryCall(handler));
    });
    handlersBuilder.getServerStreamingMethodHandlers().forEach(handler -> {
        builder.addMethod(handler.getMethodDescriptor(), asyncServerStreamingCall(handler));
    });
    return builder.build();
}
 
Example 4
Source File: GrpcRpcProtocolServer.java    From heroic with Apache License 2.0 5 votes vote down vote up
private ServerServiceDefinition bindService() {
    final ServerServiceDefinition.Builder builder =
        ServerServiceDefinition.builder(GrpcRpcProtocol.SERVICE);

    for (final GrpcEndpointHandle<?, ?> spec : container.getEndpoints()) {
        final ServerCallHandler<byte[], byte[]> handler =
            serverCallHandlerFor((GrpcEndpointHandle<Object, Object>) spec);
        builder.addMethod(spec.descriptor(), handler);
    }

    return builder.build();
}
 
Example 5
Source File: HandlerRegistryBenchmark.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Set up the registry.
 */
@Setup(Level.Trial)
public void setup() throws Exception {
  registry = new MutableHandlerRegistry();
  fullMethodNames = new ArrayList<>(serviceCount * methodCountPerService);
  for (int serviceIndex = 0; serviceIndex < serviceCount; ++serviceIndex) {
    String serviceName = randomString();
    ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(serviceName);
    for (int methodIndex = 0; methodIndex < methodCountPerService; ++methodIndex) {
      String methodName = randomString();

      MethodDescriptor<Void, Void> methodDescriptor = MethodDescriptor.<Void, Void>newBuilder()
          .setType(MethodDescriptor.MethodType.UNKNOWN)
          .setFullMethodName(MethodDescriptor.generateFullMethodName(serviceName, methodName))
          .setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
          .setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
          .build();
      serviceBuilder.addMethod(methodDescriptor,
          new ServerCallHandler<Void, Void>() {
            @Override
            public Listener<Void> startCall(ServerCall<Void, Void> call,
                Metadata headers) {
              return null;
            }
          });
      fullMethodNames.add(methodDescriptor.getFullMethodName());
    }
    registry.addService(serviceBuilder.build());
  }
}
 
Example 6
Source File: DefaultProxyExporter.java    From saluki with Apache License 2.0 4 votes vote down vote up
@Override
public ServerServiceDefinition export(Class<?> protocol, Object protocolImpl) {
  Class<?> serivce = protocol;
  Object serviceRef = protocolImpl;
  String serviceName = protocol.getName();
  ServerServiceDefinition.Builder serviceDefBuilder =
      ServerServiceDefinition.builder(serviceName);
  List<Method> methods = ReflectUtils.findAllPublicMethods(serivce);
  if (methods.isEmpty()) {
    throw new IllegalStateException(
        "protocolClass " + serviceName + " not have export method" + serivce);
  }
  final ConcurrentMap<String, AtomicInteger> concurrents = Maps.newConcurrentMap();
  for (Method method : methods) {
    MethodDescriptor<Message, Message> methodDescriptor =
        GrpcUtil.createMethodDescriptor(serivce, method);
    GrpcMethodType grpcMethodType = method.getAnnotation(GrpcMethodType.class);
    switch (grpcMethodType.methodType()) {
      case UNARY:
        serviceDefBuilder.addMethod(methodDescriptor,
            ServerCalls.asyncUnaryCall(new ServerInvocation(serviceRef, method, grpcMethodType,
                providerUrl, concurrents, clientServerMonitor)));
        break;
      case CLIENT_STREAMING:
        serviceDefBuilder.addMethod(methodDescriptor,
            ServerCalls.asyncClientStreamingCall(new ServerInvocation(serviceRef, method,
                grpcMethodType, providerUrl, concurrents, clientServerMonitor)));
        break;
      case SERVER_STREAMING:
        serviceDefBuilder.addMethod(methodDescriptor,
            ServerCalls.asyncServerStreamingCall(new ServerInvocation(serviceRef, method,
                grpcMethodType, providerUrl, concurrents, clientServerMonitor)));
        break;
      case BIDI_STREAMING:
        serviceDefBuilder.addMethod(methodDescriptor,
            ServerCalls.asyncBidiStreamingCall(new ServerInvocation(serviceRef, method,
                grpcMethodType, providerUrl, concurrents, clientServerMonitor)));
        break;
      default:
        RpcServiceException rpcFramwork =
            new RpcServiceException(RpcErrorMsgConstant.SERVICE_UNFOUND);
        throw rpcFramwork;
    }
  }
  log.info("'{}' service has been registered.", serviceName);
  return serviceDefBuilder.build();
}