1

we use JCS very simply. Not distributed or anything, simply:

JCS jcs = JCS.getInstance("region-name");

I'm trying to register some kind of listener that can be used to receive a notification/event when an element is removed or expired from the cache...

I've been digging through the JCS javadoc for awhile now and I've tried: - adding an Implementation of IElementEventHandler to the default ElementAttributes of the cache ... it never gets called. - using the various implementations of ICacheObserver to register an ICacheListener but that never gets called either. I'm not necessarily sure this point is the correct way of doing it as I think this is intended for more advanced uses of JCS ...

Does anyone know how (or if it's possible) to register some kind of listener/obsverver/whatever that will accomplish this? My final goal is to be able to be notified of when something is removed from the cache basically ... I don't particularly care about how provided it isn't a massive kludge :P

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
arw
  • 131
  • 1
  • 8

2 Answers2

0

Create an abstract class that registers the events your interested in capturing. This works for me to capture the two events.

  private static final Set<Integer> EVENTS = new HashSet<Integer>();
  {
    EVENTS.add(IElementEventHandler.ELEMENT_EVENT_EXCEEDED_IDLETIME_BACKGROUND);
    EVENTS.add(IElementEventHandler.ELEMENT_EVENT_EXCEEDED_MAXLIFE_BACKGROUND);
  }

  @Override
  public synchronized void handleElementEvent(IElementEvent event) {
   // Check for element expiration based on EVENTS.
   LOG.debug("Handling event of type : " + event.getElementEvent() + ".");
   if (EVENTS.contains(event.getElementEvent())) {
     ElementEvent elementEvent = (ElementEvent)event;
     CacheElement element = (CacheElement)elementEvent.getSource();
     handleEvent(element);
   }

  }
  // Abstract method to handle events
  protected abstract void handleEvent(CacheElement element);
  }

Add this abstract event handler to the jcs factory definition as follows

     JCS jcs = JCSCacheFactory.getCacheInstance(regionName);
     IElementAttributes attributes = jcs.getDefaultElementAttributes();
     attributes.addElementEventHandler(handler);
     jcs.setDefaultElementAttributes(attributes);
Keshi
  • 906
  • 10
  • 23
0

From what I can tell after a short review of the JCS source, it looks like those interfaces are only tied to the remote cache stuff - which I've never used. Additionally, I examined LRUMemoryCache and a few others and it looks like the calls to remove don't link in to any event handlers. Long story short, I couldn't find anything in JCS that does what you are asking.

I won't say it is impossible, but I would say it looks unlikely.

You can check out the source here and look further.

Good luck.

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
  • That's the same conclusion I came to with my digging ... :( Thanks for confirming. – arw Dec 20 '10 at 16:14