[exo-jcr-commits] exo-jcr SVN: r1224 - in jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src: test/java/org/exoplatform/services/jcr/lab/cluster/prepare and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Dec 29 04:20:10 EST 2009


Author: skabashnyuk
Date: 2009-12-29 04:20:10 -0500 (Tue, 29 Dec 2009)
New Revision: 1224

Modified:
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JbossCacheIndexUpdateMonitor.java
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare/TestIndexUpdateMonitor.java
Log:
EXOJCR-331 : Added locks

Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JbossCacheIndexUpdateMonitor.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JbossCacheIndexUpdateMonitor.java	2009-12-29 01:00:21 UTC (rev 1223)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/jbosscache/JbossCacheIndexUpdateMonitor.java	2009-12-29 09:20:10 UTC (rev 1224)
@@ -25,6 +25,8 @@
 import org.jboss.cache.Cache;
 import org.jboss.cache.CacheSPI;
 import org.jboss.cache.Fqn;
+import org.jboss.cache.lock.LockManager;
+import org.jboss.cache.lock.LockType;
 
 import java.io.IOException;
 import java.io.Serializable;
@@ -67,6 +69,25 @@
 
    }
 
+   public boolean lock(String name, LockType lockType) throws InterruptedException
+   {
+      LockManager lm = ((CacheSPI<Serializable, Object>)cache).getComponentRegistry().getComponent(LockManager.class);
+      return lm.lock(Fqn.fromRelativeFqn(PARAMETER_ROOT, Fqn.fromString(name)), lockType, Integer.MAX_VALUE);
+   }
+
+   public void unlock(String name)
+   {
+      LockManager lm = ((CacheSPI<Serializable, Object>)cache).getComponentRegistry().getComponent(LockManager.class);
+      lm.unlock(Fqn.fromRelativeFqn(PARAMETER_ROOT, Fqn.fromString(name)), cache.getInvocationContext()
+         .getGlobalTransaction());
+   }
+
+   public boolean isLocked(String name)
+   {
+      LockManager lm = ((CacheSPI<Serializable, Object>)cache).getComponentRegistry().getComponent(LockManager.class);
+      return lm.isLocked(Fqn.fromRelativeFqn(PARAMETER_ROOT, Fqn.fromString(name)));
+   }
+
    /**
     * @see org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitor#getUpdateInProgress()
     */

Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare/TestIndexUpdateMonitor.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare/TestIndexUpdateMonitor.java	2009-12-29 01:00:21 UTC (rev 1223)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/test/java/org/exoplatform/services/jcr/lab/cluster/prepare/TestIndexUpdateMonitor.java	2009-12-29 09:20:10 UTC (rev 1224)
@@ -22,16 +22,19 @@
 
 import org.exoplatform.services.jcr.impl.core.query.IndexerIoMode;
 import org.exoplatform.services.jcr.impl.core.query.jbosscache.JbossCacheIndexUpdateMonitor;
-import org.exoplatform.services.jcr.impl.core.query.lucene.IndexUpdateMonitor;
 import org.exoplatform.services.log.ExoLogger;
 import org.exoplatform.services.log.Log;
 import org.jboss.cache.Cache;
 import org.jboss.cache.CacheFactory;
+import org.jboss.cache.CacheSPI;
 import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.lock.LockType;
 
 import java.io.Serializable;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import javax.transaction.TransactionManager;
+
 /**
  * @author <a href="mailto:Sergey.Kabashnyuk at exoplatform.org">Sergey Kabashnyuk</a>
  * @version $Id: exo-jboss-codetemplates.xml 34360 2009-07-22 23:58:59Z ksm $
@@ -44,8 +47,10 @@
     */
    private final Log log = ExoLogger.getLogger(TestIndexUpdateMonitor.class);
 
-   private IndexUpdateMonitor indexUpdateMonitor;
+   private JbossCacheIndexUpdateMonitor indexUpdateMonitor;
 
