示例#1
0
 /** Register all methods provided by the given service type. */
 public void registerService(Service service) {
   synchronized (registeredServices) {
     for (MethodDescriptor methodDescriptor : service.getDescriptorForType().getMethods()) {
       registeredServices.put(
           methodDescriptor.getInputType(),
           new RegisteredServiceMethod(service, methodDescriptor));
     }
   }
 }
  private void forwardToService(
      SocketRpcProtos.Request rpcRequest,
      RpcCallback<Message> callback,
      Service service,
      RpcController socketController)
      throws RpcException {
    // Get matching method
    MethodDescriptor method = getMethod(rpcRequest, service.getDescriptorForType());

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

    // Call method
    try {
      service.callMethod(method, socketController, request, callback);
    } catch (RuntimeException e) {
      throw new RpcException(
          ErrorReason.RPC_ERROR, "Error running method " + method.getFullName(), e);
    }
  }
  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);
    }
  }
示例#4
0
    @Override
    public Message callBlockingMethod(
        MethodDescriptor md, RpcController controller, Message request, Message returnType)
        throws ServiceException {

      Message response = null;
      int callId = generateCallId();
      try {
        RequestHeader.Builder builder = RequestHeader.newBuilder();
        builder.setId(callId);
        builder.setRequestName(md.getName());
        RequestHeader header = builder.build();

        LOG.debug("SENDING RPC, CALLID:" + header.getId());
        RpcCall call = new RpcCall(callId, header, request, md);
        long tm = System.currentTimeMillis();

        ctx.writeAndFlush(call);

        RpcCall result = responsesMap.take(callId, rpcTimeout);

        response = result != null ? result.getMessage() : null;
        if (response != null) {
          LOG.debug("response taken: " + callId);
          LOG.debug(
              String.format(
                  "RPC[%d] round trip takes %d ms",
                  header.getId(), (System.currentTimeMillis() - tm)));
        }
      } catch (RpcTimeoutException e) {
        LOG.error("Rpc Timeout, call ID:" + callId + ", remote server:" + getRemoteServer());
        LOG.error("Rpc Timeout, call:" + request);
        LOG.error("Rpc Timeout", e);
        ServiceException se = new ServiceException(e.getMessage(), e);
        throw se;
      } catch (Exception e) {
        LOG.error("ctx:" + ctx);
        LOG.error("callBlockingMethod exception", e);
        throw e;
      }
      return response;
    }
示例#5
0
 public String getMethodIdentifier() {
   return methodDesc.getName();
 }
示例#6
0
 public String getServiceIdentifier() {
   return methodDesc.getService().getName();
 }