-
1. JBoss Cache 3.x Eviction notification
swaminathan.bhaskar May 16, 2011 12:53 PM (in response to swaminathan.bhaskar)Any pointers on how to achieve this would be of great help. Can someone please help ?
-
2. JBoss Cache 3.x Eviction notification
jaysmith May 19, 2011 9:27 AM (in response to swaminathan.bhaskar)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());
}
} -
3. Re: JBoss Cache 3.x Eviction notification
swaminathan.bhaskar May 20, 2011 8:21 PM (in response to jaysmith)Hi Jay,
First off - thank you very much for your response. Much appreciated. Yes, I did try that and it does not work as I would expect. Let me provide you the code:
Here is the listener code:
import org.jboss.cache.notifications.annotation.*;
import org.jboss.cache.notifications.event.*;
@CacheListener
public class MyCacheListener {
@NodeEvicted
public void logNodeEvent(NodeEvictedEvent ne) {
System.out.println("***** Evicted FQN: " + ne.getFqn() + ", Type: " + ne.getType());
}
}
Here is the main code:
import org.jboss.cache.*;
public class MyJBossCacheTest1 {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
if (args.length != 1) {
System.out.printf("Usage: java %s <config-file>\n", MyJBossCacheTest1.class.getName());
System.exit(1);
}
CacheFactory factory = new DefaultCacheFactory();
Cache cache = factory.createCache(args[0]);
cache.addCacheListener(new MyCacheListener());
cache.start();
Fqn myUserData = Fqn.fromString("/my/data/user");
// Prime Cache with User Data
{
int MAX_USERS = 3;
for (int i = 1; i <= MAX_USERS; i++) {
String user = "u." + i;
String email = "user_" + i +"@nowhere.com";
cache.put(myUserData, user, email);
}
}
// Access User - "u.2"
{
String user = (String) cache.get(myUserData, "u.2");
if (user != null) {
System.out.println("Accessed - " + user);
}
else {
System.out.println("Access of u.2 is null");
}
}
// Access Users after sleep of 30 secs
{
System.out.println("Ready to sleep for 30 secs ...");
try {
Thread.sleep(30000);
}
catch (Exception ex) {
}
System.out.println("Ready to access user(s) ...");
int MAX_USERS = 3;
for (int i = 1; i <= MAX_USERS; i++) {
String user = (String) cache.get(myUserData, "u."+i);
if (user != null) {
System.out.println("Accessed - " + user);
}
else {
System.out.println("Access of u." + i + " is null");
}
}
}
cache.stop();
cache.destroy();
}
}
Was expection to see the FQNs: /my/user/data/u.1, /my/data/user/u.2, and /my/data/user/u.3 to be shown by the listener on eviction. But instead I see the following:
***** Evicted FQN: /my/data/user, Type: NODE_EVICTED
***** Evicted FQN: /my/data/user, Type: NODE_EVICTED
In other words I would not know which keys were evicted ... Any ideas ?