public class MessageDispatcher extends Object implements RequestHandler, Closeable, ChannelListener
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.
Modifier and Type | Field and 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 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 and Description |
---|
MessageDispatcher() |
MessageDispatcher(JChannel channel) |
MessageDispatcher(JChannel channel,
RequestHandler req_handler) |
Modifier and Type | Method and Description |
---|---|
boolean |
asyncDispatching() |
<X extends MessageDispatcher> |
asyncDispatching(boolean flag) |
protected <T> GroupRequest<T> |
cast(Collection<Address> dests,
Buffer data,
RequestOptions options,
boolean block_for_results) |
protected <T> GroupRequest<T> |
cast(Collection<Address> dests,
byte[] data,
int offset,
int length,
RequestOptions options,
boolean block_for_results) |
<T> RspList<T> |
castMessage(Collection<Address> dests,
Buffer data,
RequestOptions opts)
Sends a message to all members and expects responses from members in dests (if non-null).
|
<T> RspList<T> |
castMessage(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> CompletableFuture<RspList<T>> |
castMessageWithFuture(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 callback
|
void |
channelConnected(JChannel channel)
Channel has been connected notification callback
|
void |
channelDisconnected(JChannel channel)
Channel has been disconnected notification callback
|
void |
close() |
RequestCorrelator |
correlator() |
<X extends MessageDispatcher> |
correlator(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> |
extendedStats(boolean fl) |
boolean |
getAsyncDispatching() |
JChannel |
getChannel() |
RequestCorrelator |
getCorrelator() |
boolean |
getExtendedStats() |
UpHandler |
getProtocolAdapter() |
RpcStats |
getRpcStats() |
boolean |
getWrapExceptions() |
Object |
handle(Message msg)
Processes a request synchronously, ie.
|
void |
handle(Message request,
Response response)
Processes a request asynchronously.
|
protected Object |
handleUpEvent(Event evt) |
protected <X extends MessageDispatcher> |
installUpHandler(UpHandler handler,
boolean canReplace)
Sets the given UpHandler as the UpHandler for the channel.
|
UpHandler |
protocolAdapter() |
RpcStats |
rpcStats() |
<T> T |
sendMessage(Address dest,
Buffer data,
RequestOptions opts)
Sends a unicast message and - depending on the options - returns a result
|
<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> CompletableFuture<T> |
sendMessageWithFuture(Address dest,
Buffer data,
RequestOptions opts)
Sends a unicast message to the target defined by msg.getDest() and returns a future
|
<T> 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
|
<X extends MessageDispatcher> |
setAsynDispatching(boolean flag) |
<X extends MessageDispatcher> |
setChannel(JChannel ch) |
<X extends MessageDispatcher> |
setCorrelator(RequestCorrelator c) |
<X extends MessageDispatcher> |
setExtendedStats(boolean fl) |
protected <X extends MessageDispatcher> |
setMembers(List<Address> new_mbrs) |
<X extends MessageDispatcher> |
setMembershipListener(MembershipListener l) |
<X extends MessageDispatcher> |
setRequestHandler(RequestHandler rh) |
<X extends MessageDispatcher> |
setStateListener(StateListener sl) |
<X extends MessageDispatcher> |
setWrapExceptions(boolean flag) |
<X extends MessageDispatcher> |
start() |
<X extends MessageDispatcher> |
stop() |
protected void |
updateStats(Collection<Address> dests,
boolean anycast,
boolean sync,
long time) |
boolean |
wrapExceptions() |
<X extends MessageDispatcher> |
wrapExceptions(boolean flag) |
protected JChannel channel
protected RequestCorrelator corr
protected MembershipListener membership_listener
protected StateListener state_listener
protected RequestHandler req_handler
protected boolean async_dispatching
protected boolean wrap_exceptions
protected org.jgroups.blocks.MessageDispatcher.ProtocolAdapter prot_adapter
protected volatile Collection<Address> members
protected Address local_addr
protected final Log log
protected final RpcStats rpc_stats
protected static final RspList empty_rsplist
protected static final GroupRequest empty_group_request
public MessageDispatcher()
public MessageDispatcher(JChannel channel)
public MessageDispatcher(JChannel channel, RequestHandler req_handler)
public JChannel getChannel()
public RequestCorrelator getCorrelator()
public RequestCorrelator correlator()
public boolean getAsyncDispatching()
public boolean asyncDispatching()
public boolean getWrapExceptions()
public boolean wrapExceptions()
public UpHandler getProtocolAdapter()
public UpHandler protocolAdapter()
public RpcStats getRpcStats()
public RpcStats rpcStats()
public boolean getExtendedStats()
public boolean extendedStats()
public <X extends MessageDispatcher> X setExtendedStats(boolean fl)
public <X extends MessageDispatcher> X extendedStats(boolean fl)
public <X extends MessageDispatcher> X setChannel(JChannel ch)
public <X extends MessageDispatcher> X setCorrelator(RequestCorrelator c)
public <X extends MessageDispatcher> X correlator(RequestCorrelator c)
public <X extends MessageDispatcher> X setMembershipListener(MembershipListener l)
public <X extends MessageDispatcher> X setStateListener(StateListener sl)
public <X extends MessageDispatcher> X setRequestHandler(RequestHandler rh)
public <X extends MessageDispatcher> X setAsynDispatching(boolean flag)
public <X extends MessageDispatcher> X asyncDispatching(boolean flag)
public <X extends MessageDispatcher> X setWrapExceptions(boolean flag)
public <X extends MessageDispatcher> X wrapExceptions(boolean flag)
protected <X extends MessageDispatcher> X setMembers(List<Address> new_mbrs)
public <X extends MessageDispatcher> X start()
protected static RequestCorrelator createRequestCorrelator(Protocol transport, RequestHandler handler, Address local_addr)
protected void correlatorStarted()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public <X extends MessageDispatcher> X stop()
protected <X extends MessageDispatcher> X installUpHandler(UpHandler handler, boolean canReplace)
canReplace
controls whether this method replaces it (after logging a WARN) or simply
leaves handler
uninstalled.
Passing false
as the canReplace
value allows callers to use this method to install defaults
without concern about inadvertently overriding
handler
- the UpHandler to installcanReplace
- true
if an existing Channel upHandler can be replaced; false
if this method shouldn't installpublic <T> RspList<T> castMessage(Collection<Address> dests, byte[] data, int offset, int length, RequestOptions opts) throws Exception
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. See RequestOptions
for detailsException
- If the request cannot be sentpublic <T> RspList<T> castMessage(Collection<Address> dests, Buffer data, RequestOptions opts) throws Exception
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. See RequestOptions
for detailsException
- If the request cannot be sentpublic <T> CompletableFuture<RspList<T>> castMessageWithFuture(Collection<Address> dests, Buffer data, RequestOptions opts) throws Exception
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. See RequestOptions
for detailsException
- If the request cannot be sentprotected <T> GroupRequest<T> cast(Collection<Address> dests, byte[] data, int offset, int length, RequestOptions options, boolean block_for_results) throws Exception
Exception
protected <T> GroupRequest<T> cast(Collection<Address> dests, Buffer data, RequestOptions options, boolean block_for_results) throws Exception
Exception
public void done(long req_id)
public <T> T sendMessage(Address dest, byte[] data, int offset, int length, RequestOptions opts) throws Exception
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 usedException
- If there was problem sending the request, processing it at the receiver, or processing
it at the sender.TimeoutException
- If the call didn't succeed within the timeout defined in options (if set)public <T> T sendMessage(Address dest, Buffer data, RequestOptions opts) throws Exception
dest
- the target to which to send the unicast message. Must not be null.data
- the payload to sendopts
- the options to be usedException
- If there was problem sending the request, processing it at the receiver, or processing
it at the sender.TimeoutException
- If the call didn't succeed within the timeout defined in options (if set)public <T> CompletableFuture<T> sendMessageWithFuture(Address dest, byte[] data, int offset, int length, RequestOptions opts) throws Exception
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 optionsException
- If there was problem sending the request, processing it at the receiver, or processing
it at the sender. Future.get()
will throw this exceptionpublic <T> CompletableFuture<T> sendMessageWithFuture(Address dest, Buffer data, RequestOptions opts) throws Exception
dest
- the target to which to send the unicast message. Must not be null.data
- the payload to sendopts
- the optionsException
- If there was problem sending the request, processing it at the receiver, or processing
it at the sender. Future.get()
will throw this exceptionpublic Object handle(Message msg) throws Exception
RequestHandler
handle
in interface RequestHandler
msg
- the message containing the requestException
public void handle(Message request, Response response) throws Exception
RequestHandler
Response.send(Object,boolean)
should
be called.handle
in interface RequestHandler
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.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 calledprotected void updateStats(Collection<Address> dests, boolean anycast, boolean sync, long time)
public void channelConnected(JChannel channel)
ChannelListener
channelConnected
in interface ChannelListener
channel
- the channel that has been connectedpublic void channelDisconnected(JChannel channel)
ChannelListener
channelDisconnected
in interface ChannelListener
channel
- the disconnected channelpublic void channelClosed(JChannel channel)
ChannelListener
channelClosed
in interface ChannelListener
channel
- the closed channelCopyright © 2018 JBoss, a division of Red Hat. All rights reserved.