Class AbstractRpcClient

java.lang.Object
org.knime.core.webui.data.rpc.AbstractRpcClient
All Implemented Interfaces:
RpcClient
Direct Known Subclasses:
AbstractRpcSingleClient, JsonRpcClient

public abstract class AbstractRpcClient extends Object implements RpcClient
Base class for node data service client implementations, such as JsonRpcClient.
Since:
4.3
Author:
Martin Horn, KNIME GmbH, Konstanz, Germany, Carl Witt, KNIME AG, Zurich, Switzerland
  • Constructor Details

    • AbstractRpcClient

      protected AbstractRpcClient()
      Get the rpc transport from the extension point.
    • AbstractRpcClient

      protected AbstractRpcClient(RpcTransport rpcTransport)
      For testing only: Constructor to initialize an rpc client with custom transport.
      Parameters:
      rpcTransport - a custom rpc transport for testing
  • Method Details

    • convertCall

      protected abstract String convertCall(String serviceName, Method method, Object[] args)
      Converts a call to the node model's data service interface to String.
      For instance, method countChars("abc", 'b') on a service interface could be represented in JSON-RPC as {"jsonrpc": "2.0", "method": "countChars", "params": ["abc", "b"], "id": 3}
      Parameters:
      serviceName - the name of the service to be called
      method - name of the method to call, as provided by the interface this client is generically typed to
      args - method parameters for the invocation
      Returns:
      the representation of the method call
    • convertResult

      protected abstract <R> R convertResult(String response, Type valueType) throws Exception
      Counterpart for convertCall(String, Method, Object[]), parses response and converts back to the method return type declared by the service interface.
      Type Parameters:
      R - the return type of the method invoked via remote procedure call
      Parameters:
      response - the result of the remote procedure call
      valueType - the return type of the method invoked via remote procedure call, e.g., as returned by SomeInterface.class.getMethod("someMethodName").getReturnType()
      Returns:
      the result as a java object of the specified type
      Throws:
      Exception - an exception thrown by the rpc client implementation when processing the request
    • getService

      public <S> S getService(Class<S> serviceInterface, String serviceName)
      Description copied from interface: RpcClient
      Specified by:
      getService in interface RpcClient
      Returns:
      the service implementation
    • callService

      public <S> Future<Void> callService(Class<S> serviceInterface, String serviceName, Consumer<S> serviceEvaluator)
      Description copied from interface: RpcClient
      Specified by:
      callService in interface RpcClient
      Type Parameters:
      S - the interface type of the service to use
      Parameters:
      serviceInterface - the interface of the service to call
      serviceName - the name under which the service interface is registered at the server, see RpcServer.getHandler(String)
      serviceEvaluator - used to invoke the method on the service interface
      Returns:
      an empty future
    • callServiceWithRes

      public <S, R> Future<R> callServiceWithRes(Class<S> serviceInterface, String serviceName, Function<S,R> serviceEvaluator)
      Description copied from interface: RpcClient
      Calls a method on the node model's data service implementation of the given interface registered under the given name and returns the result. Example usage:
       RpcClient m_rpcClient = ...
       Future<List<SomeType>> future =
            m_rpcClient.callServiceWithRes(MyService.class, "MyServiceInstance3", s -> s.getSomeData(someParameter));
       try {
           List<SomeType> results = future.get(3, TimeUnit.SECONDS);
       } catch (TimeoutException timeoutException) {
           ...
       
      Specified by:
      callServiceWithRes in interface RpcClient
      Type Parameters:
      S - the interface type of the service to use
      R - the result type of the invoked method
      Parameters:
      serviceInterface - the interface of the service to call to
      serviceName - the name under which the service interface is registered at the server, see RpcServer.getHandler(String)
      serviceEvaluator - the service evaluator is given an implementation of the node model's data service. It then calls one of the methods on the node model's service interface and returns the result.
      Returns:
      the result
    • isConnectedRemotely

      public boolean isConnectedRemotely()
      Specified by:
      isConnectedRemotely in interface RpcClient
      Returns:
      true if this rpc client is connected to a server, or false if the call are just forwarded to a local implementation.