[jboss-cvs] JBossAS SVN: r58552 - in trunk/ejb3: docs/tutorial/clusteredentity/src/org/jboss/tutorial/clusteredentity/bean src/main/org/jboss/ejb3/cache/tree

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Nov 18 06:18:53 EST 2006


Author: bstansberry at jboss.com
Date: 2006-11-18 06:18:51 -0500 (Sat, 18 Nov 2006)
New Revision: 58552

Modified:
   trunk/ejb3/docs/tutorial/clusteredentity/src/org/jboss/tutorial/clusteredentity/bean/EntityTestBean.java
   trunk/ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java
Log:
Update to JBC 2.0.0.Alpha1

Modified: trunk/ejb3/docs/tutorial/clusteredentity/src/org/jboss/tutorial/clusteredentity/bean/EntityTestBean.java
===================================================================
--- trunk/ejb3/docs/tutorial/clusteredentity/src/org/jboss/tutorial/clusteredentity/bean/EntityTestBean.java	2006-11-18 11:18:17 UTC (rev 58551)
+++ trunk/ejb3/docs/tutorial/clusteredentity/src/org/jboss/tutorial/clusteredentity/bean/EntityTestBean.java	2006-11-18 11:18:51 UTC (rev 58552)
@@ -35,11 +35,11 @@
 
 import org.hibernate.cache.entry.CacheEntry;
 import org.hibernate.cache.entry.CollectionCacheEntry;
+import org.jboss.cache.Cache;
 import org.jboss.cache.CacheException;
 import org.jboss.cache.Fqn;
 import org.jboss.cache.Node;
-import org.jboss.cache.TreeCache;
-import org.jboss.cache.TreeCacheMBean;
+import org.jboss.cache.jmx.CacheJmxWrapperMBean;
 import org.jboss.mx.util.MBeanProxyExt;
 import org.jboss.mx.util.MBeanServerLocator;
 
