public class MessageDispatcher extends Object implements RequestHandler, 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 Channel |
channel |
protected Set<ChannelListener> |
channel_listeners |
protected RequestCorrelator |
corr |
protected boolean |
hardware_multicast_supported |
protected Address |
local_addr |
protected Log |
log |
protected Collection<Address> |
members |
protected MembershipListener |
membership_listener |
protected MessageListener |
msg_listener |
protected org.jgroups.blocks.MessageDispatcher.ProtocolAdapter |
prot_adapter |
protected RequestHandler |
req_handler |
Constructor and Description |
---|
MessageDispatcher() |
MessageDispatcher(Channel channel,
MessageListener l,
MembershipListener l2) |
MessageDispatcher(Channel channel,
MessageListener l,
MembershipListener l2,
RequestHandler req_handler) |
Modifier and Type | Method and Description |
---|---|
void |
addChannelListener(ChannelListener l)
Adds a new channel listener to be notified on the channel's state change.
|
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 options)
Sends a message to the members listed in dests.
|
<T> NotifyingFuture<RspList<T>> |
castMessageWithFuture(Collection<Address> dests,
Message msg,
RequestOptions options)
Sends a message to the members listed in dests.
|
void |
channelClosed(Channel channel)
Channel has been closed notification callback
|
void |
channelConnected(Channel channel)
Channel has been connected notification callback
|
void |
channelDisconnected(Channel channel)
Channel has been disconnected notification callback
|
protected void |
correlatorStarted() |
protected RequestCorrelator |
createRequestCorrelator(Protocol transport,
RequestHandler handler,
Address local_addr) |
void |
done(long req_id) |
Channel |
getChannel() |
MessageListener |
getMessageListener() |
UpHandler |
getProtocolAdapter() |
Object |
handle(Message msg) |
protected void |
installUpHandler(UpHandler handler,
boolean canReplace)
Sets the given UpHandler as the UpHandler for the channel, or, if the
channel already has a Muxer installed as it's UpHandler, sets the given
handler as the Muxer's
default handler . |
void |
removeChannelListener(ChannelListener l) |
<T> T |
sendMessage(Message msg,
RequestOptions opts)
Sends a unicast message and - depending on the options - returns a result
|
<T> NotifyingFuture<T> |
sendMessageWithFuture(Message msg,
RequestOptions options)
Sends a unicast message to the target defined by msg.getDest() and returns a future
|
void |
setChannel(Channel ch) |
protected void |
setMembers(List<Address> new_mbrs)
If this dispatcher is using a user-provided PullPushAdapter, then need to set the members from the adapter
initially since viewChange has most likely already been called in PullPushAdapter.
|
void |
setMembershipListener(MembershipListener l) |
void |
setMessageListener(MessageListener l) |
void |
setRequestHandler(RequestHandler rh) |
void |
start() |
void |
stop() |
protected Channel channel
protected RequestCorrelator corr
protected MessageListener msg_listener
protected MembershipListener membership_listener
protected RequestHandler req_handler
protected org.jgroups.blocks.MessageDispatcher.ProtocolAdapter prot_adapter
protected volatile Collection<Address> members
protected Address local_addr
protected final Log log
protected boolean hardware_multicast_supported
protected final Set<ChannelListener> channel_listeners
public MessageDispatcher()
public MessageDispatcher(Channel channel, MessageListener l, MembershipListener l2)
public MessageDispatcher(Channel channel, MessageListener l, MembershipListener l2, RequestHandler req_handler)
public UpHandler getProtocolAdapter()
protected void setMembers(List<Address> new_mbrs)
public void addChannelListener(ChannelListener l)
public void removeChannelListener(ChannelListener l)
public void start()
protected RequestCorrelator createRequestCorrelator(Protocol transport, RequestHandler handler, Address local_addr)
protected void correlatorStarted()
public void stop()
public final void setMessageListener(MessageListener l)
public MessageListener getMessageListener()
public final void setMembershipListener(MembershipListener l)
public final void setRequestHandler(RequestHandler rh)
public Channel getChannel()
public void setChannel(Channel ch)
protected void installUpHandler(UpHandler handler, boolean canReplace)
default handler
.
If the relevant handler is already installed, the 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 or
Muxer default upHandler can be replaced; false
if this method shouldn't installpublic <T> RspList<T> castMessage(Collection<Address> dests, Message msg, RequestOptions options) throws Exception
dests
- A list of group members to send the message to. The message is sent to all members of the current
group if nullmsg
- The message to be sentoptions
- A set of options that govern the call. See RequestOptions
for detailsException
- If the request cannot be sentpublic <T> NotifyingFuture<RspList<T>> castMessageWithFuture(Collection<Address> dests, Message msg, RequestOptions options) throws Exception
dests
- A list of group members to send the message to. The message is sent to all members of the current
group if nullmsg
- The message to be sentoptions
- 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, Message msg, RequestOptions options, boolean block_for_results) throws Exception
Exception
public void done(long req_id)
public <T> T sendMessage(Message msg, RequestOptions opts) throws Exception
msg
- the message to be sent. The destination needs to be non-nullopts
- 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> NotifyingFuture<T> sendMessageWithFuture(Message msg, RequestOptions options) throws Exception
msg
- The unicast message to be sent. msg.getDest() must not be nulloptions
- Exception
- If there was problem sending the request, processing it at the receiver, or processing
it at the sender. Future.get()
will throw this exceptionTimeoutException
- If the call didn't succeed within the timeout defined in options (if set)public Object handle(Message msg) throws Exception
handle
in interface RequestHandler
Exception
public void channelConnected(Channel channel)
ChannelListener
channelConnected
in interface ChannelListener
channel
- the channel that has been connectedpublic void channelDisconnected(Channel channel)
ChannelListener
channelDisconnected
in interface ChannelListener
channel
- the disconnected channelpublic void channelClosed(Channel channel)
ChannelListener
channelClosed
in interface ChannelListener
channel
- the closed channelCopyright © 2013 JBoss, a division of Red Hat. All Rights Reserved.