1

I am using Apache JCS in our application to cache commonly used data at application start-up. However, if some of the data in cache is not used till its idle time, I want to manually reload that value from database.

So, basically I want to over-ride the JCS events such that my java program is aware of instants when a key is getting removed from cache, on account of the key reaching its idle time.

Suban
  • 31
  • 3
  • Override the handleEvent method in IEventListener. Refer to: http://stackoverflow.com/questions/4473479/jcs-notify-on-expire-remove – Keshi May 30 '12 at 19:06

1 Answers1

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