Package org.jgroups.blocks
Class RpcDispatcher
- java.lang.Object
-
- org.jgroups.blocks.MessageDispatcher
-
- org.jgroups.blocks.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 Summary
Fields Modifier and Type Field Description 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 callerprotected MethodInvoker
method_invoker
protected MethodLookup
method_lookup
protected java.lang.Object
server_obj
-
Fields inherited from class org.jgroups.blocks.MessageDispatcher
async_dispatching, channel, corr, empty_group_request, empty_rsplist, local_addr, log, members, membership_listener, prot_adapter, req_handler, rpc_stats, state_listener, wrap_exceptions
-
-
Constructor Summary
Constructors Constructor Description RpcDispatcher()
RpcDispatcher(JChannel channel, java.lang.Object server_obj)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
callRemoteMethod(Address dest, java.lang.String meth, java.lang.Object[] args, java.lang.Class[] types, RequestOptions opts)
Invokes a method in a cluster member and - if blocking - returns the result<T> T
callRemoteMethod(Address dest, MethodCall call, RequestOptions options)
Invokes a method in a cluster member and - if blocking - returns the result<T> RspList<T>
callRemoteMethods(java.util.Collection<Address> dests, java.lang.String method_name, java.lang.Object[] args, java.lang.Class[] types, RequestOptions options)
Invokes a method in all members and expects responses from members contained in dests (or all members if dests is null).<T> RspList<T>
callRemoteMethods(java.util.Collection<Address> dests, MethodCall method_call, RequestOptions opts)
Invokes a method in all members and expects responses from members contained in dests (or all members if dests is null).<T> java.util.concurrent.CompletableFuture<RspList<T>>
callRemoteMethodsWithFuture(java.util.Collection<Address> dests, MethodCall method_call, RequestOptions options)
Invokes a method in all members and expects responses from members contained in dests (or all members if dests is null).<T> java.util.concurrent.CompletableFuture<T>
callRemoteMethodWithFuture(Address dest, MethodCall call, RequestOptions opts)
Invokes a method in a cluster member and - if blocking - returns the resultprotected void
correlatorStarted()
Marshaller
getMarshaller()
MethodInvoker
getMethodInvoker()
MethodLookup
getMethodLookup()
java.lang.Object
getServerObject()
java.lang.Object
handle(Message req)
Message contains MethodCall.protected static MethodCall
methodCallFromBuffer(byte[] buf, int offset, int length, Marshaller marshaller)
protected static Buffer
methodCallToBuffer(MethodCall call, Marshaller marshaller)
RpcDispatcher
setMarshaller(Marshaller m)
RpcDispatcher
setMembershipListener(MembershipListener l)
RpcDispatcher
setMethodInvoker(MethodInvoker mi)
RpcDispatcher
setMethodLookup(MethodLookup method_lookup)
RpcDispatcher
setServerObject(java.lang.Object server_obj)
-
Methods inherited from class org.jgroups.blocks.MessageDispatcher
asyncDispatching, asyncDispatching, cast, cast, castMessage, castMessage, castMessageWithFuture, channelClosed, channelConnected, channelDisconnected, close, correlator, correlator, createRequestCorrelator, done, extendedStats, extendedStats, getAsyncDispatching, getChannel, getCorrelator, getExtendedStats, getProtocolAdapter, getRpcStats, getWrapExceptions, handle, handleUpEvent, installUpHandler, protocolAdapter, rpcStats, sendMessage, sendMessage, sendMessageWithFuture, sendMessageWithFuture, setAsynDispatching, setChannel, setCorrelator, setExtendedStats, setMembers, setRequestHandler, setStateListener, setWrapExceptions, start, stop, updateStats, wrapExceptions, wrapExceptions
-
-
-
-
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
-
method_lookup
protected MethodLookup method_lookup
-
method_invoker
protected MethodInvoker method_invoker
-
-
Constructor Detail
-
RpcDispatcher
public RpcDispatcher()
-
RpcDispatcher
public RpcDispatcher(JChannel channel, java.lang.Object server_obj)
-
-
Method Detail
-
getMarshaller
public Marshaller getMarshaller()
-
setMarshaller
public RpcDispatcher setMarshaller(Marshaller m)
-
getServerObject
public java.lang.Object getServerObject()
-
setServerObject
public RpcDispatcher setServerObject(java.lang.Object server_obj)
-
setMembershipListener
public RpcDispatcher setMembershipListener(MembershipListener l)
- Overrides:
setMembershipListener
in classMessageDispatcher
-
getMethodLookup
public MethodLookup getMethodLookup()
-
setMethodLookup
public RpcDispatcher setMethodLookup(MethodLookup method_lookup)
-
getMethodInvoker
public MethodInvoker getMethodInvoker()
-
setMethodInvoker
public RpcDispatcher setMethodInvoker(MethodInvoker mi)
-
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 membersmethod_name
- The name of the target methodargs
- The arguments to be passedtypes
- The types of the argumentsoptions
- 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 membersmethod_call
- The method (plus args) to be invokedopts
- 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 membersmethod_call
- The method (plus args) to be invokedoptions
- 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 methodmeth
- The name of the methodargs
- The argumentstypes
- The types of the argumentsopts
- 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 methodcall
- The call to be invoked, including method are argumentsoptions
- 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 methodcall
- The call to be invoked, including method are argumentsopts
- 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 interfaceRequestHandler
- Overrides:
handle
in classMessageDispatcher
- 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
-
correlatorStarted
protected void correlatorStarted()
- Overrides:
correlatorStarted
in classMessageDispatcher
-
-