org.jgroups.blocks

Class PullPushAdapter

Implemented Interfaces:
Runnable, ChannelListener

public class PullPushAdapter
extends java.lang.Object
implements Runnable, ChannelListener

Allows a client of Channel to be notified when messages have been received instead of having to actively poll the channel for new messages. Typically used in the client role (receive()). As this class does not implement interface Transport, but uses it for receiving messages, an underlying object has to be used to send messages (e.g. the channel on which an object of this class relies).

Multiple MembershipListeners can register with the PullPushAdapter; when a view is received, they will all be notified. There is one main message listener which sends and receives message. In addition, MessageListeners can register with a certain tag (identifier), and then send messages tagged with this identifier. When a message with such an identifier is received, the corresponding MessageListener will be looked up and the message dispatched to it. If no tag is found (default), the main MessageListener will receive the message.

Version:
$Revision
Author:
Bela Ban

Nested Class Summary

static class
PullPushAdapter.PullHeader

Field Summary

protected MessageListener
listener
protected HashMap
listeners
protected Log
log
protected List
membership_listeners
protected Thread
receiver_thread
protected Transport
transport

Constructor Summary

PullPushAdapter(Transport transport)
PullPushAdapter(Transport transport, MembershipListener ml)
PullPushAdapter(Transport transport, MessageListener l)
PullPushAdapter(Transport transport, MessageListener l, MembershipListener ml)

Method Summary

void
addMembershipListener(MembershipListener l)
void
channelClosed(Channel channel)
void
channelConnected(Channel channel)
void
channelDisconnected(Channel channel)
void
channelReconnected(Address addr)
void
channelShunned()
MessageListener
getListener()
Transport
getTransport()
protected void
handleMessage(Message msg)
Check whether the message has an identifier.
protected void
notifyBlock()
protected void
notifySuspect(Address suspected_mbr)
protected void
notifyViewChange(View v)
void
registerListener(Serializable identifier, MessageListener l)
Sets a listener to messages with a given identifier.
void
removeMembershipListener(MembershipListener l)
void
run()
Reentrant run(): message reception is serialized, then the listener is notified of the message reception
void
send(Serializable identifier, Message msg)
Sends a message to the group - listeners to this identifier will receive the messages.
void
send(Message msg)
Sends a message with no identifier; listener member will get this message on the other group members.
void
setListener(MessageListener l)
void
setMembershipListener(MembershipListener ml)
Deprecated. Use addMembershipListener(MembershipListener)
void
start()
void
stop()
void
unregisterListener(Serializable identifier)
Removes a message listener to a given identifier from the message listeners map.

Field Details

listener

protected MessageListener listener

listeners

protected final HashMap listeners

log

protected final Log log

membership_listeners

protected final List membership_listeners

receiver_thread

protected Thread receiver_thread

transport

protected Transport transport

Constructor Details

PullPushAdapter

public PullPushAdapter(Transport transport)

PullPushAdapter

public PullPushAdapter(Transport transport,
                       MembershipListener ml)

PullPushAdapter

public PullPushAdapter(Transport transport,
                       MessageListener l)

PullPushAdapter

public PullPushAdapter(Transport transport,
                       MessageListener l,
                       MembershipListener ml)

Method Details

addMembershipListener

public void addMembershipListener(MembershipListener l)

channelClosed

public void channelClosed(Channel channel)
Specified by:
channelClosed in interface ChannelListener

channelConnected

public void channelConnected(Channel channel)
Specified by:
channelConnected in interface ChannelListener

channelDisconnected

public void channelDisconnected(Channel channel)
Specified by:
channelDisconnected in interface ChannelListener

channelReconnected

public void channelReconnected(Address addr)
Specified by:
channelReconnected in interface ChannelListener

channelShunned

public void channelShunned()
Specified by:
channelShunned in interface ChannelListener

getListener

public MessageListener getListener()
Returns:
Returns the listener.

getTransport

public Transport getTransport()

handleMessage

protected void handleMessage(Message msg)
Check whether the message has an identifier. If yes, lookup the MessageListener associated with the given identifier in the hashtable and dispatch to it. Otherwise just use the main (default) message listener

notifyBlock

protected void notifyBlock()

notifySuspect

protected void notifySuspect(Address suspected_mbr)

notifyViewChange

protected void notifyViewChange(View v)

registerListener

public void registerListener(Serializable identifier,
                             MessageListener l)
Sets a listener to messages with a given identifier. Messages sent with this identifier in their headers will be routed to this listener. Note: there can be only one listener for one identifier; if you want to register a different listener to an already registered identifier, then unregister first.
Parameters:
identifier - - messages sent on the group with this object will be received by this listener
l - - the listener that will get the message

removeMembershipListener

public void removeMembershipListener(MembershipListener l)

run

public void run()
Reentrant run(): message reception is serialized, then the listener is notified of the message reception

send

public void send(Serializable identifier,
                 Message msg)
            throws Exception
Sends a message to the group - listeners to this identifier will receive the messages.
Parameters:
identifier - the key that the proper listeners are listenting on
msg - the Message to be sent

send

public void send(Message msg)
            throws Exception
Sends a message with no identifier; listener member will get this message on the other group members.
Parameters:
msg - the Message to be sent

setListener

public void setListener(MessageListener l)

setMembershipListener

public void setMembershipListener(MembershipListener ml)

Deprecated. Use addMembershipListener(MembershipListener)


start

public void start()

stop

public void stop()

unregisterListener

public void unregisterListener(Serializable identifier)
Removes a message listener to a given identifier from the message listeners map.
Parameters:
identifier - - the key to whom we do not want to listen any more

Copyright B) 1998-2005 Bela Ban. All Rights Reserved.