0 Replies Latest reply on Apr 1, 2009 9:42 AM by Morten Hattesen

    Failure to configure a cache with a TimeToLive expiration

    Morten Hattesen Newbie

      Using JBoss Cache 3.0.3-GA, I have not been able to configure a simple cache with a "TimeToLive" expiration.

      Firstly, whenever the eviction thread wakes up, a warning is logged:

      WARN org.jboss.cache.eviction.ExpirationAlgorithm - No expiration key 'expiration' for Node: /


      Secondly, no eviction is being performed.

      The configuration is reproduced in this unit-test code (the production code has the cache wired up using a Spring IOC context configuration):

      import static org.junit.Assert.assertEquals;
      
      import org.apache.log4j.Logger;
      import org.jboss.cache.Cache;
      import org.jboss.cache.DefaultCacheFactory;
      import org.jboss.cache.Node;
      import org.jboss.cache.config.Configuration;
      import org.jboss.cache.config.EvictionConfig;
      import org.jboss.cache.config.EvictionRegionConfig;
      import org.jboss.cache.eviction.ExpirationAlgorithmConfig;
      import org.junit.Test;
      
      
      public class TestJbossCacheConfig {
       private static final Logger logger = Logger.getLogger(TestJbossCacheConfig.class);
       final Cache<String, String> cache;
       private final long TIME_TO_LIVE = 1000;
       private Node<String, String> root;
      
       public TestJbossCacheConfig() {
       ExpirationAlgorithmConfig eac = new ExpirationAlgorithmConfig();
       eac.setTimeToLive(TIME_TO_LIVE);
      
       EvictionRegionConfig erc = new EvictionRegionConfig();
       erc.setRegionName("/");
       erc.setEvictionAlgorithmConfig(eac);
      
       EvictionConfig ec = new EvictionConfig();
       ec.setWakeupInterval(TIME_TO_LIVE / 2); //msec
       ec.setDefaultEvictionRegionConfig(erc);
      
       Configuration config = new Configuration();
       config.setEvictionConfig(ec);
      
       cache = new DefaultCacheFactory<String, String>().createCache(config);
       root = cache.getRoot();
       }
      
      
       @Test
       //@Ignore
       public void testEviction() throws InterruptedException {
      
       root.put("1", "one");
       root.put("2", "two");
      
       assertEquals("one", root.get("1"));
      
       assertEquals(2, root.dataSize());
      
       logger.info("Sleeps while eviction kicks in");
       Thread.sleep(TIME_TO_LIVE * 2);
       /*
       * During sleep:
       * "WARN org.jboss.cache.eviction.ExpirationAlgorithm - No expiration key 'expiration' for Node: /"
       */
      
       /*
       * java.lang.AssertionError: expected:<0> but was:<2>
       */
       assertEquals(0, root.dataSize());
       }
      }


      What am I doing wrong here?

      Another point: Although I know that JBoss cache is typically configured using an XML, or an IOC framework, it seems a little excessive to require four levels of nested configuration objects to configure a simple cache:
      configuration = new Configuration(new EvictionConfig(new EvictionRegionConfig(new ExpirationAlgorithmConfig())))