5 Replies Latest reply: Aug 12, 2011 5:29 AM by MagnusE RSS

    EventProcessor question

    MagnusE
      I am playing around with the DelegatingBackingmapListener. I want to create a cache object that will be called with its "own" backing map events. I have got something that looks like this to compile (and work with the EntryInsertedEvent):
      public class MyObject implements EventProcessor<EntryEvent<Map.Entry>> {
      
          @Override
          public void process(EventDispatcher eventDispatcher, EntryEvent<Map.Entry> entryEvent) {
              if (entryEvent instanceof EntryInsertedEvent) {
                  EntryInsertedEvent<?> insertedEvent = (EntryInsertedEvent) entryEvent;
                  System.out.println("EntryInsertedEvent : " + entryEvent + ", key = " + entryEvent.getEntry().getKey() + ", value = " + entryEvent.getEntry().getValue());
              } else if (entryEvent instanceof EntryUpdatedEvent) {
                  EntryUpdatedEvent<?> updatedEvent = (EntryUpdatedEvent) entryEvent;
                  System.out.println("EntryUpdatedEvent : " + entryEvent + ", key = " + entryEvent.getEntry().getKey() + ", value = " + entryEvent.getEntry().getValue());
              } else if (entryEvent instanceof EntryRemovedEvent) {
                  EntryRemovedEvent<?> removedEvent = (EntryRemovedEvent) entryEvent;
                  System.out.println("EntryRemovedEvent : " + entryEvent + ", key = " + entryEvent.getEntry().getKey() + ", value = " + entryEvent.getEntry().getValue());
              }
              System.out.println("Dispatcher : " + eventDispatcher.toString());
          }
      
          // More stuff here 
      }
      It is however not clear to me if those are the only events I can distinguish or if I somehow also can find out when the entry is evicted, moved to another node etc?

      Can I specify some more "capable" interface in the generic template than EntryEvent<Map.Entry> that would give me more information?

      Is there some "clearner" way to determine the event type than a bunch of "instanceof" - for instance some ID that I can "switch" on?

      I find it quite hard to figure out how to use the "basic" packages of the incubator only from the existing documentation and the usage in the other incubator components is a bit to involved (for a person not familiar with the source) to be a practical source of information (this often ends up in guesses "I THINK it can be used like this" that are not always true making it time consuming).

      /Magnus

      Edited by: MagnusE on Aug 8, 2011 1:37 PM
        • 1. Re: EventProcessor question
          pmackin
          Hi Magnus

          I think part of the confusion is that Common has migrated to a new event model while leaving the old code for backwards compatibility. You should be using events from backing map such as:

          import com.oracle.coherence.common.events.backingmap.BackingMapEntryDepartedEvent;

          You do need to use instanceof for comparison. Be careful to check for BackingMapEntryDepartedEvent before BackingMapEntryRemovedEvent since a ..DepartedEvent is a RemovedEvent. The same is true for BackingMapEntryArrivedEvent which should be checked before BackingMapEntryInsertedEvent.

          Make sure you use EntryEvent from

          import com.oracle.coherence.common.events.EntryEvent;

          This event has the old and new entries.

          Paul
          • 2. Re: EventProcessor question
            MagnusE
            Thanks for the info. I am now trying the following little debug code that seem to work:
            public class ServiceObject implements EventProcessor<EntryEvent<BinaryEntry>>, PortableObject {
              public void process(EventDispatcher eventDispatcher, EntryEvent<BinaryEntry> entryEvent) {
                      if (entryEvent instanceof EntryInsertedEvent) {
                        if (entryEvent instanceof BackingMapEntryArrivedEvent) {
                            BackingMapEntryArrivedEvent arrivedEvent = (BackingMapEntryArrivedEvent) entryEvent;
                            System.out.println("Arrived: " + arrivedEvent);
                            return;
                        } else if (entryEvent instanceof BackingMapEntryInsertedEvent) {
                            BackingMapEntryInsertedEvent insertedEvent = (BackingMapEntryInsertedEvent) entryEvent;
                            System.out.println("Inserted: " + insertedEvent);
                            return;
                        }
                    } else if (entryEvent instanceof BackingMapEntryRemovedEvent) {
                        if (entryEvent instanceof BackingMapEntryDepartedEvent) {
                            BackingMapEntryDepartedEvent departedEvent = (BackingMapEntryDepartedEvent) entryEvent;
                            System.out.println("Departed: " + departedEvent);
                            return;
                        } else if (entryEvent instanceof BackingMapEntryEvictedEvent) {
                            BackingMapEntryEvictedEvent evictedEvent = (BackingMapEntryEvictedEvent) entryEvent;
                            System.out.println("Evicted: " + evictedEvent);
                            return;
                        }
                    } else if (entryEvent instanceof EntryUpdatedEvent) {
                        if (entryEvent instanceof BackingMapEntryUpdatedEvent) {
                            BackingMapEntryUpdatedEvent updatedEvent = (BackingMapEntryUpdatedEvent) entryEvent;
                            System.out.println("Updated: " + updatedEvent);
                            return;
                        }
                    }
                    System.out.println("Unknown EventType " + entryEvent.getClass().getName());
                    throw new IllegalArgumentException("Unknown EventType " + entryEvent.getClass().getName());
                }
            }
            Is it correct to use the generic parameters the way I do or is there some more appropriate interfaces/classes I can use?

            /Magnus
            • 3. Re: EventProcessor question
              MagnusE
              What about when an object is deleted from the cache - will this not cause any event to be sent to the object or does this also result in an "evicted" event?

              /Magnus
              • 4. Re: EventProcessor question
                pmackin
                Hi Magnus

                A delete will result in a BackingMapEntryRemovedEvent. I am not sure if that answers your question.

                Paul
                • 5. Re: EventProcessor question
                  MagnusE
                  Thanks a lot it sure did - I had missed one level of interfaces/classes but now I think I got it :-)
                  if (entryEvent instanceof EntryInsertedEvent) {
                     if (entryEvent instanceof BackingMapEntryArrivedEvent) {
                        System.out.println("Arrived!");
                        return;
                     } else if (entryEvent instanceof BackingMapEntryInsertedEvent) {
                        System.out.println("Inserted!");
                        return;
                     }
                  } else if (entryEvent instanceof EntryRemovedEvent) {
                     if (entryEvent instanceof BackingMapEntryDepartedEvent) {
                        System.out.println("Departed!");
                        return;
                     } else if (entryEvent instanceof BackingMapEntryEvictedEvent) {
                        System.out.println("Evicted!");
                        return;
                     } else if (entryEvent instanceof BackingMapEntryRemovedEvent){
                        System.out.println("Removed!");
                        return;
                     }
                  } else if (entryEvent instanceof EntryUpdatedEvent) {
                     if (entryEvent instanceof BackingMapEntryUpdatedEvent) {
                        System.out.println("Updated!");
                        return;
                     }
                  }
                  System.out.println("Unknown EventType " + entryEvent.getClass().getName());