JBoss Community

Re: JBoss Cache 3.x Eviction notification

created by jaysmith in JBoss Cache - View the full discussion

Hi,

 

you sure the CacheListener didn't work? I have this code for a listener and it works pretty well if memory serves me correctly.

 

import java.text.SimpleDateFormat;
import java.util.Date;

 

import org.jboss.cache.notifications.annotation.CacheListener;
import org.jboss.cache.notifications.annotation.CacheStarted;
import org.jboss.cache.notifications.annotation.NodeActivated;
import org.jboss.cache.notifications.annotation.NodeCreated;
import org.jboss.cache.notifications.annotation.NodeEvicted;
import org.jboss.cache.notifications.annotation.NodeLoaded;
import org.jboss.cache.notifications.annotation.NodePassivated;
import org.jboss.cache.notifications.annotation.NodeRemoved;
import org.jboss.cache.notifications.annotation.NodeVisited;
import org.jboss.cache.notifications.event.CacheStartedEvent;
import org.jboss.cache.notifications.event.NodeActivatedEvent;
import org.jboss.cache.notifications.event.NodeCreatedEvent;
import org.jboss.cache.notifications.event.NodeEvictedEvent;
import org.jboss.cache.notifications.event.NodeLoadedEvent;
import org.jboss.cache.notifications.event.NodePassivatedEvent;
import org.jboss.cache.notifications.event.NodeRemovedEvent;
import org.jboss.cache.notifications.event.NodeVisitedEvent;

 

@CacheListener
public class CustomCacheListener {
    @CacheStarted
    public final void cacheStarted(final CacheStartedEvent e) {
        System.out.println(format(new Date()) + " [CustomCacheListener] Cache started");
    }

 

    private String format(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:MM:ss.SSS");
        return formatter.format(date);
    }

 

    @NodeActivated
    public final void nodeActivated(final NodeActivatedEvent ne) {
        System.out.println(format(new Date()) + " [CustomCacheListener] Activated key: " + ne.getFqn().toString());
    }

 

    @NodeCreated
    public final void nodeCreated(final NodeCreatedEvent ne) {
        System.out.println(format(new Date()) + " [CustomCacheListener] Created key: " + ne.getFqn().toString());
    }

 

    @NodeEvicted
    public final void nodeEvicted(final NodeEvictedEvent ne) {
        System.out.println(format(new Date()) + " [CustomCacheListener] Evicted key: " + ne.getFqn().toString());
    }

 

    @NodeLoaded
    public final void nodeLoaded(final NodeLoadedEvent ne) {
        System.out.println(format(new Date()) + " [CustomCacheListener] Loaded key: " + ne.getFqn().toString());
    }

 

    @NodePassivated
    public final void nodePassivated(final NodePassivatedEvent ne) {
        System.out.println(format(new Date()) + " [CustomCacheListener] Passivated key: " + ne.getFqn().toString());
    }

 

    @NodeRemoved
    public final void nodeRemoved(final NodeRemovedEvent cere) {
        System.out.println(format(new Date()) + " [CustomCacheListener] Removed key: " + cere.getFqn().toString());
    }

 

    @NodeVisited
    public final void nodeVisited(final NodeVisitedEvent ne) {
        System.out.println(format(new Date()) + " [CustomCacheListener] Visited key: " + ne.getFqn().toString());
    }
}

Reply to this message by going to Community

Start a new discussion in JBoss Cache at Community