com.j_spaces.core.client
Class NotifyDelegatorMultiplexer

java.lang.Object
  extended by com.j_spaces.core.client.NotifyDelegatorMultiplexer
All Implemented Interfaces:
INotifyDelegator, Serializable, Remote, EventListener, RemoteEventListener

public class NotifyDelegatorMultiplexer
extends Object
implements INotifyDelegator, Serializable

Just like the call to JavaSpace.notify(), it serves to register interest in matching entries for a specific period of time. If a matching entry is written to the space before the notification request expires, then the notify() method on the given event listener will be called. In comparison to JavaSpace.notify() there is no need to set the client codebase. Both clients and server agree on this proxy object to be used for notifications.

1. Registering for notifications

We use com.j_spaces.core.client.NotifyModifiers as the notification interest. You could either register for all notifications with NotifyModifiers.NOTIFY_ALL or you could use bitwise or operator "|" and have several notifications: e.g. NotifyModifiers.NOTIFY_WRITE | NotifyModifiers.NOTIFY_READ


 NotifyDelegatorMultiplexer ntfyDelegator = 
        new NotifyDelegatorMultiplexer((IJSpace) spaceProxy, 
                                                new MyEntry(), 
                                                theTransaction, 
                                                theListener, 
                                                Lease.FOREVER, 
                                                null, 
                                                fifoEnabled, 
                                                NotifyModifiers.NOTIFY_ALL);
 
theListener has to implement RemoteEventListener and its inherited method notify().
theTransaction could be either a null transaction or an obtained one from a TransactionManager.

2. The notification event

When new entries that match the template arrive at the space, the local listener notify() method will be called by the delegator. From the notification event you can access the entry that triggered the event in the space, and act upon each interested notification.

 public void notify(RemoteEvent event) 
 {
        EntryArrivedRemoteEvent arrivedRemoteEvent = (EntryArrivedRemoteEvent)event;
        int notifyType = arrivedRemoteEvent.getNotifyType();
 
        // section for write notifications 
        if (notifyType == NotifyModifiers.NOTIFY_WRITE)
        { 
                MyEntry myEntry = (MyEntry)((EntryArrivedRemoteEvent)event).getEntry();
                ...
        }
 }
 

The following code demonstrates registering for 5 different template notification:


 // assuming MyListener implements RemoteEventListener 
RemoteEventListener localListener = new MyListener();
NotifyDelegatorMultiplexer nDelegator = new NotifyDelegatorMultiplexer(space);
for( int i = 0; i < 5; i++ ) { Entry myTemplate = new MyTemplate( "MyTemplate: " + i ); EventRegistration eventReg = nDelegator.notify( myTemplate, txn, localListener, lease, handback, fifoEnabled, NotifyModifiers.NOTIFY_WRITE ); System.out.println("Notify registered successfully: " + eventReg); }

See Also:
EntryArrivedRemoteEvent, NotifyDelegatorListener, Serialized Form

Constructor Summary
NotifyDelegatorMultiplexer(IJSpace space)
          This constructor is called by NotifyDelegatorMultiplexer Manager.
NotifyDelegatorMultiplexer(IJSpace space, Entry template, Transaction txn, RemoteEventListener listener, long lease, MarshalledObject handback, boolean fifoEnabled, int notifyMask)
           Creates a new delegator that can receive notifications.
NotifyDelegatorMultiplexer(RemoteEventListener listener)
          Use this constructor when requesting notifications from supplied listener.
NotifyDelegatorMultiplexer(RemoteEventListener listener, boolean fifoEnabled)
          Use this constructor when requesting notifications from supplied listener.
 
Method Summary
 void close()
          Cancels all the notify templates and unexport this instance of NotifyDelegatorMultiplex.
 IJSpace getSpace()
          Returns space proxy or null if NotifyDelegatorMultiplexer was constructed as listener, not via constructor with spaceProxy as parameter.
 EventRegistration notify(Entry template, Transaction txn, RemoteEventListener listener, long lease, MarshalledObject handback, boolean fifoEnabled, int notifyMask)
          Registers a notification with this space for the specified template.
 void notify(RemoteEvent theEvent)
          This is a callback method invoked by the space when an entry that matches the notify templates is written to the space.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NotifyDelegatorMultiplexer

