Class MessageDispatcher
- All Implemented Interfaces:
Closeable,AutoCloseable,RequestHandler,ChannelListener
- Direct Known Subclasses:
RpcDispatcher
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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected booleanWhen enabled, responses are handled by the common ForkJoinPool (https://issues.redhat.com/browse/JGRP-2644)protected JChannelprotected RequestCorrelatorprotected static final GroupRequestprotected static final RspListprotected Addressprotected final Logprotected Collection<Address> protected MessageDispatcher.ProtocolAdapterprotected Receiverprotected RequestHandlerprotected boolean -
Constructor Summary
ConstructorsConstructorDescriptionMessageDispatcher(JChannel channel) MessageDispatcher(JChannel channel, RequestHandler req_handler) -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> UnicastRequest<T> _sendMessage(Message msg, RequestOptions opts) boolean<X extends MessageDispatcher>
XasyncDispatching(boolean flag) booleanasyncRspHandling(boolean f) protected <T> GroupRequest<T> cast(Collection<Address> dests, Message msg, RequestOptions options, boolean block_for_results) <T> RspList<T> castMessage(Collection<Address> dests, Message msg, 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, Message msg, RequestOptions opts) Sends a message to all members and expects responses from members in dests (if non-null).voidchannelClosed(JChannel channel) Channel has been closed notification callbackvoidchannelDisconnected(JChannel channel) Channel has been disconnected notification callbackvoidclose()<X extends MessageDispatcher>
Xprotected static RequestCorrelatorcreateRequestCorrelator(Protocol transport, RequestHandler handler, Address local_addr) booleanprotected booleangetState(OutputStream out) booleanProcesses a request synchronously, ie.voidProcesses a request asynchronously.protected ObjecthandleUpEvent(Event evt) protected <X extends MessageDispatcher>
XinstallUpHandler(UpHandler handler, boolean canReplace) Sets the given UpHandler as the UpHandler for the channel.rpcStats()<T> TsendMessage(Message msg, RequestOptions opts) Sends a unicast message and - depending on the options - returns a result<T> CompletableFuture<T> sendMessageWithFuture(Message msg, 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>
Xprotected <X extends MessageDispatcher>
XsetMembers(List<Address> new_mbrs) <X extends MessageDispatcher>
X<X extends MessageDispatcher>
Xprotected booleansetState(InputStream in) <X extends MessageDispatcher>
XsetWrapExceptions(boolean flag) <X extends MessageDispatcher>
Xstart()<X extends MessageDispatcher>
Xstop()boolean<X extends MessageDispatcher>
XwrapExceptions(boolean flag) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.jgroups.ChannelListener
channelConnected
-
Field Details
-
channel
-
corr
-
receiver
-
req_handler
-
async_dispatching
protected boolean async_dispatching -
async_rsp_handling
protected boolean async_rsp_handlingWhen enabled, responses are handled by the common ForkJoinPool (https://issues.redhat.com/browse/JGRP-2644) -
wrap_exceptions
protected boolean wrap_exceptions -
prot_adapter
-
members
-
local_addr
-
log
-
empty_rsplist
-
empty_group_request
-
-
Constructor Details
-
MessageDispatcher
public MessageDispatcher() -
MessageDispatcher
-
MessageDispatcher
-
-
Method Details
-
getChannel
-
getCorrelator
-
correlator
-
getAsyncDispatching
public boolean getAsyncDispatching() -
asyncDispatching
public boolean asyncDispatching() -
asyncRspHandling
public boolean asyncRspHandling() -
asyncRspHandling
-
getWrapExceptions
public boolean getWrapExceptions() -
wrapExceptions
public boolean wrapExceptions() -
getProtocolAdapter
-
protocolAdapter
-
rpcStats
-
setChannel
-
setCorrelator
-
correlator
-
setReceiver
-
setRequestHandler
-
setAsynDispatching
-
asyncDispatching
-
setWrapExceptions
-
wrapExceptions
-
setMembers
-
start
-
createRequestCorrelator
protected static RequestCorrelator createRequestCorrelator(Protocol transport, RequestHandler handler, Address local_addr) -
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
stop
-
installUpHandler
Sets the given UpHandler as the UpHandler for the channel. If the relevant handler is already installed, thecanReplacecontrols whether this method replaces it (after logging a WARN) or simply leaveshandleruninstalled.Passing
falseas thecanReplacevalue allows callers to use this method to install defaults without concern about inadvertently overriding- Parameters:
handler- the UpHandler to installcanReplace-trueif an existing Channel upHandler can be replaced;falseif this method shouldn't install
-
castMessage
public <T> RspList<T> castMessage(Collection<Address> dests, Message msg, RequestOptions opts) throws 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).msg- The message to be sentopts- A set of options that govern the call. SeeRequestOptionsfor details- Returns:
- RspList A list of Rsp elements, or null if the RPC is asynchronous
- Throws:
Exception- If the request cannot be sent- Since:
- 2.9
-
castMessageWithFuture
public <T> CompletableFuture<RspList<T>> castMessageWithFuture(Collection<Address> dests, Message msg, RequestOptions opts) throws 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).msg- The message to be sentopts- A set of options that govern the call. SeeRequestOptionsfor details- Returns:
- CompletableFuture A future from which the results (RspList) can be retrieved, or null if the request was sent asynchronously
- Throws:
Exception- If the request cannot be sent
-
cast
protected <T> GroupRequest<T> cast(Collection<Address> dests, Message msg, RequestOptions options, boolean block_for_results) throws Exception - Throws:
Exception
-
sendMessage
Sends a unicast message and - depending on the options - returns a result- Parameters:
msg- 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:
Exception- 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)
-
sendMessageWithFuture
public <T> CompletableFuture<T> sendMessageWithFuture(Message msg, RequestOptions opts) throws Exception Sends a unicast message to the target defined by msg.getDest() and returns a future- Parameters:
msg- 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:
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
Description copied from interface:RequestHandlerProcesses a request synchronously, ie. on the thread invoking this handler- Specified by:
handlein interfaceRequestHandler- Parameters:
msg- the message containing the request- Returns:
- the object, rceeived as result, or null (void method)
- Throws:
Exception
-
handle
Description copied from interface:RequestHandlerProcesses 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:
handlein 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:
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
-
_sendMessage
- Throws:
Exception
-
handleUpEvent
- Throws:
Exception
-
getState
- Throws:
Exception
-
setState
- Throws:
Exception
-
channelDisconnected
Description copied from interface:ChannelListenerChannel has been disconnected notification callback- Specified by:
channelDisconnectedin interfaceChannelListener- Parameters:
channel- the disconnected channel
-
channelClosed
Description copied from interface:ChannelListenerChannel has been closed notification callback- Specified by:
channelClosedin interfaceChannelListener- Parameters:
channel- the closed channel
-