Class MessageDispatcher
- java.lang.Object
-
- org.jgroups.blocks.MessageDispatcher
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,RequestHandler
,ChannelListener
- Direct Known Subclasses:
RpcDispatcher
public class MessageDispatcher extends java.lang.Object implements RequestHandler, java.io.Closeable, ChannelListener
Provides synchronous and asynchronous message sending with request-response correlation; i.e., matching responses with the original request. It also offers push-style message reception (by internally using the PullPushAdapter).Channels are simple patterns to asynchronously send a receive messages. However, a significant number of communication patterns in group communication require synchronous communication. For example, a sender would like to send a message to the group and wait for all responses. Or another application would like to send a message to the group and wait only until the majority of the receivers have sent a response, or until a timeout occurred. MessageDispatcher offers a combination of the above pattern with other patterns.
Used on top of channel to implement group requests. Client's
handle()
method is called when request is received. Is the equivalent of RpcProtocol on the application instead of protocol level.- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
async_dispatching
protected JChannel
channel
protected RequestCorrelator
corr
protected static GroupRequest
empty_group_request
protected static RspList
empty_rsplist
protected Address
local_addr
protected Log
log
protected java.util.Collection<Address>
members
protected MembershipListener
membership_listener
protected org.jgroups.blocks.MessageDispatcher.ProtocolAdapter
prot_adapter
protected RequestHandler
req_handler
protected RpcStats
rpc_stats
protected StateListener
state_listener
protected boolean
wrap_exceptions
-
Constructor Summary
Constructors Constructor Description MessageDispatcher()
MessageDispatcher(JChannel channel)
MessageDispatcher(JChannel channel, RequestHandler req_handler)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
asyncDispatching()
<X extends MessageDispatcher>
XasyncDispatching(boolean flag)
protected <T> GroupRequest<T>
cast(java.util.Collection<Address> dests, byte[] data, int offset, int length, RequestOptions options, boolean block_for_results)
protected <T> GroupRequest<T>
cast(java.util.Collection<Address> dests, Buffer data, RequestOptions options, boolean block_for_results)
<T> RspList<T>
castMessage(java.util.Collection<Address> dests, byte[] data, int offset, int length, RequestOptions opts)
Sends a message to all members and expects responses from members in dests (if non-null).<T> RspList<T>
castMessage(java.util.Collection<Address> dests, Buffer data, RequestOptions opts)
Sends a message to all members and expects responses from members in dests (if non-null).<T> java.util.concurrent.CompletableFuture<RspList<T>>
castMessageWithFuture(java.util.Collection<Address> dests, Buffer data, RequestOptions opts)
Sends a message to all members and expects responses from members in dests (if non-null).void
channelClosed(JChannel channel)
Channel has been closed notification callbackvoid
channelConnected(JChannel channel)
Channel has been connected notification callbackvoid
channelDisconnected(JChannel channel)
Channel has been disconnected notification callbackvoid
close()
RequestCorrelator
correlator()
<X extends MessageDispatcher>
Xcorrelator(RequestCorrelator c)
protected void
correlatorStarted()
protected static RequestCorrelator
createRequestCorrelator(Protocol transport, RequestHandler handler, Address local_addr)
void
done(long req_id)
boolean
extendedStats()
<X extends MessageDispatcher>
XextendedStats(boolean fl)
boolean
getAsyncDispatching()
JChannel
getChannel()
RequestCorrelator
getCorrelator()
boolean
getExtendedStats()
UpHandler
getProtocolAdapter()
RpcStats
getRpcStats()
boolean
getWrapExceptions()
java.lang.Object
handle(Message msg)
Processes a request synchronously, ie.void
handle(Message request, Response response)
Processes a request asynchronously.protected java.lang.Object
handleUpEvent(Event evt)
protected <X extends MessageDispatcher>
XinstallUpHandler(UpHandler handler, boolean canReplace)
Sets the given UpHandler as the UpHandler for the channel.UpHandler
protocolAdapter()
RpcStats
rpcStats()
<T> T
sendMessage(Address dest, byte[] data, int offset, int length, RequestOptions opts)
Sends a unicast message and - depending on the options - returns a result<T> T
sendMessage(Address dest, Buffer data, RequestOptions opts)
Sends a unicast message and - depending on the options - returns a result<T> java.util.concurrent.CompletableFuture<T>
sendMessageWithFuture(Address dest, byte[] data, int offset, int length, RequestOptions opts)
Sends a unicast message to the target defined by msg.getDest() and returns a future<T> java.util.concurrent.CompletableFuture<T>
sendMessageWithFuture(Address dest, Buffer data, RequestOptions opts)
Sends a unicast message to the target defined by msg.getDest() and returns a future<X extends MessageDispatcher>
XsetAsynDispatching(boolean flag)
<X extends MessageDispatcher>
XsetChannel(JChannel ch)
<X extends MessageDispatcher>
XsetCorrelator(RequestCorrelator c)
<X extends MessageDispatcher>
XsetExtendedStats(boolean fl)
protected <X extends MessageDispatcher>
XsetMembers(java.util.List<Address> new_mbrs)
<X extends MessageDispatcher>
XsetMembershipListener(MembershipListener l)
<X extends MessageDispatcher>
XsetRequestHandler(RequestHandler rh)
<X extends MessageDispatcher>
XsetStateListener(StateListener sl)
<X extends MessageDispatcher>
XsetWrapExceptions(boolean flag)
<X extends MessageDispatcher>
Xstart()
<X extends MessageDispatcher>
Xstop()
protected void
updateStats(java.util.Collection<Address> dests, boolean anycast, boolean sync, long time)
boolean
wrapExceptions()
<X extends MessageDispatcher>
XwrapExceptions(boolean flag)
-
-
-
Field Detail
-
channel
protected JChannel channel
-
corr
protected RequestCorrelator corr
-
membership_listener
protected MembershipListener membership_listener
-
state_listener
protected StateListener state_listener
-
req_handler
protected RequestHandler req_handler
-
async_dispatching
protected boolean async_dispatching
-
wrap_exceptions
protected boolean wrap_exceptions
-
prot_adapter
protected org.jgroups.blocks.MessageDispatcher.ProtocolAdapter prot_adapter
-
members
protected volatile java.util.Collection<Address> members
-
local_addr
protected Address local_addr
-
log
protected final Log log
-
rpc_stats
protected final RpcStats rpc_stats
-
empty_rsplist
protected static final RspList empty_rsplist
-
empty_group_request
protected static final GroupRequest empty_group_request
-
-
Constructor Detail
-
MessageDispatcher
public MessageDispatcher()
-
MessageDispatcher
public MessageDispatcher(JChannel channel)
-
MessageDispatcher
public MessageDispatcher(JChannel channel, RequestHandler req_handler)
-
-
Method Detail
-
getChannel
public JChannel getChannel()
-
getCorrelator
public RequestCorrelator getCorrelator()
-
correlator
public RequestCorrelator correlator()
-
getAsyncDispatching
public boolean getAsyncDispatching()
-
asyncDispatching
public boolean asyncDispatching()
-
getWrapExceptions
public boolean getWrapExceptions()
-
wrapExceptions
public boolean wrapExceptions()
-
getProtocolAdapter
public UpHandler getProtocolAdapter()
-
protocolAdapter
public UpHandler protocolAdapter()
-
getRpcStats
public RpcStats getRpcStats()
-
rpcStats
public RpcStats rpcStats()
-
getExtendedStats
public boolean getExtendedStats()
-
extendedStats
public boolean extendedStats()
-
setExtendedStats
public <X extends MessageDispatcher> X setExtendedStats(boolean fl)
-
extendedStats
public <X extends MessageDispatcher> X extendedStats(boolean fl)
-
setChannel
public <X extends MessageDispatcher> X setChannel(JChannel ch)
-
setCorrelator
public <X extends MessageDispatcher> X setCorrelator(RequestCorrelator c)
-
correlator
public <X extends MessageDispatcher> X correlator(RequestCorrelator c)
-
setMembershipListener
public <X extends MessageDispatcher> X setMembershipListener(MembershipListener l)
-
setStateListener
public <X extends MessageDispatcher> X setStateListener(StateListener sl)
-
setRequestHandler
public <X extends MessageDispatcher> X setRequestHandler(RequestHandler rh)
-
setAsynDispatching
public <X extends MessageDispatcher> X setAsynDispatching(boolean flag)
-
asyncDispatching
public <X extends MessageDispatcher> X asyncDispatching(boolean flag)
-
setWrapExceptions
public <X extends MessageDispatcher> X setWrapExceptions(boolean flag)
-
wrapExceptions
public <X extends MessageDispatcher> X wrapExceptions(boolean flag)
-
setMembers
protected <X extends MessageDispatcher> X setMembers(java.util.List<Address> new_mbrs)
-
start
public <X extends MessageDispatcher> X start()
-
createRequestCorrelator
protected static RequestCorrelator createRequestCorrelator(Protocol transport, RequestHandler handler, Address local_addr)
-
correlatorStarted
protected void correlatorStarted()
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
stop
public <X extends MessageDispatcher> X stop()
-
installUpHandler
protected <X extends MessageDispatcher> X installUpHandler(UpHandler handler, boolean canReplace)
Sets the given UpHandler as the UpHandler for the channel. If the relevant handler is already installed, thecanReplace
controls whether this method replaces it (after logging a WARN) or simply leaveshandler
uninstalled.Passing
false
as thecanReplace
value allows callers to use this method to install defaults without concern about inadvertently overriding- Parameters:
handler
- the UpHandler to installcanReplace
-true
if an existing Channel upHandler can be replaced;false
if this method shouldn't install
-
castMessage
public <T> RspList<T> castMessage(java.util.Collection<Address> dests, byte[] data, int offset, int length, RequestOptions opts) throws java.lang.Exception
Sends a message to all members and expects responses from members in dests (if non-null).- Parameters:
dests
- A list of group members from which to expect responses (if the call is blocking).data
- The bufferoffset
- the offset into datalength
- the number of bytes to sendopts
- A set of options that govern the call. SeeRequestOptions
for details- Returns:
- RspList A list of Rsp elements, or null if the RPC is asynchronous
- Throws:
java.lang.Exception
- If the request cannot be sent- Since:
- 4.0
-
castMessage
public <T> RspList<T> castMessage(java.util.Collection<Address> dests, Buffer data, RequestOptions opts) throws java.lang.Exception
Sends a message to all members and expects responses from members in dests (if non-null).- Parameters:
dests
- A list of group members from which to expect responses (if the call is blocking).data
- The message to be sentopts
- A set of options that govern the call. SeeRequestOptions
for details- Returns:
- RspList A list of Rsp elements, or null if the RPC is asynchronous
- Throws:
java.lang.Exception
- If the request cannot be sent- Since:
- 2.9
-
castMessageWithFuture
public <T> java.util.concurrent.CompletableFuture<RspList<T>> castMessageWithFuture(java.util.Collection<Address> dests, Buffer data, RequestOptions opts) throws java.lang.Exception
Sends a message to all members and expects responses from members in dests (if non-null).- Parameters:
dests
- A list of group members from which to expect responses (if the call is blocking).data
- The message to be sentopts
- A set of options that govern the call. SeeRequestOptions
for details- Returns:
- CompletableFuture
A future from which the results (RspList) can be retrieved, or null if the request was sent asynchronously - Throws:
java.lang.Exception
- If the request cannot be sent
-
cast
protected <T> GroupRequest<T> cast(java.util.Collection<Address> dests, byte[] data, int offset, int length, RequestOptions options, boolean block_for_results) throws java.lang.Exception
- Throws:
java.lang.Exception
-
cast
protected <T> GroupRequest<T> cast(java.util.Collection<Address> dests, Buffer data, RequestOptions options, boolean block_for_results) throws java.lang.Exception
- Throws:
java.lang.Exception
-
done
public void done(long req_id)
-
sendMessage
public <T> T sendMessage(Address dest, byte[] data, int offset, int length, RequestOptions opts) throws java.lang.Exception
Sends a unicast message and - depending on the options - returns a result- Parameters:
dest
- the target to which to send the unicast message. Must not be null.data
- the payload to sendoffset
- the offset at which the data startslength
- the number of bytes to sendopts
- the options to be used- Returns:
- T the result. Null if the call is asynchronous (non-blocking) or if the response is null
- Throws:
java.lang.Exception
- If there was problem sending the request, processing it at the receiver, or processing it at the sender.java.util.concurrent.TimeoutException
- If the call didn't succeed within the timeout defined in options (if set)
-
sendMessage
public <T> T sendMessage(Address dest, Buffer data, RequestOptions opts) throws java.lang.Exception
Sends a unicast message and - depending on the options - returns a result- Parameters:
dest
- the target to which to send the unicast message. Must not be null.data
- the payload to sendopts
- the options to be used- Returns:
- T the result. Null if the call is asynchronous (non-blocking) or if the response is null
- Throws:
java.lang.Exception
- If there was problem sending the request, processing it at the receiver, or processing it at the sender.java.util.concurrent.TimeoutException
- If the call didn't succeed within the timeout defined in options (if set)
-
sendMessageWithFuture
public <T> java.util.concurrent.CompletableFuture<T> sendMessageWithFuture(Address dest, byte[] data, int offset, int length, RequestOptions opts) throws java.lang.Exception
Sends a unicast message to the target defined by msg.getDest() and returns a future- Parameters:
dest
- the target to which to send the unicast message. Must not be null.data
- the payload to sendoffset
- the offset at which the data startslength
- the number of bytes to sendopts
- the options- Returns:
- CompletableFuture
A future from which the result can be fetched, or null if the call was asynchronous - Throws:
java.lang.Exception
- If there was problem sending the request, processing it at the receiver, or processing it at the sender.Future.get()
will throw this exception
-
sendMessageWithFuture
public <T> java.util.concurrent.CompletableFuture<T> sendMessageWithFuture(Address dest, Buffer data, RequestOptions opts) throws java.lang.Exception
Sends a unicast message to the target defined by msg.getDest() and returns a future- Parameters:
dest
- the target to which to send the unicast message. Must not be null.data
- the payload to sendopts
- the options- Returns:
- CompletableFuture
A future from which the result can be fetched, or null if the call was asynchronous - Throws:
java.lang.Exception
- If there was problem sending the request, processing it at the receiver, or processing it at the sender.Future.get()
will throw this exception
-
handle
public java.lang.Object handle(Message msg) throws java.lang.Exception
Description copied from interface:RequestHandler
Processes a request synchronously, ie. on the thread invoking this handler- Specified by:
handle
in interfaceRequestHandler
- Parameters:
msg
- the message containing the request- Returns:
- the object, rceeived as result, or null (void method)
- Throws:
java.lang.Exception
-
handle
public void handle(Message request, Response response) throws java.lang.Exception
Description copied from interface:RequestHandler
Processes a request asynchronously. This could be done (for example) by dispatching this to a thread pool. When done, if a response is needed (e.g. in case of a sync RPC),Response.send(Object,boolean)
should be called.- Specified by:
handle
in interfaceRequestHandler
- Parameters:
request
- The requestresponse
- The response implementation. Contains information needed to send the reply (e.g. a request ID). If no response is required, e.g. because this is an asynchronous RPC, then response will be null.- Throws:
java.lang.Exception
- If an exception is thrown (e.g. in case of an issue submitting the request to a thread pool, the exception will be taken as return value and will be sent as a response. In this case,Response.send(Object,boolean)
must not be called
-
updateStats
protected void updateStats(java.util.Collection<Address> dests, boolean anycast, boolean sync, long time)
-
handleUpEvent
protected java.lang.Object handleUpEvent(Event evt) throws java.lang.Exception
- Throws:
java.lang.Exception
-
channelConnected
public void channelConnected(JChannel channel)
Description copied from interface:ChannelListener
Channel has been connected notification callback- Specified by:
channelConnected
in interfaceChannelListener
- Parameters:
channel
- the channel that has been connected
-
channelDisconnected
public void channelDisconnected(JChannel channel)
Description copied from interface:ChannelListener
Channel has been disconnected notification callback- Specified by:
channelDisconnected
in interfaceChannelListener
- Parameters:
channel
- the disconnected channel
-
channelClosed
public void channelClosed(JChannel channel)
Description copied from interface:ChannelListener
Channel has been closed notification callback- Specified by:
channelClosed
in interfaceChannelListener
- Parameters:
channel
- the closed channel
-
-