[Opensymphony-oscache] How do you make listeners listen??

John Hughes <[email protected]> Thu, 07 Jul 2005 11:12:28 CDT
Newsgroups gmane.comp.java.open-symphony.os-cache
Message-ID <1796563.1120752910858.JavaMail.os-j2ee@opensymphony01.contegix.com>
Can someone tell me why this simple example doesn't do anything with the event listener I'm adding? There is very little documentation help here. What is the "clazz" argument for addCacheEventListener, anyway? Could it ever be anything other than the actual class of the thing I'm passing in? If so what, and if not, why am I passing it in?

Here's the (very simple) code, which I expected to print out a bunch of messages indicating that the listener methods were being called, but doesn't:

import com.opensymphony.oscache.base.*;
import com.opensymphony.oscache.base.algorithm.*;
import com.opensymphony.oscache.base.events.*;
import com.opensymphony.oscache.extra.*;
import com.opensymphony.oscache.general.*;
                                                                                
public class TestListener
    implements CacheEntryEventListener
{
    public void cacheEntryAdded(CacheEntryEvent e) {
        System.out.println("ENTRY ADDED!");
    }
    public void cacheEntryFlushed(CacheEntryEvent e) {
        System.out.println("ENTRY FLUSHED!");
    }
    public void cacheEntryRemoved(CacheEntryEvent e) {
        System.out.println("ENTRY REMOVED!");
    }
    public void cacheEntryUpdated(CacheEntryEvent e) {
        System.out.println("ENTRY UPDATED!");
    }
    public void cacheFlushed(CachewideEvent e) {
        System.out.println("CACHE FLUSHED!");
    }
    public void cacheGroupFlushed(CacheGroupEvent e) {
        System.out.println("GROUP FLUSHED!");
    }
    public void cachePatternFlushed(CachePatternEvent e) {
        System.out.println("PATTERN FLUSHED!");
    }
    public static void main(String[] arg) {
        Cache cache =
            new Cache(true, false, false, false,
                      "com.opensymphony.oscache.base.algorithm.LRUCache",
                      5);
        TestListener listener = new TestListener();
        cache.addCacheEventListener(listener, listener.getClass());
        int count = 20;
        for (int i=0; i<count; ++i)
            cache.putInCache(String.valueOf(i), new Integer(i));
        for (int i=0; i<count; ++i) {
            String key = String.valueOf(i);
            try {
                Integer x = (Integer)cache.getFromCache(key);
            }
            catch (NeedsRefreshException e) {
                cache.cancelUpdate(key);
                System.out.println(":::NOT FOUND!");
            }
        }
    }
                                                                                
}

---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=4018&messageID=9961#9961