@@ -89,7 +89,7 @@
    {
       try
       {
-         TreeCache cache = getCache();
+         Cache cache = getCache();
          String key = "/org/jboss/tutorial/clusteredentity/bean/Customer/org.jboss.tutorial.clusteredentity.bean.Customer#" + id;
          return isInCache(cache, key);
       }
@@ -103,7 +103,7 @@
    {
       try
       {
-         TreeCache cache = getCache();
+         Cache cache = getCache();
          String key = "/org/jboss/tutorial/clusteredentity/bean/Contact/org.jboss.tutorial.clusteredentity.bean.Contact#" + id;
 
          return isInCache(cache, key);
@@ -118,7 +118,7 @@
    {
       try
       {
-         TreeCache cache = getCache();
+         Cache cache = getCache();
          String key = "/org/jboss/tutorial/clusteredentity/bean/Customer/contacts/org.jboss.tutorial.clusteredentity.bean.Customer.contacts#" + id;
 
          return isInCache(cache, key);
@@ -129,30 +129,30 @@
       }
    }
 
-   private TreeCache getCache() throws Exception
+   private Cache getCache() throws Exception
    {
       MBeanServer server = MBeanServerLocator.locateJBoss();
-      TreeCacheMBean proxy = (TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, new ObjectName("jboss.cache:service=EJB3EntityTreeCache"), server);
-      return proxy.getInstance();
+      CacheJmxWrapperMBean proxy = (CacheJmxWrapperMBean)MBeanProxyExt.create(CacheJmxWrapperMBean.class, new ObjectName("jboss.cache:service=EJB3EntityTreeCache"), server);
+      return proxy.getCache();
    }
 
-   private boolean isInCache(TreeCache cache, String key)throws CacheException
+   private boolean isInCache(Cache cache, String key)throws CacheException
    {
       return isInCache(cache, null, key);
    }
 
-   private boolean isInCache(TreeCache cache, Node node, String key) throws CacheException
+   private boolean isInCache(Cache cache, Node node, String key) throws CacheException
    {
       //Not the best way to look up the cache entry, but how hibernate creates the cache entry
       //and fqn seems to be buried deep deep down inside hibernate...
 
       if (node == null)
       {
-         node = cache.get("/");
+         node = cache.getRoot();
       }
 
-      Map map = node.getChildren();
-      for(Object child : map.values())
+      Set children = node.getChildren();
+      for(Object child : children)
       {
          Node childNode = (Node)child;
 
@@ -163,8 +163,8 @@
             return (entry != null) && (entry instanceof CacheEntry || entry instanceof CollectionCacheEntry);
          }
 
-         Map children = childNode.getChildren();
-         if (children != null && children.size() > 0)
+         Set grandchildren = childNode.getChildren();
+         if (grandchildren != null && grandchildren.size() > 0)
          {
             if (isInCache(cache, childNode, key))
             {

Modified: trunk/ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java	2006-11-18 11:18:17 UTC (rev 58551)
+++ trunk/ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java	2006-11-18 11:18:51 UTC (rev 58552)
@@ -21,20 +21,23 @@
   */
 package org.jboss.ejb3.cache.tree;
 
+import java.util.Map;
+
 import javax.ejb.EJBException;
 import javax.ejb.NoSuchEJBException;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
 import org.jboss.aop.Advisor;
-import org.jboss.cache.xml.XmlHelper;
-import org.jboss.cache.eviction.RegionManager;
-import org.jboss.cache.eviction.Region;
+import org.jboss.cache.eviction.EvictionPolicyConfig;
+import org.jboss.cache.eviction.LRUConfiguration;
+import org.jboss.cache.jmx.CacheJmxWrapperMBean;
+import org.jboss.cache.AbstractCacheListener;
+import org.jboss.cache.Cache;
 import org.jboss.cache.CacheException;
-import org.jboss.cache.AbstractTreeCacheListener;
-import org.jboss.cache.Node;
-import org.jboss.cache.TreeCache;
-import org.jboss.cache.TreeCacheMBean;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.InvocationContext;
+import org.jboss.cache.Region;
 import org.jboss.ejb3.Container;
 import org.jboss.ejb3.Pool;
 import org.jboss.ejb3.cache.ClusteredStatefulCache;
@@ -43,7 +46,6 @@
 import org.jboss.mx.util.MBeanServerLocator;
 import org.jboss.logging.Logger;
 import org.jboss.annotation.ejb.cache.tree.CacheConfig;
-import org.w3c.dom.Element;
 
 import org.jboss.cache.Fqn;
 import org.jboss.cache.config.Option;
@@ -59,11 +61,20 @@
    protected static Logger log = Logger.getLogger(StatefulTreeCache.class);
    static int FQN_SIZE = 2; // depth of fqn that we store the session in.
 
+   private static Option BYPASS_OPTION = new Option();
+   private static Option LOCAL_ONLY_OPTION = new Option();
+   static
+   {
+      BYPASS_OPTION.setBypassInterceptorChain(true);
+      LOCAL_ONLY_OPTION.setCacheModeLocal(true);
+   }
+   
    private Pool pool;
-   private TreeCache cache;
+   private Cache cache;
    private Fqn cacheNode;
+   private Region region;
    private ClusteredStatefulCacheListener listener;
-   private RegionManager evictRegionManager;
+   
    public static long MarkInUseWaitTime = 15000;
 
    public StatefulBeanContext create()
@@ -72,7 +83,7 @@
       try
       {
          ctx = (StatefulBeanContext) pool.get();
-         cache.put(cacheNode + "/" + ctx.getId(), "bean", ctx);
+         cache.put(new Fqn(cacheNode, ctx.getId()), "bean", ctx);
          ctx.inUse = true;
          ctx.lastUsed = System.currentTimeMillis();
       }
@@ -93,7 +104,7 @@
       try
       {
          ctx = (StatefulBeanContext) pool.get(initTypes, initValues);
-         Fqn id = Fqn.fromString(cacheNode + "/" + ctx.getId());
+         Fqn id = new Fqn(cacheNode, ctx.getId());
          cache.put(id, "bean", ctx);
          ctx.inUse = true;
          ctx.lastUsed = System.currentTimeMillis();
@@ -112,7 +123,7 @@
    public StatefulBeanContext get(Object key) throws EJBException
    {
       StatefulBeanContext entry = null;
-      Fqn id = Fqn.fromString(cacheNode + "/" +key);
+      Fqn id = new Fqn(cacheNode, key);
       try
       {
          Object obj = cache.get(id, "bean");
@@ -128,7 +139,8 @@
       }
       entry.inUse = true;
       // Mark it to eviction thread that don't passivate it yet.
-      evictRegionManager.markNodeCurrentlyInUse(id, MarkInUseWaitTime);
+      // Note the Fqn we use is relative to the region!
+      region.markNodeCurrentlyInUse(new Fqn(key), MarkInUseWaitTime);
       if(log.isDebugEnabled())
       {
          log.debug("get: retrieved bean with cache id " +id.toString());
@@ -140,21 +152,19 @@
 
    public void remove(Object key)
    {
-      StatefulBeanContext ctx = null;
-      Fqn id = Fqn.fromString(cacheNode + "/" +key);
+      Fqn id = new Fqn(cacheNode, key);
       try
       {
          if(log.isDebugEnabled())
          {
             log.debug("remove: cache id " +id.toString());
          }
-         cache.remove(id);
+         cache.removeNode(id);
       }
       catch (CacheException e)
       {
          throw new RuntimeException(e);
       }
-//      if (ctx != null) pool.remove(ctx);
    }
 
    public void finished(StatefulBeanContext ctx)
@@ -163,9 +173,9 @@
       {
          ctx.inUse = false;
          ctx.lastUsed = System.currentTimeMillis();
-         Fqn id = Fqn.fromString(cacheNode + "/" + ctx.getId());
          // OK, it is free to passivate now.
-         evictRegionManager.unmarkNodeCurrentlyInUse(id);
+         // Note the Fqn we use is relative to the region!
+         region.unmarkNodeCurrentlyInUse(new Fqn(ctx.getId()));
       }
    }
 
@@ -173,7 +183,7 @@
    {
       try
       {
-         cache.put(cacheNode + "/" + ctx.getId(), "bean", ctx);
+         cache.put(new Fqn(cacheNode, ctx.getId()), "bean", ctx);
       }
       catch (CacheException e)
       {
@@ -188,25 +198,28 @@
       CacheConfig config = (CacheConfig) advisor.resolveAnnotation(CacheConfig.class);
       MBeanServer server = MBeanServerLocator.locateJBoss();
       ObjectName cacheON = new ObjectName(config.name());
-      TreeCacheMBean mbean = (TreeCacheMBean) MBeanProxyExt.create(TreeCacheMBean.class, cacheON, server);
-      cache = (TreeCache) mbean.getInstance();
+      CacheJmxWrapperMBean mbean = (CacheJmxWrapperMBean) MBeanProxyExt.create(CacheJmxWrapperMBean.class, cacheON, server);
+      cache = mbean.getCache();
 
       cacheNode = Fqn.fromString("/" + container.getEjbName() + "/");
 
       // Try to create an eviction region per ejb
-      evictRegionManager = cache.getEvictionRegionManager();
-      Element element = getElementConfig(cacheNode.toString(), config.idleTimeoutSeconds(),
-              config.maxSize());
-      Region region = evictRegionManager.createRegion(cacheNode, element);
-      log.debug("initialize(): create eviction region: " +region + " for ejb: " +container.getEjbName());
+      region = cache.getRegion(cacheNode, true);
+      EvictionPolicyConfig epc = getEvictionPolicyConfig((int) config.idleTimeoutSeconds(),
+            config.maxSize());
+      region.setEvictionPolicy(epc);
+      // BES 11/16/2006 uncomment if we switch to per-region marshalling
+//      region.registerContextClassLoader(Thread.currentThread().getContextClassLoader());
+//      region.activate();
+      log.debug("initialize(): created eviction region: " +region + " for ejb: " +container.getEjbName());
    }
-
-   protected Element getElementConfig(String regionName, long timeToLiveSeconds, int maxNodes) throws Exception {
-      String xml = "<region name=\"" +regionName +"\" policyClass=\"org.jboss.cache.eviction.LRUPolicy\">\n" +
-               "<attribute name=\"maxNodes\">" +maxNodes +"</attribute>\n" +
-               "<attribute name=\"timeToLiveSeconds\">"+ timeToLiveSeconds +"</attribute>\n" +
-               "</region>";
-      return XmlHelper.stringToElement(xml);
+   
+   protected EvictionPolicyConfig getEvictionPolicyConfig(int timeToLiveSeconds, int maxNodes)
+   {
+      LRUConfiguration epc = new LRUConfiguration();
+      epc.setTimeToLiveSeconds(timeToLiveSeconds);
+      epc.setMaxNodes(maxNodes);
+      return epc;
    }
 
    public void start()
@@ -215,27 +228,39 @@
       // TODO this approach may not be scalable when there are many beans since then we will need to go thru
       // N listeners to figure out which this event this belongs to.
       listener = new ClusteredStatefulCacheListener();
-      cache.addTreeCacheListener(listener);
+      cache.addCacheListener(listener);
    }
 
    public void stop()
    {
       // Remove the listener
-      cache.removeTreeCacheListener(listener);
+      cache.removeCacheListener(listener);
+      
+      // BES 11/16/2006 uncomment if we switch to per-region marshalling
+      // If we do, we don't need to remove the node below; deactivate() does it
+//      if (region != null)
+//      {
+//         region.deactivate();
+//         region.unregisterContextClassLoader();
+//      }
 
-      Option opt = new Option();
-      opt.setCacheModeLocal(true);
+      // FIXME this method needs to be in Cache
+      ((CacheSPI) cache).getRegionManager().removeRegion(region.getFqn());
+      // Clear any queues
+      region.resetEvictionQueues();
+      region = null;
+      
       try {
-         // remove locally.
-         cache.remove(cacheNode, opt);
-      } catch (CacheException e) {
+         // remove locally.  
+         InvocationContext ctx = cache.getInvocationContext();
+         ctx.setOptionOverrides(getLocalOnlyOption());
+         cache.removeNode(cacheNode);
+      } 
+      catch (CacheException e) 
+      {
          log.error("Stop(): can't remove bean from the underlying distributed cache");
       }
 
-      // Remove the eviction region
-      RegionManager rm = cache.getEvictionRegionManager();
-      rm.removeRegion(cacheNode);
-
       log.debug("stop(): StatefulTreeCache stopped successfully for " +cacheNode);
    }
 
@@ -244,26 +269,54 @@
     * it will get all the treecache events (instead of just passivation/activation). But we will have to
     * wait untill JBossCache2.0 for the refactoring then.
     */
-   public class ClusteredStatefulCacheListener extends AbstractTreeCacheListener
+   public class ClusteredStatefulCacheListener extends AbstractCacheListener
    {
       protected Logger log = Logger.getLogger(ClusteredStatefulCacheListener.class);
 
-      public void nodeActivate(Fqn fqn, boolean pre) {
-         if(pre) return;  // we are not interested in postActivate event
+      // BES 11/18/2006 this was causing stack overflow; switched to nodeLoaded,
+      // which gives direct access to the data
+//      @Override
+//      public void nodeActivated(Fqn fqn, boolean pre) {
+//         if(pre) return;  // we are not interested in preActivate event
+//         if(fqn.size() != FQN_SIZE) return;
+//         if(!fqn.isChildOrEquals(cacheNode)) return;  // don't care about fqn that doesn't belong to me.
+//
+//         StatefulBeanContext bean = null;
+//         try {
+//            // TODO Can this cause deadlock in the cache level? Should be ok but need review.
+//            bean = (StatefulBeanContext) cache.get(fqn, "bean");
+//         } catch (CacheException e) {
+//            log.error("nodeActivate(): can't retrieve bean instance from: " +fqn + " with exception: " +e);
+//            return;
+//         }
+//         
+//         if(bean == null)
+//         {
+//            throw new IllegalStateException("nodeActivate(): null bean instance.");
+//         }
+//
+////         log.debug("nodeActivate(): send postActivate event on fqn: " +fqn);
+//         if(log.isTraceEnabled())
+//         {
+//            log.trace("nodeActivate(): send postActivate event on fqn: " +fqn);
+//         }
+//
+//         bean.postActivate();
+//      }
+      
+      @Override
+      public void nodeLoaded(Fqn fqn, boolean pre, Map nodeData)
+      {
+         if(pre) return;  // we are not interested in preActivate event
          if(fqn.size() != FQN_SIZE) return;
          if(!fqn.isChildOrEquals(cacheNode)) return;  // don't care about fqn that doesn't belong to me.
-
-         StatefulBeanContext bean = null;
-         try {
-            // TODO Can this cause deadlock in the cache level? Should be ok but need review.
-            bean = (StatefulBeanContext) cache.get(fqn, "bean");
-         } catch (CacheException e) {
-            log.error("nodeActivate(): can't retrieve bean instance from: " +fqn + " with exception: " +e);
-            return;
-         }
+         if (nodeData == null) return;
+         
+         StatefulBeanContext bean = (StatefulBeanContext) nodeData.get("bean");
+         
          if(bean == null)
          {
-            throw new IllegalStateException("StatefuleTreeCache.nodeActivate(): null bean instance.");
+            throw new IllegalStateException("nodeActivate(): null bean instance.");
          }
 
 //         log.debug("nodeActivate(): send postActivate event on fqn: " +fqn);
@@ -273,20 +326,29 @@
          }
 
          bean.postActivate();
+         
       }
 
-      public void nodePassivate(Fqn fqn, boolean pre)
-      {
+      @Override
+      public void nodePassivated(Fqn fqn, boolean pre) {
          if(!pre) return;  // we are not interested in postPassivate event
          if(fqn.size() != FQN_SIZE) return;
          if(!fqn.isChildOrEquals(cacheNode)) return;  // don't care about fqn that doesn't belong to me.
 
-         // TODO Can this cause deadlock in the cache level? Should be ok but need review.
-         org.jboss.cache.DataNode node = cache.peek(fqn);
-         StatefulBeanContext bean = (StatefulBeanContext) node.getData().get("bean");
-         if (bean != null && !bean.inUse)
-            bean.prePassivate();
+         
+         try {
+            InvocationContext ctx = cache.getInvocationContext();
+            ctx.setOptionOverrides(getBypassOption());
+            StatefulBeanContext bean = (StatefulBeanContext) cache.get(fqn, "bean");
+            if (bean != null && !bean.inUse)
+               bean.prePassivate();
 
+         } catch (CacheException e) {
+            log.error("nodePassivate(): can't retrieve bean instance from: " +fqn + " with exception: " +e);
+            return;
+         }
+
+//         log.debug("nodePassivate(): send prePassivate event on fqn: " +fqn);
          if(log.isTraceEnabled())
          {
             log.trace("nodePassivate(): send prePassivate event on fqn: " +fqn);
@@ -294,4 +356,28 @@
 
       }
    }
+   
+   private Option getBypassOption()
+   {
+      try
+      {
+         return BYPASS_OPTION.clone();
+      }
+      catch (CloneNotSupportedException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+   
+   private Option getLocalOnlyOption()
+   {
+      try
+      {
+         return LOCAL_ONLY_OPTION.clone();
+      }
+      catch (CloneNotSupportedException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
 }




More information about the jboss-cvs-commits mailing list