Пример #1
0
 public ServiceHolderImpl(@NotNull BlockingService... services) {
   final ImmutableMap.Builder<String, ServiceInfo> builder =
       ImmutableMap.<String, ServiceInfo>builder();
   for (BlockingService service : services) {
     String name = service.getDescriptorForType().getName().toLowerCase();
     ServiceInfo info = new ServiceInfo(new BlockingServiceWrapper(service));
     builder.put(name, info);
   }
   this.services = builder.build();
 }
  private Response forwardToBlockingService(Request rpcRequest, BlockingService blockingService)
      throws RpcException {
    // Get matching method
    MethodDescriptor method = getMethod(rpcRequest, blockingService.getDescriptorForType());

    // Create request for method
    Message request = getRequestProto(rpcRequest, blockingService.getRequestPrototype(method));

    // Call method
    SocketRpcController socketController = new SocketRpcController();
    try {
      Message response = blockingService.callBlockingMethod(method, socketController, request);
      return createRpcResponse(response, true, socketController);
    } catch (ServiceException e) {
      throw new RpcException(ErrorReason.RPC_FAILED, e.getMessage(), e);
    } catch (RuntimeException e) {
      throw new RpcException(
          ErrorReason.RPC_ERROR, "Error running method " + method.getFullName(), e);
    }
  }
 @Override
 public void callMethod(
     @NotNull Descriptors.MethodDescriptor method,
     @NotNull RpcController controller,
     @NotNull Message request,
     @NotNull RpcCallback<Message> done) {
   try {
     done.run(service.callBlockingMethod(method, controller, request));
   } catch (ServiceException e) {
     log.error("Blocking method error " + method.getFullName(), e);
     done.run(null);
   }
 }
 @NotNull
 @Override
 public Message getResponsePrototype(@NotNull Descriptors.MethodDescriptor method) {
   return service.getResponsePrototype(method);
 }
 @NotNull
 @Override
 public Descriptors.ServiceDescriptor getDescriptorForType() {
   return service.getDescriptorForType();
 }
 /** Register an RPC blocking service implementation to this forwarder. */
 public void registerBlockingService(BlockingService service) {
   blockingServiceMap.put(service.getDescriptorForType().getFullName(), service);
 }