|
Summary: How to register to be notified when objects are put to or removed from the space/cache.
OverviewApplications can be notified when new objects are written to the cache, when objects are updated, when objects are removed, or when their time to live expires. The application needs to register in advance to be notified about specific events. When the events occur, a notification is triggered and the event is sent to the application. The event encapsulates the source of the notification, the notification type, and the originated object. A request for notification has two main components: a template and a mask. The template identifies the objects the application is interested in. The mask (also called a NotifyModifier) specifies which operations the application wants to be notified about. The application may want to be notified when the objects are read, written, taken, etc. – or when any operation is performed on the object. GigaSpaces supplies a mechanism that handles this process, via the NotifyDelegator (com.j_spaces.core.client.NotifyDelegator; see Javadoc). The application instantiates this class, supplying a template, a mask, some other parameters, and most importantly a RemoteEventListener. The listener acts as a callback, and is used by the space when a matching event occurs. In such a case, the cache instance calls the RemoteEventListener.notify() implementation. This process happens in a separate thread different than the application main thread. ExampleIn the following example, a sample application instantiates the NotifyDelegator, puts objects into a cache, and is automatically notified of each put operation. package com.j_spaces.examples.hellomap; import com.j_spaces.map.*; import com.j_spaces.core.*; import com.j_spaces.core.client.*; import net.jini.core.event.*; import net.jini.core.lease.*; public class HelloWorldNotifyDelegator { public static void main(String[] args) throws Exception { say("\nWelcome to Gigaspaces Map NotifyDelegator.\n"); if ( args.length != 1 ) { say("Usage: <spaceURL>"); say("<protocol>://host:port/containername/spacename"); System.exit(1); } try { IMap cache = (IMap)CacheFinder.find(args[0]); say("Cache has " + cache.size() + " values"); IJSpace space = cache.getMasterSpace(); say("Clean map-cache..."); space.clean(); say("Register the EventListener..."); RemoteEventListener theListener = new RemoteEventListener() { public void notify(RemoteEvent theEvent){ say("Event received: " + theEvent); EntryArrivedRemoteEvent arrivedRemoteEvent = (EntryArrivedRemoteEvent)theEvent; int notifyType = arrivedRemoteEvent.getNotifyType(); // section for write notifications if (notifyType == NotifyModifiers.NOTIFY_WRITE) { try { ExternalEntry entry = (ExternalEntry)arrivedRemoteEvent.getEntry(true); Object[] values = entry.getFieldsValues(); Object key = values[Envelope.KEY]; Object value = values[Envelope.VALUE]; say("Map-Key: " + key + " Value: " + value); } catch (Throwable e) { e.printStackTrace(); } } } }; /* Register the NotifyDelegator: * com.j_spaces.core.client.NotifyDelegator(IJSpace space, * Entry template, Transaction txn, * RemoteEventListener listener, * long lease, MarshalledObject handback, * boolean fifoEnabled, int notifyMask) */ NotifyDelegator ntfyDelegator = new NotifyDelegator( space, new Envelope(), null, theListener, Lease.FOREVER, null, false, NotifyModifiers.NOTIFY_ALL); say("Values are addes to the cache..."); cache.put("Key1", "value_1"); cache.put("Key2", "value_2"); cache.put("Key3", "value_3"); say("Cache has " + cache.size()+" values"); System.exit(0); } catch( FinderException ex ) { ex.printStackTrace(); say("Could not find cache: " + args[0]); say("Please check that GigaSpaces Server is running."); } catch (Exception e) { e.printStackTrace(); } } public static void say(String msg) { System.out.println(msg); } } |
(works on Firefox 2 and Internet Explorer 7)