Package org.jgroups.blocks
Class ReplicatedTree
- java.lang.Object
-
- org.jgroups.blocks.ReplicatedTree
-
- All Implemented Interfaces:
java.lang.Runnable,MembershipListener,MessageListener
public class ReplicatedTree extends java.lang.Object implements java.lang.Runnable, MessageListener, MembershipListener
A tree-like structure that is replicated across several members. Updates will be multicast to all group members reliably and in the same order.- Author:
- Bela Ban Jan 17 2002, Alfonso Olias-Sanz
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classReplicatedTree.Nodestatic interfaceReplicatedTree.ReplicatedTreeListener
-
Constructor Summary
Constructors Constructor Description ReplicatedTree()ReplicatedTree(java.lang.String groupname, java.lang.String props, long state_fetch_timeout)Creates a channel with the given properties.ReplicatedTree(java.lang.String groupname, java.lang.String props, long state_fetch_timeout, boolean jmx)ReplicatedTree(JChannel channel)Expects an already connected channel.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void_put(java.lang.String fqn, java.lang.String key, java.lang.Object value)void_put(java.lang.String fqn, java.util.HashMap data)void_remove(java.lang.String fqn)void_remove(java.lang.String fqn, java.lang.String key)void_removeData(java.lang.String fqn)voidaddReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)voidblock()Block sending and receiving of messages until viewAccepted() is calledbooleanexists(java.lang.String fqn)Checks whether a given node exists in the treevoidfetchState(long timeout)Fetch the group state from the current coordinator.java.lang.Objectget(java.lang.String fqn, java.lang.String key)Finds a node given its name and returns the value associated with a given key in itsdatamap.ChannelgetChannel()Returns the Channel the DistributedTree is connected tojava.util.SetgetChildrenNames(java.lang.String fqn)Returns all children of a given nodeintgetGroupMembersNumber()Returns the number of current members joined to the groupjava.lang.StringgetGroupName()Returns the name of the group that the DistributedTree is connected tojava.util.SetgetKeys(java.lang.String fqn)Gets the keys of thedatamap.AddressgetLocalAddress()java.util.VectorgetMembers()byte[]getState()Return a copy of the current cache (tree)static voidmain(java.lang.String[] args)java.lang.Stringprint(java.lang.String fqn)Prints a representation of the node defined byfqn.voidput(java.lang.String fqn, java.lang.String key, java.lang.Object value)Adds a key and value to a given node.voidput(java.lang.String fqn, java.util.HashMap data)Adds a new node to the tree and sets its data.voidreceive(Message msg)Callback.voidremove(java.lang.String fqn)Removes the node from the tree.voidremove(java.lang.String fqn, java.lang.String key)Removeskeyfrom the node's hashmapvoidremoveReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)voidrun()Request handler threadvoidsetRemoteCalls(boolean flag)voidsetRootNode(ReplicatedTree.Node n)voidsetState(byte[] new_state)Set the cache (tree) to this valuevoidstart()voidstop()voidsuspect(Address suspected_mbr)Called when a member is suspectedjava.lang.StringtoString()voidviewAccepted(View new_view)Called when a change in membership has occurred.
-
-
-
Field Detail
-
SEPARATOR
public static final java.lang.String SEPARATOR
- See Also:
- Constant Field Values
-
log
protected final org.apache.commons.logging.Log log
-
-
Constructor Detail
-
ReplicatedTree
public ReplicatedTree(java.lang.String groupname, java.lang.String props, long state_fetch_timeout) throws java.lang.ExceptionCreates a channel with the given properties. Connects to the channel, then creates a PullPushAdapter and starts it- Throws:
java.lang.Exception
-
ReplicatedTree
public ReplicatedTree(java.lang.String groupname, java.lang.String props, long state_fetch_timeout, boolean jmx) throws java.lang.Exception- Throws:
java.lang.Exception
-
ReplicatedTree
public ReplicatedTree()
-
ReplicatedTree
public ReplicatedTree(JChannel channel) throws java.lang.Exception
Expects an already connected channel. Creates a PullPushAdapter and starts it- Throws:
java.lang.Exception
-
-
Method Detail
-
setRemoteCalls
public void setRemoteCalls(boolean flag)
-
setRootNode
public void setRootNode(ReplicatedTree.Node n)
-
getLocalAddress
public Address getLocalAddress()
-
getMembers
public java.util.Vector getMembers()
-
fetchState
public void fetchState(long timeout) throws ChannelClosedException, ChannelNotConnectedExceptionFetch the group state from the current coordinator. If successful, this will trigger setState().
-
addReplicatedTreeListener
public void addReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)
-
removeReplicatedTreeListener
public void removeReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)
-
start
public final void start() throws java.lang.Exception- Throws:
java.lang.Exception
-
stop
public void stop()
-
put
public void put(java.lang.String fqn, java.util.HashMap data)Adds a new node to the tree and sets its data. If the node doesn not yet exist, it will be created. Also, parent nodes will be created if not existent. If the node already has data, then the new data will override the old one. If the node already existed, a nodeModified() notification will be generated. Otherwise a nodeCreated() motification will be emitted.- Parameters:
fqn- The fully qualified name of the new nodedata- The new data. May be null if no data should be set in the node.
-
put
public void put(java.lang.String fqn, java.lang.String key, java.lang.Object value)Adds a key and value to a given node. If the node doesn't exist, it will be created. If the node already existed, a nodeModified() notification will be generated. Otherwise a nodeCreated() motification will be emitted.- Parameters:
fqn- The fully qualified name of the nodekey- The keyvalue- The value
-
remove
public void remove(java.lang.String fqn)
Removes the node from the tree.- Parameters:
fqn- The fully qualified name of the node.
-
remove
public void remove(java.lang.String fqn, java.lang.String key)Removeskeyfrom the node's hashmap- Parameters:
fqn- The fullly qualified name of the nodekey- The key to be removed
-
exists
public boolean exists(java.lang.String fqn)
Checks whether a given node exists in the tree- Parameters:
fqn- The fully qualified name of the node- Returns:
- boolean Whether or not the node exists
-
getKeys
public java.util.Set getKeys(java.lang.String fqn)
Gets the keys of thedatamap. Returns all keys as Strings. Returns null if node does not exist.- Parameters:
fqn- The fully qualified name of the node- Returns:
- Set A set of keys (as Strings)
-
get
public java.lang.Object get(java.lang.String fqn, java.lang.String key)Finds a node given its name and returns the value associated with a given key in itsdatamap. Returns null if the node was not found in the tree or the key was not found in the hashmap.- Parameters:
fqn- The fully qualified name of the node.key- The key.
-
print
public java.lang.String print(java.lang.String fqn)
Prints a representation of the node defined byfqn. Output includes name, fqn and data.
-
getChildrenNames
public java.util.Set getChildrenNames(java.lang.String fqn)
Returns all children of a given node- Parameters:
fqn- The fully qualified name of the node- Returns:
- Set A list of child names (as Strings)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getGroupName
public java.lang.String getGroupName()
Returns the name of the group that the DistributedTree is connected to- Returns:
- String
-
getChannel
public Channel getChannel()
Returns the Channel the DistributedTree is connected to- Returns:
- Channel
-
getGroupMembersNumber
public int getGroupMembersNumber()
Returns the number of current members joined to the group- Returns:
- int
-
_put
public void _put(java.lang.String fqn, java.util.HashMap data)
-
_put
public void _put(java.lang.String fqn, java.lang.String key, java.lang.Object value)
-
_remove
public void _remove(java.lang.String fqn)
-
_remove
public void _remove(java.lang.String fqn, java.lang.String key)
-
_removeData
public void _removeData(java.lang.String fqn)
-
receive
public void receive(Message msg)
Callback. Process the contents of the message; typically an _add() or _set() request- Specified by:
receivein interfaceMessageListener
-
getState
public byte[] getState()
Return a copy of the current cache (tree)- Specified by:
getStatein interfaceMessageListener- Returns:
- byte[]
-
setState
public void setState(byte[] new_state)
Set the cache (tree) to this value- Specified by:
setStatein interfaceMessageListener
-
viewAccepted
public void viewAccepted(View new_view)
Description copied from interface:MembershipListenerCalled when a change in membership has occurred. No long running actions or sending of messages should be done in this callback. If some long running action needs to be performed, it should be done in a separate thread. Note that on reception of the first view (a new member just joined), the channel will not yet be in the connected state. This only happens whenChannel.connect(String)returns.- Specified by:
viewAcceptedin interfaceMembershipListener
-
suspect
public void suspect(Address suspected_mbr)
Called when a member is suspected- Specified by:
suspectin interfaceMembershipListener
-
block
public void block()
Block sending and receiving of messages until viewAccepted() is called- Specified by:
blockin interfaceMembershipListener
-
run
public void run()
Request handler thread- Specified by:
runin interfacejava.lang.Runnable
-
main
public static void main(java.lang.String[] args)
-
-