+   private Cache<Serializable, Object> cache;
+
    /**
     * @see org.exoplatform.services.jcr.BaseStandaloneTest#setUp()
     */
@@ -54,9 +59,22 @@
    {
       // TODO Auto-generated method stub
       super.setUp();
-      indexUpdateMonitor = new JbossCacheIndexUpdateMonitor(createCache(), IndexerIoMode.READ_WRITE);
+      cache = createCache();
+      TransactionManager tm = ((CacheSPI<Serializable, Object>)cache).getTransactionManager();
+      indexUpdateMonitor = new JbossCacheIndexUpdateMonitor(cache, IndexerIoMode.READ_WRITE);
    }
 
+   /**
+    * @see junit.framework.TestCase#tearDown()
+    */
+   @Override
+   protected void tearDown() throws Exception
+   {
+      // TODO Auto-generated method stub
+      super.tearDown();
+      cache.destroy();
+   }
+
    public void testSimpleBoolean() throws Exception
    {
       //test default
@@ -76,8 +94,24 @@
 
    }
 
-   public void testMultiThread() throws Exception
+   public void testLock() throws Exception
    {
+      String lockName = "testLock";
+      assertFalse(indexUpdateMonitor.isLocked(lockName));
+      assertTrue(indexUpdateMonitor.lock(lockName, LockType.WRITE));
+      assertTrue(indexUpdateMonitor.isLocked(lockName));
+      LockChecker checker = new LockChecker(indexUpdateMonitor, lockName);
+      Thread lockThread = new Thread(checker);
+      assertFalse(checker.isWaiting());
+      lockThread.start();
+      assertTrue(checker.isWaiting());
+      indexUpdateMonitor.unlock(lockName);
+      assertFalse(checker.isWaiting());
+
+   }
+
+   public void _testMultiThread() throws Exception
+   {
       AtomicBoolean atomicBoolean = new AtomicBoolean();
       ThreadGroup chengers = new ThreadGroup("Changers");
       ThreadGroup checkers = new ThreadGroup("Checkers");
@@ -106,6 +140,52 @@
       checkers.destroy();
    }
 
+   private class LockChecker implements Runnable
+   {
+      private final String lockName;
+
+      private boolean waiting = false;
+
+      /**
+       * @return the waiting
+       */
+      protected boolean isWaiting()
+      {
+         return waiting;
+      }
+
+      /**
+       * @param indexUpdateMonitor
+       */
+      public LockChecker(JbossCacheIndexUpdateMonitor indexUpdateMonitor, String lockName)
+      {
+         super();
+         this.indexUpdateMonitor = indexUpdateMonitor;
+         this.lockName = lockName;
+      }
+
+      private final JbossCacheIndexUpdateMonitor indexUpdateMonitor;
+
+      /**
+       * @see java.lang.Runnable#run()
+       */
+      public void run()
+      {
+         // TODO Auto-generated method stub
+         try
+         {
+            waiting = true;
+            assertTrue(indexUpdateMonitor.lock(lockName, LockType.WRITE));
+            waiting = false;
+         }
+         catch (InterruptedException e)
+         {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+         }
+      }
+   }
+
    private class UpdateMonitorChanger implements Runnable
    {
       private AtomicBoolean atomicBoolean;
@@ -196,13 +276,15 @@
 
    private Cache<Serializable, Object> createCache()
    {
-      String jbcConfig = "conf/standalone/test-jbosscache-config.xml";
+      String jbcConfig = "conf/cluster/test-jbosscache-indexer-config-exoloader_db1_ws.xml";
+
       CacheFactory<Serializable, Object> factory = new DefaultCacheFactory<Serializable, Object>();
       log.info("JBoss Cache configuration used: " + jbcConfig);
       Cache<Serializable, Object> cache = factory.createCache(jbcConfig, false);
 
       cache.create();
       cache.start();
+
       return cache;
    }
 



More information about the exo-jcr-commits mailing list