public NotifyDelegatorMultiplexer(IJSpace space)
                           throws RemoteException
This constructor is called by NotifyDelegatorMultiplexer Manager.

Parameters:
space - Space proxy.
Throws:
RemoteException

NotifyDelegatorMultiplexer

public NotifyDelegatorMultiplexer(RemoteEventListener listener)
Use this constructor when requesting notifications from supplied listener. Default is non fifo ordered notifications.

Parameters:
listener - Supplied listener to notify delegator.

NotifyDelegatorMultiplexer

public NotifyDelegatorMultiplexer(RemoteEventListener listener,
                                  boolean fifoEnabled)
Use this constructor when requesting notifications from supplied listener.

Parameters:
listener - Supplied listener to notify delegator.
fifoEnabled - enables/disables ordered notifications.
See Also:
NotifyModifiers

NotifyDelegatorMultiplexer

public NotifyDelegatorMultiplexer(IJSpace space,
                                  Entry template,
                                  Transaction txn,
                                  RemoteEventListener listener,
                                  long lease,
                                  MarshalledObject handback,
                                  boolean fifoEnabled,
                                  int notifyMask)
                           throws TransactionException,
                                  RemoteException
 Creates a new delegator that can receive notifications.
 
 Equivalent to calling:
 NotifyDelegatorMultiplexer nDelegator = new NotifyDelegatorMultiplexer(space);
 nDelegator.notify( template, txn, listener, lease, handback, fifoEnabled, notifyMask );
 

Parameters:
space - the space to register for notification.
template - the template to use for notification.
txn - the transaction to use for notification.
lease - the lease of the notification template.
handback - the handback to use for notification.
fifoEnabled - enables/disables ordered notifications.
notifyMask - Available Notify Types: NotifyModifiers.NOTIFY_WRITE NotifyModifiers.NOTIFY_UPDATE NotifyModifiers.NOTIFY_TAKE NotifyModifiers.NOTIFY_LEASE_EXPIRATION NotifyModifiers.NOTIFY_ALL
Throws:
TransactionException - if the transaction is not valid
RemoteException - if a communication error occurs
See Also:
NotifyModifiers
Method Detail

getSpace

public IJSpace getSpace()
Returns space proxy or null if NotifyDelegatorMultiplexer was constructed as listener, not via constructor with spaceProxy as parameter.


notify

public EventRegistration notify(Entry template,
                                Transaction txn,
                                RemoteEventListener listener,
                                long lease,
                                MarshalledObject handback,
                                boolean fifoEnabled,
                                int notifyMask)
                         throws TransactionException,
                                RemoteException
Registers a notification with this space for the specified template. Through the supplied callback listener, a notification will be sent when an entry that matches a specified template is written (or any other operation chosen by the notifyMask).

An EventRegistration object is returned that can be used to cancel, renew the notification lease, aquire the unique id of the registration, or the sequence number at the time of registration and the space that is the source of the registration.

 EventRegistration eventReg = 
        nDelegator.notify( myTemplate, 
                txn, 
                localListener, 
                lease, 
                handback, 
                fifoEnabled, 
                NotifyModifiers.NOTIFY_WRITE );
 
 System.out.println("Notify registered successfully: " + eventReg);
 

Parameters:
template - The Entry template to be notified of.
txn - When notifications are under transaction.
listener - The RemoteEventListener implementor.
lease - The lease timeout for the template notification.
handback - the handback to use for notification.
fifoEnabled - enables/disables ordered notifications.
notifyMask - notification interest mask specified by NotifyModifiers constants.
Returns:
EventRegistration Object to access registration properties
Throws:
TransactionException
RemoteException

notify

public void notify(RemoteEvent theEvent)
            throws UnknownEventException,
                   RemoteException
This is a callback method invoked by the space when an entry that matches the notify templates is written to the space. This method delegates the call to the user supplied listener.

Specified by:
notify in interface RemoteEventListener
Parameters:
theEvent - the event that the space sent. This event includes the event id, the source, the handback and the sequence number of the notification and the referent Uuid.
Throws:
UnknownEventException - if the user-supplied listener throws this event.
RemoteException - if the user-supplied listener throws this event.

close

public void close()
Cancels all the notify templates and unexport this instance of NotifyDelegatorMultiplex.