Class RpcDispatcher

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, RequestHandler, ChannelListener

    public class RpcDispatcher
    extends MessageDispatcher
    This class allows a programmer to invoke remote methods in all (or single) group members and optionally wait for the return value(s).

    An application will typically create a channel and layer the RpcDispatcher building block on top of it, which allows it to dispatch remote methods (client role) and at the same time be called by other members (server role).

    This class is derived from MessageDispatcher.

    Author:
    Bela Ban
    • Field Detail

      • server_obj

        protected java.lang.Object server_obj
      • marshaller

        protected Marshaller marshaller
        Marshaller to marshall requests at the caller, unmarshal requests at the receiver(s), marshall responses at the receivers and unmarshall responses at the caller
    • Constructor Detail

      • RpcDispatcher

        public RpcDispatcher()
      • RpcDispatcher

        public RpcDispatcher​(JChannel channel,
                             java.lang.Object server_obj)
    • Method Detail

      • getMarshaller

        public Marshaller getMarshaller()
      • getServerObject

        public java.lang.Object getServerObject()
      • setServerObject

        public RpcDispatcher setServerObject​(java.lang.Object server_obj)
      • callRemoteMethods

        public <T> RspList<T> callRemoteMethods​(java.util.Collection<Address> dests,
                                                java.lang.String method_name,
                                                java.lang.Object[] args,
                                                java.lang.Class[] types,
                                                RequestOptions options)
                                         throws java.lang.Exception
        Invokes a method in all members and expects responses from members contained in dests (or all members if dests is null).
        Parameters:
        dests - A list of addresses. If null, we'll wait for responses from all cluster members
        method_name - The name of the target method
        args - The arguments to be passed
        types - The types of the arguments
        options - A collection of call options, e.g. sync versus async, timeout etc
        Returns:
        RspList A response list with results, one for each member in dests, or null if the RPC is asynchronous
        Throws:
        java.lang.Exception - If the sending of the message threw an exception. Note that no exception will be thrown if any of the target members threw an exception, but this exception will be in the Rsp object for the particular member in the RspList
      • callRemoteMethods

        public <T> RspList<T> callRemoteMethods​(java.util.Collection<Address> dests,
                                                MethodCall method_call,
                                                RequestOptions opts)
                                         throws java.lang.Exception
        Invokes a method in all members and expects responses from members contained in dests (or all members if dests is null).
        Parameters:
        dests - A list of addresses. If null, we'll wait for responses from all cluster members
        method_call - The method (plus args) to be invoked
        opts - A collection of call options, e.g. sync versus async, timeout etc
        Returns:
        RspList A list of return values and flags (suspected, not received) per member, or null if the RPC is asynchronous
        Throws:
        java.lang.Exception - If the sending of the message threw an exception. Note that no exception will be thrown if any of the target members threw an exception, but this exception will be in the Rsp object for the particular member in the RspList
        Since:
        2.9
      • callRemoteMethodsWithFuture

        public <T> java.util.concurrent.CompletableFuture<RspList<T>> callRemoteMethodsWithFuture​(java.util.Collection<Address> dests,
                                                                                                  MethodCall method_call,
                                                                                                  RequestOptions options)
                                                                                           throws java.lang.Exception
        Invokes a method in all members and expects responses from members contained in dests (or all members if dests is null).
        Parameters:
        dests - A list of addresses. If null, we'll wait for responses from all cluster members
        method_call - The method (plus args) to be invoked
        options - A collection of call options, e.g. sync versus async, timeout etc
        Returns:
        CompletableFuture A future from which the results can be fetched, or null if the RPC is asynchronous
        Throws:
        java.lang.Exception - If the sending of the message threw an exception. Note that no exception will be thrown if any of the target members threw an exception; such an exception will be in the Rsp element for the particular member in the RspList
      • callRemoteMethod

        public <T> T callRemoteMethod​(Address dest,
                                      java.lang.String meth,
                                      java.lang.Object[] args,
                                      java.lang.Class[] types,
                                      RequestOptions opts)
                               throws java.lang.Exception
        Invokes a method in a cluster member and - if blocking - returns the result
        Parameters:
        dest - The target member on which to invoke the method
        meth - The name of the method
        args - The arguments
        types - The types of the arguments
        opts - The options (e.g. blocking, timeout etc)
        Returns:
        The result. Null if the call is asynchronous (non-blocking) or if the method returns void
        Throws:
        java.lang.Exception - Thrown if the method invocation threw an exception, either at the caller or the callee
      • callRemoteMethod

        public <T> T callRemoteMethod​(Address dest,
                                      MethodCall call,
                                      RequestOptions options)
                               throws java.lang.Exception
        Invokes a method in a cluster member and - if blocking - returns the result
        Parameters:
        dest - The target member on which to invoke the method
        call - The call to be invoked, including method are arguments
        options - The options (e.g. blocking, timeout etc)
        Returns:
        The result. Null if the call is asynchronous (non-blocking) or if the method returns void
        Throws:
        java.lang.Exception - Thrown if the method invocation threw an exception, either at the caller or the callee
      • callRemoteMethodWithFuture

        public <T> java.util.concurrent.CompletableFuture<T> callRemoteMethodWithFuture​(Address dest,
                                                                                        MethodCall call,
                                                                                        RequestOptions opts)
                                                                                 throws java.lang.Exception
        Invokes a method in a cluster member and - if blocking - returns the result
        Parameters:
        dest - The target member on which to invoke the method
        call - The call to be invoked, including method are arguments
        opts - The options (e.g. blocking, timeout etc)
        Returns:
        A future from which the result can be fetched. If the callee threw an invocation, an ExecutionException will be thrown on calling Future.get(). If the invocation was asynchronous, null will be returned.
        Throws:
        java.lang.Exception - Thrown if the method invocation threw an exception
      • handle

        public java.lang.Object handle​(Message req)
                                throws java.lang.Exception
        Message contains MethodCall. Execute it against *this* object and return result. Use MethodCall.invoke() to do this. Return result.
        Specified by:
        handle in interface RequestHandler
        Overrides:
        handle in class MessageDispatcher
        Parameters:
        req - the message containing the request
        Returns:
        the object, rceeived as result, or null (void method)
        Throws:
        java.lang.Exception
      • methodCallToBuffer

        protected static Buffer methodCallToBuffer​(MethodCall call,
                                                   Marshaller marshaller)
                                            throws java.lang.Exception
        Throws:
        java.lang.Exception
      • methodCallFromBuffer

        protected static MethodCall methodCallFromBuffer​(byte[] buf,
                                                         int offset,
                                                         int length,
                                                         Marshaller marshaller)
                                                  throws java.lang.Exception
        Throws:
        java.lang.Exception