Class DistributedQueue
java.lang.Object
org.jgroups.blocks.DistributedQueue
- All Implemented Interfaces:
Cloneable, MembershipListener, MessageListener
public class DistributedQueue
extends Object
implements MessageListener, MembershipListener, Cloneable
Provides the abstraction of a java.util.LinkedList that is replicated at several
locations. Any change to the list (reset, add, remove, etc.) will transparently be
propagated to all replicas in the group. All read-only methods will always access the
local replica.
Both keys and values added to the list must be serializable, the reason being that they will be sent across the network to all replicas of the group. An instance of this class will contact an existing member of the group to fetch its initial state. Beware to use a total protocol on initialization or elements would not be in same order on all replicas.
- Author:
- Romuald du Song
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Channelprotected RpcDispatcherprotected Stringprotected LinkedListprotected org.apache.commons.logging.Logprotected Vectorprotected final Objectprotected Vectorprotected boolean -
Constructor Summary
ConstructorsConstructorDescriptionDistributedQueue(String groupname, ChannelFactory factory, String properties, long state_timeout) Creates a DistributedQueueDistributedQueue(PullPushAdapter adapter, Serializable id) Uses a user-provided PullPushAdapter to create the dispatcher rather than a Channel.DistributedQueue(JChannel channel) -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidvoid_addAtHead(Object value) protected void_remove()void_reset()voidAdd the speficied element at the bottom of the queuevoidaddAll(Collection values) Add the speficied collection to the top of the queue.voidAdd the speficied element at the top of the queuevoidvoidblock()Block sending and receiving of messages until ViewAccepted is calledprotected voidcheckResult(RspList rsp, Object retval) byte[]getState()Answers the group state; e.g., when joining.protected final voidinit()static voidpeek()returns the first object on the queue, without removing it.voidCalled when a message is received.remove()Try to return the first objet in the queue.It does not wait for an object.remove(long timeout) voidvoidreset()voidsetState(byte[] new_state) Sets the group state; e.g., when joining.intsize()final voidstart(long state_timeout) voidstop()voidCalled when a member is suspectedtoString()voidviewAccepted(View new_view) Called when a change in membership has occurred.
-
Field Details
-
logger
protected org.apache.commons.logging.Log logger -
mutex
-
stopped
protected boolean stopped -
internalQueue
-
channel
-
disp
-
groupname
-
notifs
-
members
-
-
Constructor Details
-
DistributedQueue
public DistributedQueue(String groupname, ChannelFactory factory, String properties, long state_timeout) throws ChannelException Creates a DistributedQueue- Parameters:
groupname- The name of the group to joinfactory- The ChannelFactory which will be used to create a channelproperties- The property string to be used to define the channelstate_timeout- The time to wait until state is retrieved in milliseconds. A value of 0 means wait forever.- Throws:
ChannelException
-
DistributedQueue
-
DistributedQueue
Uses a user-provided PullPushAdapter to create the dispatcher rather than a Channel. If id is non-null, it will be used to register under that id. This is typically used when another building block is already using PullPushAdapter, and we want to add this building block in addition. The id is the used to discriminate between messages for the various blocks on top of PullPushAdapter. If null, we will assume we are the first block created on PullPushAdapter. The caller needs to call start(), before using the this block. It gives the opportunity for the caller to register as a lessoner for Notifications events.- Parameters:
adapter- The PullPushAdapter which to use as underlying transportid- A serializable object (e.g. an Integer) used to discriminate (multiplex/demultiplex) between requests/responses for different building blocks on top of PullPushAdapter.
-
-
Method Details
-
init
protected final void init() -
start
public final void start(long state_timeout) throws ChannelClosedException, ChannelNotConnectedException -
getLocalAddress
-
getChannel
-
addNotifier
-
removeNotifier
-
stop
public void stop() -
add
Add the speficied element at the bottom of the queue- Parameters:
value-
-
addAtHead
Add the speficied element at the top of the queue- Parameters:
value-
-
addAll
Add the speficied collection to the top of the queue. Elements are added in the order that they are returned by the specified collection's iterator.- Parameters:
values-
-
getContents
-
size
public int size() -
peek
returns the first object on the queue, without removing it. If the queue is empty this object blocks until the first queue object has been added- Returns:
- the first object on the queue
-
reset
public void reset() -
checkResult
-
remove
Try to return the first objet in the queue.It does not wait for an object.- Returns:
- the first object in the queue or null if none were found.
-
remove
- Parameters:
timeout- The time to wait until an entry is retrieved in milliseconds. A value of 0 means wait forever.- Returns:
- the first object in the queue or null if none were found
-
toString
-
_add
-
_addAtHead
-
_reset
public void _reset() -
_private_reset
protected void _private_reset() -
_remove
-
_addAll
-
receive
Description copied from interface:MessageListenerCalled when a message is received.- Specified by:
receivein interfaceMessageListener- Parameters:
msg-
-
getState
public byte[] getState()Description copied from interface:MessageListenerAnswers the group state; e.g., when joining.- Specified by:
getStatein interfaceMessageListener- Returns:
- byte[]
-
setState
public void setState(byte[] new_state) Description copied from interface:MessageListenerSets the group state; e.g., when joining.- Specified by:
setStatein interfaceMessageListener- Parameters:
new_state-
-
viewAccepted
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
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
-
main
-