public class JGroupsConnector extends Object implements CommandRouter, org.jgroups.Receiver, CommandBusConnector
DistributedCommandBus
based on JGroups that acts both as the discovery and routing
mechanism (implementing CommandRouter
) as well as the Connector between nodes
(implementing CommandBusConnector
).
After configuring the Connector, it needs to connect()
, before it can start dispatching messages to other
nodes. For a clean shutdown, connectors should disconnect()
to notify other nodes of the node leaving.
Constructor and Description |
---|
JGroupsConnector(CommandBus localSegment,
org.jgroups.JChannel channel,
String clusterName,
Serializer serializer)
Initialize the connector using the given
localSegment to handle commands on the local node, and the given
channel to connect between nodes. |
JGroupsConnector(CommandBus localSegment,
org.jgroups.JChannel channel,
String clusterName,
Serializer serializer,
RoutingStrategy routingStrategy)
Initialize the connector using the given
localSegment to handle commands on the local node, and the given
channel to connect between nodes. |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitJoined()
this method blocks until this member has successfully joined the other members, until the thread is
interrupted, or when joining has failed.
|
boolean |
awaitJoined(long timeout,
TimeUnit timeUnit)
this method blocks until this member has successfully joined the other members, until the thread is
interrupted, when the given number of milliseconds have passed, or when joining has failed.
|
void |
block() |
protected void |
broadCastMembership(int updateVersion,
boolean expectReply)
Send the local membership details (load factor and supported Command types) to other member nodes of this
cluster.
|
void |
connect()
Connects this Node to the cluster and shares membership details about this node with the other nodes in the
cluster.
|
void |
disconnect()
Disconnects from the Cluster, preventing any Commands from being routed to this node.
|
Optional<Member> |
findDestination(CommandMessage<?> message)
Returns the member instance to which the given
message should be routed. |
protected ConsistentHash |
getConsistentHash()
Returns the ConsistentHash instance that describes the current membership status.
|
String |
getNodeName()
Returns the name of the current node, as it is known to the Cluster.
|
void |
getState(OutputStream ostream) |
void |
receive(org.jgroups.Message msg) |
protected org.jgroups.Address |
resolveAddress(Member destination)
Resolve the JGroups Address of the given
Member . |
<C> void |
send(Member destination,
CommandMessage<? extends C> command)
Sends the given
command to the node assigned to handle messages with the given
routingKey . |
<C,R> void |
send(Member destination,
CommandMessage<C> command,
CommandCallback<? super C,R> callback)
Sends the given
command to the node assigned to handle messages with the given
routingKey . |
void |
setState(InputStream istream) |
Registration |
subscribe(String commandName,
MessageHandler<? super CommandMessage<?>> handler)
Subscribes a command message handler for commands with given
commandName . |
void |
suspect(org.jgroups.Address suspected_mbr) |
void |
unblock() |
void |
updateMembership(int loadFactor,
Predicate<? super CommandMessage<?>> commandFilter)
Updates the load factor and capabilities of this member representing the current endpoint if the implementation
allows memberships to be updated dynamically.
|
void |
viewAccepted(org.jgroups.View view) |
public JGroupsConnector(CommandBus localSegment, org.jgroups.JChannel channel, String clusterName, Serializer serializer)
localSegment
to handle commands on the local node, and the given
channel
to connect between nodes. A unique clusterName
should be chose to define which nodes can
connect to each other. The given serializer
is used to serialize messages when they are sent between
nodes.
Commands are routed based on the TargetAggregateIdentifier
localSegment
- The CommandBus implementation that handles the local Commandschannel
- The JGroups Channel used to communicate between nodesclusterName
- The name of the Clusterserializer
- The serializer to serialize Command Messages withpublic JGroupsConnector(CommandBus localSegment, org.jgroups.JChannel channel, String clusterName, Serializer serializer, RoutingStrategy routingStrategy)
localSegment
to handle commands on the local node, and the given
channel
to connect between nodes. A unique clusterName
should be chose to define which nodes can
connect to each other. The given serializer
is used to serialize messages when they are sent between
nodes. The routingStrategy
is used to define the key based on which Command Messages are routed to their
respective handler nodes.localSegment
- The CommandBus implementation that handles the local Commandschannel
- The JGroups Channel used to communicate between nodesclusterName
- The name of the Clusterserializer
- The serializer to serialize Command Messages withroutingStrategy
- The strategy for routing Commands to a Nodepublic void updateMembership(int loadFactor, Predicate<? super CommandMessage<?>> commandFilter)
CommandRouter
updateMembership
in interface CommandRouter
loadFactor
- the new load factor of the member for this endpointcommandFilter
- the new capabilities of the member for this endpointprotected void broadCastMembership(int updateVersion, boolean expectReply) throws ServiceRegistryException
updateVersion
- The version for the update to be send with the membership informationexpectReply
- ServiceRegistryException
- when an exception occurs sending membership details to other nodespublic void connect() throws Exception
The Join messages have been sent, but may not have been processed yet when the method returns. Before sending
messages via this connector, await for the joining process to be completed (see and
{@link #awaitJoined(long, TimeUnit)}
.
Exception
- when an error occurs connecting or communicating with the clusterpublic void disconnect()
public void getState(OutputStream ostream) throws Exception
getState
in interface org.jgroups.StateListener
Exception
public void setState(InputStream istream) throws Exception
setState
in interface org.jgroups.StateListener
Exception
public void viewAccepted(org.jgroups.View view)
viewAccepted
in interface org.jgroups.MembershipListener
public void suspect(org.jgroups.Address suspected_mbr)
suspect
in interface org.jgroups.MembershipListener
public void block()
block
in interface org.jgroups.MembershipListener
public void unblock()
unblock
in interface org.jgroups.MembershipListener
public void receive(org.jgroups.Message msg)
receive
in interface org.jgroups.MessageListener
public boolean awaitJoined() throws InterruptedException
true
if the member successfully joined, otherwise false
.InterruptedException
- when the thread is interrupted while joiningpublic boolean awaitJoined(long timeout, TimeUnit timeUnit) throws InterruptedException
timeout
- The amount of time to wait for the connection to completetimeUnit
- The time unit of the timeouttrue
if the member successfully joined, otherwise false
.InterruptedException
- when the thread is interrupted while joiningpublic String getNodeName()
protected ConsistentHash getConsistentHash()
ConsistentHash
is
used to decide which node is to be sent a Message.public <C> void send(Member destination, CommandMessage<? extends C> command) throws Exception
CommandBusConnector
command
to the node assigned to handle messages with the given
routingKey
. The sender does not expect a reply.
If this method throws an exception, the sender is guaranteed that the destination of the command did not receive
it. If the method returns normally, the actual implementation of the connector defines the delivery guarantees.
Connectors route the commands based on the given routingKey
. Using the same routingKey
will result in the command being sent to the same member. Each message must be sent to exactly one
member.send
in interface CommandBusConnector
destination
- The member of the network to send the message tocommand
- The command to send to the (remote) memberException
- when an error occurs before or during the sending of the messagepublic <C,R> void send(Member destination, CommandMessage<C> command, CommandCallback<? super C,R> callback) throws Exception
CommandBusConnector
command
to the node assigned to handle messages with the given
routingKey
. The sender expect a reply, and will be notified of the result in the given
callback
.
If this method throws an exception, the sender is guaranteed that the destination of the command did not receive
it. If the method returns normally, the actual implementation of the connector defines the delivery guarantees.
Implementations should always invoke the callback with an outcome.
If a member's connection was lost, and the result of the command is unclear, the CommandCallback.onFailure(org.axonframework.commandhandling.CommandMessage, Throwable)
method is invoked with a
RemoteCommandHandlingException
describing
the failed connection. A client may choose to resend a command.
Connectors route the commands based on the given routingKey
. Using the same routingKey
will result in the command being sent to the same member.send
in interface CommandBusConnector
C
- The type of object expected as commandR
- The type of object expected as result of the commanddestination
- The member of the network to send the message tocommand
- The command to send to the (remote) membercallback
- The callbackException
- when an error occurs before or during the sending of the messagepublic Registration subscribe(String commandName, MessageHandler<? super CommandMessage<?>> handler)
CommandBusConnector
commandName
.subscribe
in interface CommandBusConnector
commandName
- the command name. Usually this equals the fully qualified class name of the command.handler
- the handler to subscribeprotected org.jgroups.Address resolveAddress(Member destination)
Member
.destination
- The node of which to solve the AddressCommandBusConnectorCommunicationException
- when an error occurs resolving the adresspublic Optional<Member> findDestination(CommandMessage<?> message)
CommandRouter
message
should be routed. If no suitable member could be
found an empty Optional is returned.findDestination
in interface CommandRouter
message
- the command message to find a member forCopyright © 2010–2017. All rights reserved.