JBoss Cache SVN: r7929 - in searchable/trunk: src/main/java/org/jboss/cache/search and 1 other directories.
by jbosscache-commits@lists.jboss.org
Author: navssurtani
Date: 2009-03-19 14:08:54 -0400 (Thu, 19 Mar 2009)
New Revision: 7929
Modified:
searchable/trunk/pom.xml
searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java
searchable/trunk/src/main/java/org/jboss/cache/search/Transformer.java
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java
Log:
@NodeRemoved not being picked up from the test in the IndexingInTransaction.java
Modified: searchable/trunk/pom.xml
===================================================================
--- searchable/trunk/pom.xml 2009-03-19 17:06:05 UTC (rev 7928)
+++ searchable/trunk/pom.xml 2009-03-19 18:08:54 UTC (rev 7929)
@@ -2,140 +2,148 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <properties>
- <jbosscache-searchable-version>1.1.0-SNAPSHOT</jbosscache-searchable-version>
- <!-- By default only generate Javadocs when we package the module. -->
- <javadocPhase>package</javadocPhase>
- </properties>
+ <modelVersion>4.0.0</modelVersion>
+ <properties>
+ <jbosscache-searchable-version>1.1.0-SNAPSHOT</jbosscache-searchable-version>
+ <!-- By default only generate Javadocs when we package the module. -->
+ <javadocPhase>package</javadocPhase>
+ </properties>
- <parent>
- <groupId>org.jboss.cache</groupId>
- <artifactId>jbosscache-common-parent</artifactId>
- <version>1.4</version>
- </parent>
- <groupId>org.jboss.cache</groupId>
- <artifactId>jbosscache-searchable</artifactId>
- <version>${jbosscache-searchable-version}</version>
- <name>JBoss Cache - Searchable Edition</name>
- <description>JBoss Cache - Searchable Edition</description>
- <url>http://www.jbosscache.org</url>
- <packaging>jar</packaging>
- <dependencies>
- <dependency>
- <groupId>org.jboss.cache</groupId>
- <artifactId>jbosscache-core</artifactId>
- <version>3.0.3.GA</version>
- </dependency>
+ <parent>
+ <groupId>org.jboss.cache</groupId>
+ <artifactId>jbosscache-common-parent</artifactId>
+ <version>1.4</version>
+ </parent>
+ <groupId>org.jboss.cache</groupId>
+ <artifactId>jbosscache-searchable</artifactId>
+ <version>${jbosscache-searchable-version}</version>
+ <name>JBoss Cache - Searchable Edition</name>
+ <description>JBoss Cache - Searchable Edition</description>
+ <url>http://www.jbosscache.org</url>
+ <packaging>jar</packaging>
+ <dependencies>
+ <dependency>
+ <groupId>org.jboss.cache</groupId>
+ <artifactId>jbosscache-core</artifactId>
+ <version>3.0.3.GA</version>
+ </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-search</artifactId>
- <version>3.1.0.GA</version>
- </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-search</artifactId>
+ <version>3.1.0.GA</version>
+ </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- </dependency>
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <version>1.1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.cache</groupId>
+ <artifactId>jbosscache-pojo</artifactId>
+ <version>3.0.0.GA</version>
+ </dependency>
- <dependency>
- <groupId>org.jboss.cache</groupId>
- <artifactId>jbosscache-pojo</artifactId>
- <version>3.0.0.GA</version>
- </dependency>
+ <!-- Test dependencies -->
- <!-- Test dependencies -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.4.2</version>
+ <scope>test</scope>
+ </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.4.2</version>
- <scope>test</scope>
- </dependency>
+ <dependency>
+ <groupId>org.hibernate.apache.lucene.solr</groupId>
+ <artifactId>apache-solr-analyzer</artifactId>
+ <version>1.2.0</version>
+ <scope>test</scope>
+ </dependency>
- <dependency>
- <groupId>org.hibernate.apache.lucene.solr</groupId>
- <artifactId>apache-solr-analyzer</artifactId>
- <version>1.2.0</version>
- <scope>test</scope>
- </dependency>
+ <!-- Optional deps -->
+ <dependency>
+ <groupId>jdbm</groupId>
+ <artifactId>jdbm</artifactId>
+ <version>1.0</version>
+ <optional>true</optional>
+ </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.2-beta-1</version>
- <executions>
- <execution>
- <id>assemble</id>
- <phase>install</phase>
- <goals>
- <goal>attached</goal>
- </goals>
- <configuration>
- <descriptors>
- <descriptor>assembly/bin.xml</descriptor>
- <descriptor>assembly/all.xml</descriptor>
- </descriptors>
- <finalName>${artifactId}-${jbosscache-searchable-version}</finalName>
- <outputDirectory>target/distribution</outputDirectory>
- <workDirectory>target/assembly/work</workDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
- <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
- <mainClass>org.jboss.cache.search.Version</mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>build-test-jar</id>
- <goals>
- <goal>test-jar</goal>
- </goals>
- <configuration>
- <archive>
+
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <version>2.2-beta-1</version>
+ <executions>
+ <execution>
+ <id>assemble</id>
+ <phase>install</phase>
+ <goals>
+ <goal>attached</goal>
+ </goals>
+ <configuration>
+ <descriptors>
+ <descriptor>assembly/bin.xml</descriptor>
+ <descriptor>assembly/all.xml</descriptor>
+ </descriptors>
+ <finalName>${artifactId}-${jbosscache-searchable-version}</finalName>
+ <outputDirectory>target/distribution</outputDirectory>
+ <workDirectory>target/assembly/work</workDirectory>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <configuration>
+ <archive>
<manifest>
- <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
- <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
+ <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
+ <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
+ <mainClass>org.jboss.cache.search.Version</mainClass>
</manifest>
- </archive>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
+ </archive>
+ </configuration>
+ <executions>
+ <execution>
+ <id>build-test-jar</id>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ <configuration>
+ <archive>
+ <manifest>
+ <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
+ <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
+ </manifest>
+ </archive>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
- <!-- basic JBoss repository so that the common parent POM in jbosscache-support can be found -->
- <repositories>
- <repository>
- <id>repository.jboss.org</id>
- <url>http://repository.jboss.org/maven2</url>
- </repository>
- <repository>
- <id>snapshots.jboss.org</id>
- <url>http://snapshots.jboss.org/maven2</url>
- </repository>
- <!-- For Amazon S3 artifacts -->
- <repository>
- <id>e-xml.sourceforge.net</id>
- <url>http://e-xml.sourceforge.net/maven2/repository</url>
- </repository>
- </repositories>
+ <!-- basic JBoss repository so that the common parent POM in jbosscache-support can be found -->
+ <repositories>
+ <repository>
+ <id>repository.jboss.org</id>
+ <url>http://repository.jboss.org/maven2</url>
+ </repository>
+ <repository>
+ <id>snapshots.jboss.org</id>
+ <url>http://snapshots.jboss.org/maven2</url>
+ </repository>
+ <!-- For Amazon S3 artifacts -->
+ <repository>
+ <id>e-xml.sourceforge.net</id>
+ <url>http://e-xml.sourceforge.net/maven2/repository</url>
+ </repository>
+ </repositories>
</project>
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java 2009-03-19 17:06:05 UTC (rev 7928)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java 2009-03-19 18:08:54 UTC (rev 7929)
@@ -41,13 +41,13 @@
public CacheEntityId(String documentId)
{
super(documentId);
- if (log.isDebugEnabled()) log.debug("Called CEI constructor using documentID constructor. DocId is " + documentId);
+// if (log.isDebugEnabled()) log.debug("Called CEI constructor using documentID constructor. DocId is " + documentId);
}
public CacheEntityId(Fqn fqn, String key) throws InvalidKeyException
{
super(fqn);
- if (log.isDebugEnabled()) log.debug("Called CEI constructor using fqn, key constructor. Fqn & key are " + fqn + key);
+// if (log.isDebugEnabled()) log.debug("Called CEI constructor using fqn, key constructor. Fqn & key are " + fqn + key);
if(key == null) throw new NullPointerException("Key is null");
this.key = key;
this.documentId = Transformer.generateId(fqn, key);
@@ -89,7 +89,7 @@
public Fqn getFqn()
{
- if (log.isWarnEnabled()) log.warn("getFqn() called with fqn of " + fqn + " so my documentId should be " + documentId);
+// if (log.isWarnEnabled()) log.warn("getFqn() called with fqn of " + fqn + " so my documentId should be " + documentId);
return Transformer.getFqn(documentId);
}
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java 2009-03-19 17:06:05 UTC (rev 7928)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java 2009-03-19 18:08:54 UTC (rev 7929)
@@ -87,7 +87,7 @@
protected void handleNodeRemoved(NodeRemovedEvent event) throws InvalidKeyException
{
- if (log.isDebugEnabled()) log.debug("Entered the SCL. @NodeRemoved annotation found");
+ if (log.isWarnEnabled()) log.debug("Entered the SCL. @NodeRemoved annotation found");
if (!event.isPre())
{
// Directly going to handle the deletion of data.
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/Transformer.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/Transformer.java 2009-03-19 17:06:05 UTC (rev 7928)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/Transformer.java 2009-03-19 18:08:54 UTC (rev 7929)
@@ -84,15 +84,15 @@
// This will be the index of the first time the sub-String "Fqn=[" occurs within the whole String.
//Adding 1 so that the index being pointed at will be the first character in the Fqn sequence.
int startIndex = docId.indexOf("[") + 1;
- if(log.isWarnEnabled()) log.warn("startIndex is " + startIndex);
+// if(log.isWarnEnabled()) log.warn("startIndex is " + startIndex);
//The endIndex of the Fqn sequence so that we know when to cut out the sub-String.
int endIndex = docId.indexOf("]");
- if(log.isWarnEnabled()) log.warn("endIndex is " + endIndex);
+// if(log.isWarnEnabled()) log.warn("endIndex is " + endIndex);
String fqnString = docId.substring(startIndex, endIndex);
- if(log.isWarnEnabled()) log.warn("fqnString is" + fqnString);
+// if(log.isWarnEnabled()) log.warn("fqnString is" + fqnString);
Fqn fqn = Fqn.fromString(fqnString);
Modified: searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java
===================================================================
--- searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java 2009-03-19 17:06:05 UTC (rev 7928)
+++ searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java 2009-03-19 18:08:54 UTC (rev 7929)
@@ -103,8 +103,9 @@
searchableCache.createQuery(
Entity.searchByName("Name15")).list().isEmpty());
- node.removeChild(Fqn.fromString("Name15"));
+ searchableCache.removeNode(Fqn.fromString("Name15"));
+
List l = searchableCache.createQuery(Entity.searchByName("Name15")).list();
// bug: collection is not empty - there is null as there are inconsistencies in the indexes
15 years, 9 months
JBoss Cache SVN: r7928 - in searchable/trunk/src: test/java/org/jboss/cache/search/blackbox and 1 other directory.
by jbosscache-commits@lists.jboss.org
Author: navssurtani
Date: 2009-03-19 13:06:05 -0400 (Thu, 19 Mar 2009)
New Revision: 7928
Added:
searchable/trunk/src/main/java/org/jboss/cache/search/NodeRemovedTransactionContext.java
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java
Log:
Added NodeRemovedTransactionContext class ... should deal with indexes not being removed when @NodeRemoved is found
Added: searchable/trunk/src/main/java/org/jboss/cache/search/NodeRemovedTransactionContext.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/NodeRemovedTransactionContext.java (rev 0)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/NodeRemovedTransactionContext.java 2009-03-19 17:06:05 UTC (rev 7928)
@@ -0,0 +1,52 @@
+package org.jboss.cache.search;
+
+import org.hibernate.search.backend.TransactionContext;
+import org.jboss.cache.notifications.event.NodeRemovedEvent;
+
+import javax.transaction.Synchronization;
+import javax.transaction.Transaction;
+
+/**
+ * @author Navin Surtani (<a href="mailto:nsurtani@redhat.com">nsurtani(a)redhat.com</a>)
+ */
+public class NodeRemovedTransactionContext implements TransactionContext
+{
+
+ NodeRemovedEvent event;
+
+ public NodeRemovedTransactionContext(NodeRemovedEvent event)
+ {
+ if (event == null) throw new NullPointerException("event cannot be null");
+ this.event = event;
+ }
+
+ public boolean isTransactionInProgress()
+ {
+ return (event.getTransaction() != null);
+ }
+
+ public Object getTransactionIdentifier()
+ {
+ return event.getTransaction();
+ }
+
+ public void registerSynchronization(Synchronization synchronization)
+ {
+ if (synchronization == null) throw new NullPointerException("Synchronization passed in is null!");
+
+ Transaction transaction = event.getTransaction();
+ if (transaction != null)
+ {
+ try
+ {
+ transaction.registerSynchronization(synchronization);
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+
+
+ }
+ }
+}
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java 2009-03-19 15:47:58 UTC (rev 7927)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCoreListener.java 2009-03-19 17:06:05 UTC (rev 7928)
@@ -18,7 +18,7 @@
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
+ */
package org.jboss.cache.search;
@@ -28,7 +28,9 @@
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.jboss.cache.notifications.annotation.CacheListener;
import org.jboss.cache.notifications.annotation.NodeModified;
+import org.jboss.cache.notifications.annotation.NodeRemoved;
import org.jboss.cache.notifications.event.NodeModifiedEvent;
+import org.jboss.cache.notifications.event.NodeRemovedEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -36,8 +38,8 @@
/**
* @author Navin Surtani (<a href="mailto:nsurtani@redhat.com">nsurtani(a)redhat.com</a>)
- *
- * Listener class for changes made to the cache. This listener makes changes if it is a {@link org.jboss.cache.Cache} being used.
+ * <p/>
+ * Listener class for changes made to the cache. This listener makes changes if it is a {@link org.jboss.cache.Cache} being used.
*/
@CacheListener
public class SearchableCoreListener
@@ -47,7 +49,7 @@
public SearchableCoreListener(SearchFactoryImplementor searchFactory)
{
-
+
this.searchFactory = searchFactory;
}
@@ -57,7 +59,7 @@
* @param event that has occured - or a node that has been changed. {@link org.jboss.cache.notifications.event.NodeModifiedEvent}
* @throws InvalidKeyException if an invalid key is passed in.
*/
-
+
@NodeModified
public void updateLuceneIndexes(NodeModifiedEvent event) throws InvalidKeyException
{
@@ -80,11 +82,47 @@
}
}
- /**
+
+ @NodeRemoved
+ protected void handleNodeRemoved(NodeRemovedEvent event) throws InvalidKeyException
+ {
+
+ if (log.isDebugEnabled()) log.debug("Entered the SCL. @NodeRemoved annotation found");
+ if (!event.isPre())
+ {
+ // Directly going to handle the deletion of data.
+ Map dataMap = event.getData();
+ if (log.isTraceEnabled()) log.trace("Called event.getData() and saved to a Map");
+
+
+ TransactionContext ctx = new NodeRemovedTransactionContext(event);
+
+ for (Object key : dataMap.keySet())
+ {
+ CacheEntityId cacheEntityId = new CacheEntityId(event.getFqn(), (String) key);
+ if (log.isTraceEnabled()) log.trace("Created new CacheEntityId");
+
+ String fqnString = cacheEntityId.getFqn().toString(); // Vars for logging
+ String keyString = (String) key;
+
+
+ searchFactory.getWorker().performWork(new Work(dataMap.get(key), cacheEntityId.getDocumentId(), WorkType.DELETE), ctx);
+ searchFactory.getWorker().performWork(new Work(dataMap.get(key), cacheEntityId.getDocumentId(), WorkType.ADD), ctx);
+
+
+ if (log.isTraceEnabled())
+ {
+ log.debug("Added your object into Lucene with Fqn " + fqnString + " and key " + keyString);
+ }
+ }
+ }
+ }
+
+ /**
* If the modification type is PUT_MAP or PUT_DATA then this method will be called.
* Takes in the event as a parameter
*
- * @param event that has occured - or a node that has been changed. {@link org.jboss.cache.notifications.event.NodeModifiedEvent}
+ * @param event that has occured - or a node that has been changed. {@link org.jboss.cache.notifications.event.NodeModifiedEvent}
* @param searchFactory - a SearchFactoryImpl instance.
* @throws InvalidKeyException
*/
@@ -122,12 +160,11 @@
}
-
/**
* If the modification type is DELETE_DATA then this method will be called.
* Takes in the event as a parameter
*
- * @param event that has occured - or a node that has been changed. {@link org.jboss.cache.notifications.event.NodeModifiedEvent}
+ * @param event that has occured - or a node that has been changed. {@link org.jboss.cache.notifications.event.NodeModifiedEvent}
* @param searchFactory - a SearchFactoryImpl instance.
* @throws InvalidKeyException
*/
@@ -146,7 +183,8 @@
searchFactory.getWorker().performWork(new Work(dataMap.get(key), cacheEntityId.getDocumentId(), WorkType.DELETE), ctx);
- if (log.isTraceEnabled()) log.trace("Deleted your object from Lucene with Fqn " + fqnString + " and key " + keyString);
+ if (log.isTraceEnabled())
+ log.trace("Deleted your object from Lucene with Fqn " + fqnString + " and key " + keyString);
}
Added: searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java
===================================================================
--- searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java (rev 0)
+++ searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/IndexingInTransaction.java 2009-03-19 17:06:05 UTC (rev 7928)
@@ -0,0 +1,194 @@
+package org.jboss.cache.search.blackbox;
+
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.ProvidedId;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Store;
+import org.jboss.cache.*;
+import org.jboss.cache.loader.jdbm.JdbmCacheLoaderConfig;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.config.CacheLoaderConfig;
+import org.jboss.cache.search.SearchableCacheFactory;
+import org.jboss.cache.search.SearchableCache;
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeMethod;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.index.Term;
+
+import static org.testng.AssertJUnit.*;
+
+import java.util.Properties;
+import java.util.List;
+import java.io.Serializable;
+
+@Test(groups = {"unit"}, sequential = true)
+public class IndexingInTransaction
+{
+
+ private static final Fqn fname = Fqn.fromString("blah");
+
+ private Cache coreCache;
+
+ private SearchableCache searchableCache;
+
+ Node node;
+
+ public IndexingInTransaction()
+ {
+ init();
+ }
+
+ @BeforeMethod
+ public void init()
+ {
+ CacheFactory factory = new DefaultCacheFactory();
+ Configuration c = new Configuration();
+ c.setInvocationBatchingEnabled(true);
+
+ CacheLoaderConfig clc = new CacheLoaderConfig();
+
+ JdbmCacheLoaderConfig jclc = new JdbmCacheLoaderConfig();
+ jclc.setAsync(false);
+ jclc.setFetchPersistentState(false);
+ jclc.setIgnoreModifications(false);
+ jclc.setPurgeOnStartup(true);
+ jclc.setLocation("/tmp/c1");
+
+ clc.addIndividualCacheLoaderConfig(jclc);
+ c.setCacheLoaderConfig(clc);
+
+ coreCache = factory.createCache(c, false);
+
+ SearchableCacheFactory f = new SearchableCacheFactory();
+ Properties p = new Properties();
+ p.put("hibernate.search.default.indexBase", "/tmp/c1idx");
+ p.put("hibernate.search.worker.batch_size", "1");
+ searchableCache = f.createSearchableCache(coreCache, p, Entity.class);
+ searchableCache.create();
+ searchableCache.start();
+ node = searchableCache.getRoot().addChild(fname);
+ }
+
+ @Test
+ public void testPutEntitiesWithoutTransaction()
+ {
+ for (int i = 1; i <= 10; i++)
+ {
+ Entity e = getEntity(i);
+ System.out.println("caching: " + e);
+ Node c = node.addChild(Fqn.fromString(e.getName()));
+ c.put("" + e.getId(), e);
+ }
+ assertFalse(searchableCache.createQuery(
+ Entity.searchByName("Name5")).list().isEmpty());
+ }
+
+ @Test
+ public void testPutEntitiesWithTransaction() throws Exception
+ {
+ ((CacheSPI) (coreCache)).getTransactionManager().begin();
+ for (int i = 15; i <= 15; i++)
+ {
+ Entity e = getEntity(i);
+ System.out.println("caching: " + e);
+ Node c = node.addChild(Fqn.fromString(e.getName()));
+ c.put("" + e.getId(), e);
+ }
+ ((CacheSPI) (coreCache)).getTransactionManager().commit();
+
+ assertFalse("there should be record for the name15",
+ searchableCache.createQuery(
+ Entity.searchByName("Name15")).list().isEmpty());
+
+ node.removeChild(Fqn.fromString("Name15"));
+
+ List l = searchableCache.createQuery(Entity.searchByName("Name15")).list();
+
+ // bug: collection is not empty - there is null as there are inconsistencies in the indexes
+ assertTrue("there should be no record found", l.isEmpty());
+
+ }
+
+ Entity getEntity(long id)
+ {
+ return new Entity(id, "Name" + id, "Surname" + id, true);
+ }
+
+// public static void main(String[] args) {
+// IndexingInTransaction stb = new IndexingInTransaction();
+// stb.init();
+// }
+
+}
+
+@ProvidedId
+@Indexed
+class Entity implements Serializable
+{
+
+ public static final String IDX_NAME = "name";
+ public static final String IDX_SURNAME = "surname";
+
+ private long id;
+
+ @Field(store = Store.YES)
+ private String name;
+
+ @Field(store = Store.YES)
+ private String surname;
+
+ private boolean dead;
+
+ Entity(long id, String name, String surname, boolean dead)
+ {
+ this.id = id;
+ this.name = name;
+ this.surname = surname;
+ this.dead = dead;
+ }
+
+ public long getId()
+ {
+ return id;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getSurname()
+ {
+ return surname;
+ }
+
+ public boolean isDead()
+ {
+ return dead;
+ }
+
+ public static Query searchByName(String name)
+ {
+ BooleanQuery query = new BooleanQuery();
+ query.add(new TermQuery(
+ new Term(Entity.IDX_NAME,
+ name.toLowerCase())),
+ BooleanClause.Occur.MUST);
+ return query;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Entity{" +
+ "id=" + id +
+ ", name='" + name + '\'' +
+ ", surname='" + surname + '\'' +
+ ", dead=" + dead +
+ '}';
+ }
+
+}
\ No newline at end of file
15 years, 9 months
JBoss Cache SVN: r7927 - in searchable/trunk/src: test/java/org/jboss/cache/search and 1 other directories.
by jbosscache-commits@lists.jboss.org
Author: navssurtani
Date: 2009-03-19 11:47:58 -0400 (Thu, 19 Mar 2009)
New Revision: 7927
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java
searchable/trunk/src/main/java/org/jboss/cache/search/Transformer.java
searchable/trunk/src/test/java/org/jboss/cache/search/CacheEntityIdTest.java
searchable/trunk/src/test/java/org/jboss/cache/search/QueryResultIteratorImplTest.java
searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/LocalPOJOCacheTest.java
Log:
Disabled pojo tests ... core tests run okay
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java 2009-03-19 14:24:08 UTC (rev 7926)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/CacheEntityId.java 2009-03-19 15:47:58 UTC (rev 7927)
@@ -23,6 +23,8 @@
package org.jboss.cache.search;
import org.jboss.cache.Fqn;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
/**
*
@@ -32,18 +34,23 @@
*/
public class CacheEntityId extends PojoEntityId
{
+ private static final Log log = LogFactory.getLog(CacheEntityId.class);
+
String key;
public CacheEntityId(String documentId)
{
super(documentId);
+ if (log.isDebugEnabled()) log.debug("Called CEI constructor using documentID constructor. DocId is " + documentId);
}
- public CacheEntityId(Fqn fqn, String key)
+ public CacheEntityId(Fqn fqn, String key) throws InvalidKeyException
{
super(fqn);
+ if (log.isDebugEnabled()) log.debug("Called CEI constructor using fqn, key constructor. Fqn & key are " + fqn + key);
if(key == null) throw new NullPointerException("Key is null");
this.key = key;
+ this.documentId = Transformer.generateId(fqn, key);
}
/**
@@ -70,7 +77,6 @@
* @return documentId String.
*/
-
public String getDocumentId() throws InvalidKeyException
{
if (key == null || fqn == null)
@@ -83,6 +89,8 @@
public Fqn getFqn()
{
- throw new RuntimeException("implement me");
+ if (log.isWarnEnabled()) log.warn("getFqn() called with fqn of " + fqn + " so my documentId should be " + documentId);
+
+ return Transformer.getFqn(documentId);
}
}
Modified: searchable/trunk/src/main/java/org/jboss/cache/search/Transformer.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/Transformer.java 2009-03-19 14:24:08 UTC (rev 7926)
+++ searchable/trunk/src/main/java/org/jboss/cache/search/Transformer.java 2009-03-19 15:47:58 UTC (rev 7927)
@@ -23,6 +23,8 @@
package org.jboss.cache.search;
import org.jboss.cache.Fqn;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
/**
*
@@ -36,6 +38,8 @@
*/
public class Transformer
{
+ private static final Log log = LogFactory.getLog(Transformer.class);
+
/**
* Takes in the documentId string from the user and will return the key from the Fqn, key combination.
*
@@ -80,12 +84,17 @@
// This will be the index of the first time the sub-String "Fqn=[" occurs within the whole String.
//Adding 1 so that the index being pointed at will be the first character in the Fqn sequence.
int startIndex = docId.indexOf("[") + 1;
+ if(log.isWarnEnabled()) log.warn("startIndex is " + startIndex);
//The endIndex of the Fqn sequence so that we know when to cut out the sub-String.
int endIndex = docId.indexOf("]");
+ if(log.isWarnEnabled()) log.warn("endIndex is " + endIndex);
+
String fqnString = docId.substring(startIndex, endIndex);
+ if(log.isWarnEnabled()) log.warn("fqnString is" + fqnString);
+
Fqn fqn = Fqn.fromString(fqnString);
return fqn;
}
Modified: searchable/trunk/src/test/java/org/jboss/cache/search/CacheEntityIdTest.java
===================================================================
--- searchable/trunk/src/test/java/org/jboss/cache/search/CacheEntityIdTest.java 2009-03-19 14:24:08 UTC (rev 7926)
+++ searchable/trunk/src/test/java/org/jboss/cache/search/CacheEntityIdTest.java 2009-03-19 15:47:58 UTC (rev 7927)
@@ -13,14 +13,14 @@
{
@Test (expectedExceptions = NullPointerException.class)
- public void testNullFqn()
+ public void testNullFqn() throws InvalidKeyException
{
CacheEntityId nullFqn = new CacheEntityId(null, "key");
}
@Test (expectedExceptions = NullPointerException.class)
- public void testNullKey()
+ public void testNullKey() throws InvalidKeyException
{
CacheEntityId nullKey = new CacheEntityId(Fqn.fromString("/a/b/c"), null);
}
Modified: searchable/trunk/src/test/java/org/jboss/cache/search/QueryResultIteratorImplTest.java
===================================================================
--- searchable/trunk/src/test/java/org/jboss/cache/search/QueryResultIteratorImplTest.java 2009-03-19 14:24:08 UTC (rev 7926)
+++ searchable/trunk/src/test/java/org/jboss/cache/search/QueryResultIteratorImplTest.java 2009-03-19 15:47:58 UTC (rev 7927)
@@ -25,7 +25,7 @@
int fetchSize = 1;
@BeforeMethod
- public void setUp()
+ public void setUp() throws InvalidKeyException
{
// create a set of dummy cache entity IDs
ids = new ArrayList();
Modified: searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/LocalPOJOCacheTest.java
===================================================================
--- searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/LocalPOJOCacheTest.java 2009-03-19 14:24:08 UTC (rev 7926)
+++ searchable/trunk/src/test/java/org/jboss/cache/search/blackbox/LocalPOJOCacheTest.java 2009-03-19 15:47:58 UTC (rev 7927)
@@ -30,7 +30,7 @@
* @author Navin Surtani (<a href="mailto:nsurtani@redhat.com">nsurtani(a)redhat.com</a>)
*/
-@Test(groups = "functional", enabled = true)
+@Test(groups = "functional", enabled = false)
public class LocalPOJOCacheTest
{
SearchableCache searchableCache;
15 years, 9 months
JBoss Cache SVN: r7926 - core/branches.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-19 10:24:08 -0400 (Thu, 19 Mar 2009)
New Revision: 7926
Removed:
core/branches/flat/
Log:
Moved to its own repo
15 years, 9 months
JBoss Cache SVN: r7925 - in core/branches/flat/src: main/java/org/horizon/config/parsing and 5 other directories.
by jbosscache-commits@lists.jboss.org
Author: mircea.markus
Date: 2009-03-19 10:11:30 -0400 (Thu, 19 Mar 2009)
New Revision: 7925
Added:
core/branches/flat/src/main/java/org/horizon/jmx/ComponentGroupJmxRegistration.java
core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerCacheRegistration.java
core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerGlobalRegistration.java
core/branches/flat/src/test/java/org/horizon/jmx/GlobalComponentsRegitrationTest.java
Removed:
core/branches/flat/src/main/java/org/horizon/jmx/JmxRegistrationManager.java
core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerRegistration.java
Modified:
core/branches/flat/src/main/java/org/horizon/config/GlobalConfiguration.java
core/branches/flat/src/main/java/org/horizon/config/parsing/XmlConfigurationParserImpl.java
core/branches/flat/src/main/java/org/horizon/factories/DefaultCacheFactory.java
core/branches/flat/src/main/java/org/horizon/factories/GlobalComponentRegistry.java
core/branches/flat/src/test/java/org/horizon/config/parsing/XmlFileParsingTest.java
core/branches/flat/src/test/java/org/horizon/jmx/JmxRegistrationManagerTest.java
core/branches/flat/src/test/java/org/horizon/test/TestingUtil.java
Log:
ongoing JMX integration
Modified: core/branches/flat/src/main/java/org/horizon/config/GlobalConfiguration.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/config/GlobalConfiguration.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/main/java/org/horizon/config/GlobalConfiguration.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -51,8 +51,40 @@
private GlobalComponentRegistry gcr;
private long distributedSyncTimeout = 60000; // default
+ private boolean exposeGlobalManagementStatistics = true;
+ private String jmxDomain;
+
+ public boolean isExposeGlobalManagementStatistics() {
+ return exposeGlobalManagementStatistics;
+ }
+
+ public void setExposeGlobalManagementStatistics(boolean exposeGlobalManagementStatistics) {
+ testImmutability("exposeGlobalManagementStatistics");
+ this.exposeGlobalManagementStatistics = exposeGlobalManagementStatistics;
+ }
+
/**
+ * If JMX statistics are enabled then all 'published' JMX objects will appear under this name. This is optional, if
+ * not specified an object name will be created for you by default.
+ *
+ * @see javax.management.ObjectName
+ * @see #isExposeManagementStatistics()
+ */
+ public void setJmxDomain(String jmxObjectName) {
+ testImmutability("jmxNameBase");
+ this.jmxDomain = jmxObjectName;
+ }
+
+ /**
+ * @see #setJmxDomain(String)
+ */
+ public String getJmxDomain() {
+ return jmxDomain;
+ }
+
+
+ /**
* Behavior of the JVM shutdown hook registered by the cache
*/
public static enum ShutdownHookBehavior {
Modified: core/branches/flat/src/main/java/org/horizon/config/parsing/XmlConfigurationParserImpl.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/config/parsing/XmlConfigurationParserImpl.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/main/java/org/horizon/config/parsing/XmlConfigurationParserImpl.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -293,7 +293,8 @@
}
String jmxNameBase = getAttributeValue(element, "jmxNameBase");
if (existsAttribute(jmxNameBase)) {
- config.setJmxNameBase(jmxNameBase);
+ //todo update this
+// config.setJmxNameBase(jmxNameBase);
}
}
}
Modified: core/branches/flat/src/main/java/org/horizon/factories/DefaultCacheFactory.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/factories/DefaultCacheFactory.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/main/java/org/horizon/factories/DefaultCacheFactory.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -26,7 +26,7 @@
import org.horizon.CacheDelegate;
import org.horizon.config.Configuration;
import org.horizon.config.ConfigurationException;
-import org.horizon.jmx.PlatformMBeanServerRegistration;
+import org.horizon.jmx.PlatformMBeanServerCacheRegistration;
import org.horizon.manager.DefaultCacheManager;
/**
@@ -87,7 +87,7 @@
componentRegistry = new ComponentRegistry(cacheName, configuration, spi, globalComponentRegistry);
componentRegistry.registerDefaultClassLoader(defaultClassLoader);
componentRegistry.registerComponent(spi, AdvancedCache.class);
- componentRegistry.registerComponent(new PlatformMBeanServerRegistration(), PlatformMBeanServerRegistration.class);
+ componentRegistry.registerComponent(new PlatformMBeanServerCacheRegistration(), PlatformMBeanServerCacheRegistration.class);
}
/**
Modified: core/branches/flat/src/main/java/org/horizon/factories/GlobalComponentRegistry.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/factories/GlobalComponentRegistry.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/main/java/org/horizon/factories/GlobalComponentRegistry.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -7,6 +7,7 @@
import org.horizon.factories.annotations.NonVolatile;
import org.horizon.factories.scopes.Scope;
import org.horizon.factories.scopes.Scopes;
+import org.horizon.jmx.PlatformMBeanServerGlobalRegistration;
import org.horizon.logging.Log;
import org.horizon.logging.LogFactory;
import org.horizon.manager.CacheManager;
@@ -54,6 +55,7 @@
registerComponent(this, GlobalComponentRegistry.class);
registerComponent(cacheManager, CacheManager.class);
registerComponent(configuration, GlobalConfiguration.class);
+ registerComponent(new PlatformMBeanServerGlobalRegistration(), PlatformMBeanServerGlobalRegistration.class);
}
catch (Exception e) {
throw new CacheException("Unable to construct a GlobalComponentRegistry!", e);
Copied: core/branches/flat/src/main/java/org/horizon/jmx/ComponentGroupJmxRegistration.java (from rev 7896, core/branches/flat/src/main/java/org/horizon/jmx/JmxRegistrationManager.java)
===================================================================
--- core/branches/flat/src/main/java/org/horizon/jmx/ComponentGroupJmxRegistration.java (rev 0)
+++ core/branches/flat/src/main/java/org/horizon/jmx/ComponentGroupJmxRegistration.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -0,0 +1,150 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2000 - 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.horizon.jmx;
+
+import org.horizon.CacheException;
+import org.horizon.factories.AbstractComponentRegistry;
+import org.horizon.factories.ComponentRegistry;
+import org.horizon.logging.Log;
+import org.horizon.logging.LogFactory;
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Registers all the <b>MBean</b>s from an <b>Cache</b> instance to a <b>MBeanServer</b>. It iterates over all the
+ * components within <b>ComponentRegistry</b> and registers all the components annotated with <b>ManagedAttribute</b>,
+ * <b>ManagedOperation</b> or/and <b>MBean</b>. If no <b>MBean</b> server is provided, then the {@link
+ * java.lang.management.ManagementFactory#getPlatformMBeanServer()} is being used. <p/> It is immutable: both cache
+ * instance and MBeanServer are being passed as arguments to the constructor. <p /> <p> Note that by default object
+ * names used are prefixed with <tt>jboss.cache:service=Horizon</tt>. While this format works for and is consistent
+ * with JBoss AS and the JMX console, it has been known to cause problems with other JMX servers such as Websphere. To
+ * work around this, you can provide the following VM system property to override this prefix with a prefix of your
+ * choice: <tt><b>-Dhorizon.jmx.prefix=Horizon</b></tt> </p>
+ *
+ * @author Mircea.Markus(a)jboss.com
+ * @since 1.0
+ */
+public class ComponentGroupJmxRegistration {
+
+ private static final Log log = LogFactory.getLog(ComponentGroupJmxRegistration.class);
+
+ private MBeanServer mBeanServer;
+
+ private String jmxDomain = "horizon:";
+ private String groupName;
+
+ private Set<AbstractComponentRegistry.Component> components;
+
+ public static final String CACHE_NAME_KEY = "cache-name";
+ public static final String JMX_RESOURCE_KEY = "jmx-resource";
+
+ /**
+ * C-tor.
+ *
+ * @param mBeanServer the server where mbeans are being registered
+ * @param cache cache that needs to be monitored
+ * @param objectNameBase path in the MBeanServer where to register cache MBeans
+ * @see java.lang.management.ManagementFactory#getPlatformMBeanServer()
+ * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/management/mxbeans.html#mbean_s...">platform
+ * MBeanServer</a>
+ */
+ public ComponentGroupJmxRegistration(MBeanServer mBeanServer, Set<AbstractComponentRegistry.Component> components, String groupName) {
+ this.mBeanServer = mBeanServer;
+ this.components = components;
+ this.groupName = groupName;
+ }
+
+ public void setJmxDomain(String jmxDomain) {
+ this.jmxDomain = jmxDomain;
+ }
+
+ /**
+ * Performs the MBean registration.
+ */
+ public void registerMBeans() throws CacheException {
+ try {
+ List<ResourceDMBean> resourceDMBeans = getResourceDMBeansFromComponents();
+ for (ResourceDMBean resource : resourceDMBeans) {
+ String resourceName = resource.getObjectName();
+ ObjectName objectName = new ObjectName(getObjectName(resourceName));
+ if (!mBeanServer.isRegistered(objectName)) {
+ try {
+ mBeanServer.registerMBean(resource, objectName);
+ } catch (InstanceAlreadyExistsException e) {
+ //this might happen if multiple instances are trying to concurrently register same objectName
+ log.info("Could not register object with name:" + objectName + "(" + e.getMessage() + ")");
+ }
+ } else {
+ if (log.isInfoEnabled())
+ log.info("Could not register object with name: " + objectName);
+ }
+ }
+ }
+ catch (Exception e) {
+ throw new CacheException("Failure while registering mbeans", e);
+ }
+ }
+
+ /**
+ * Unregisters all the MBeans registered through {@link #registerMBeans()}.
+ */
+ public void unregisterCacheMBeans() throws CacheException {
+ log.trace("Unregistering jmx resources..");
+ try {
+ List<ResourceDMBean> resourceDMBeans = getResourceDMBeansFromComponents();
+ for (ResourceDMBean resource : resourceDMBeans) {
+ String resourceName = resource.getObject().getClass().getSimpleName();
+ ObjectName objectName = new ObjectName(getObjectName(resourceName));
+ if (mBeanServer.isRegistered(objectName)) {
+ mBeanServer.unregisterMBean(objectName);
+ }
+ }
+ }
+ catch (Exception e) {
+ throw new CacheException("Failure while unregistering mbeans", e);
+ }
+ }
+
+ private List<ResourceDMBean> getResourceDMBeansFromComponents() {
+ List<ResourceDMBean> resourceDMBeans = new ArrayList<ResourceDMBean>();
+ for (ComponentRegistry.Component component : components) {
+ ResourceDMBean resourceDMBean = new ResourceDMBean(component.getInstance());
+ if (resourceDMBean.isManagedResource()) {
+ resourceDMBeans.add(resourceDMBean);
+ }
+ }
+ return resourceDMBeans;
+ }
+
+ public String getObjectName(String resourceName) {
+ return getObjectName(jmxDomain, groupName, resourceName);
+ }
+
+ public static String getObjectName(String jmxDomain, String groupName, String resourceName) {
+ return jmxDomain + CACHE_NAME_KEY + "=" + groupName + "," + JMX_RESOURCE_KEY + "=" + resourceName;
+ }
+}
Property changes on: core/branches/flat/src/main/java/org/horizon/jmx/ComponentGroupJmxRegistration.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Deleted: core/branches/flat/src/main/java/org/horizon/jmx/JmxRegistrationManager.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/jmx/JmxRegistrationManager.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/main/java/org/horizon/jmx/JmxRegistrationManager.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -1,164 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2000 - 2008, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.horizon.jmx;
-
-import org.horizon.AdvancedCache;
-import org.horizon.CacheException;
-import org.horizon.factories.ComponentRegistry;
-import org.horizon.logging.Log;
-import org.horizon.logging.LogFactory;
-
-import javax.management.InstanceAlreadyExistsException;
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-import java.lang.management.ManagementFactory;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Registers all the <b>MBean</b>s from an <b>Cache</b> instance to a <b>MBeanServer</b>. It iterates over all the
- * components within <b>ComponentRegistry</b> and registers all the components annotated with <b>ManagedAttribute</b>,
- * <b>ManagedOperation</b> or/and <b>MBean</b>. If no <b>MBean</b> server is provided, then the {@link
- * java.lang.management.ManagementFactory#getPlatformMBeanServer()} is being used. <p/> It is immutable: both cache
- * instance and MBeanServer are being passed as arguments to the constructor. <p /> <p> Note that by default object
- * names used are prefixed with <tt>jboss.cache:service=Horizon</tt>. While this format works for and is consistent
- * with JBoss AS and the JMX console, it has been known to cause problems with other JMX servers such as Websphere. To
- * work around this, you can provide the following VM system property to override this prefix with a prefix of your
- * choice: <tt><b>-Dhorizon.jmx.prefix=Horizon</b></tt> </p>
- *
- * @author Mircea.Markus(a)jboss.com
- * @since 1.0
- */
-public class JmxRegistrationManager {
-
- private static final Log log = LogFactory.getLog(JmxRegistrationManager.class);
-
- private MBeanServer mBeanServer;
-
- private String objectNameBase;
-
- private AdvancedCache cache;
- public static final String CACHE_NAME_KEY = "cache-name";
- public static final String JMX_RESOURCE_KEY = "jmx-resource";
-
- /**
- * C-tor.
- *
- * @param mBeanServer the server where mbeans are being registered
- * @param cache cache that needs to be monitored
- * @param objectNameBase path in the MBeanServer where to register cache MBeans
- */
- public JmxRegistrationManager(MBeanServer mBeanServer, AdvancedCache cache) {
- this.mBeanServer = mBeanServer;
- this.cache = cache;
- processBaseName();
- }
-
- /**
- * Defaults to platform to platform MBeanServer.
- *
- * @see java.lang.management.ManagementFactory#getPlatformMBeanServer()
- * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/management/mxbeans.html#mbean_s...">platform
- * MBeanServer</a>
- */
- public JmxRegistrationManager(AdvancedCache cache) {
- this(ManagementFactory.getPlatformMBeanServer(), cache);
- }
-
- /**
- * Performs the MBean registration.
- */
- public void registerAllMBeans() throws CacheException {
- try {
- List<ResourceDMBean> resourceDMBeans = getResourceDMBeans();
- for (ResourceDMBean resource : resourceDMBeans) {
- String resourceName = resource.getObjectName();
- ObjectName objectName = new ObjectName(getObjectName(resourceName));
- if (!mBeanServer.isRegistered(objectName)) {
- try {
- mBeanServer.registerMBean(resource, objectName);
- } catch (InstanceAlreadyExistsException e) {
- //this might happen if multiple instances are trying to concurrently register same objectName
- log.info("Could not register object with name:" + objectName + "(" + e.getMessage() + ")");
- }
- } else {
- if (log.isInfoEnabled())
- log.info("Could not register object with name: " + objectName);
- }
- }
- }
- catch (Exception e) {
- throw new CacheException("Failure while registering mbeans", e);
- }
- }
-
- /**
- * Unregisters all the MBeans registered through {@link #registerAllMBeans()}.
- */
- public void unregisterAllMBeans() throws CacheException {
- log.trace("Unregistering jmx resources..");
- try {
- List<ResourceDMBean> resourceDMBeans = getResourceDMBeans();
- for (ResourceDMBean resource : resourceDMBeans) {
- String resourceName = resource.getObject().getClass().getSimpleName();
- ObjectName objectName = new ObjectName(getObjectName(resourceName));
- if (mBeanServer.isRegistered(objectName)) {
- mBeanServer.unregisterMBean(objectName);
- }
- }
- }
- catch (Exception e) {
- throw new CacheException("Failure while unregistering mbeans", e);
- }
- }
-
-
- private List<ResourceDMBean> getResourceDMBeans() {
- List<ResourceDMBean> resourceDMBeans = new ArrayList<ResourceDMBean>();
- for (ComponentRegistry.Component component : cache.getComponentRegistry().getRegisteredComponents()) {
- ResourceDMBean resourceDMBean = new ResourceDMBean(component.getInstance());
- if (resourceDMBean.isManagedResource()) {
- resourceDMBeans.add(resourceDMBean);
- }
- }
- return resourceDMBeans;
- }
-
- private void processBaseName() {
- String base = cache.getConfiguration().getJmxNameBase();
- if (base != null) {
- objectNameBase = base;
- if (log.isTraceEnabled()) log.trace("Using custom base name: '" + base + "'");
- return;
- }
- objectNameBase = "horizon:" + CACHE_NAME_KEY + "=";
- objectNameBase += cache.getName() + "(" + cache.getConfiguration().getCacheModeString().toLowerCase() + ")";
- }
-
- public String getObjectName(String resourceName) {
- return objectNameBase + "," + JMX_RESOURCE_KEY + "=" + resourceName;
- }
-
- public String getObjectNameBase() {
- return objectNameBase;
- }
-}
Copied: core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerCacheRegistration.java (from rev 7896, core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerRegistration.java)
===================================================================
--- core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerCacheRegistration.java (rev 0)
+++ core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerCacheRegistration.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2000 - 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.horizon.jmx;
+
+import org.horizon.AdvancedCache;
+import org.horizon.config.Configuration;
+import org.horizon.config.GlobalConfiguration;
+import org.horizon.factories.AbstractComponentRegistry;
+import org.horizon.factories.annotations.Inject;
+import org.horizon.factories.annotations.NonVolatile;
+import org.horizon.factories.annotations.Start;
+import org.horizon.factories.annotations.Stop;
+import org.horizon.logging.Log;
+import org.horizon.logging.LogFactory;
+
+import java.lang.management.ManagementFactory;
+import java.util.Set;
+
+/**
+ * If {@link Configuration#isExposeManagementStatistics()} is true, then class will register all the MBeans from the
+ * ConfigurationRegistry to the pltform MBean server.
+ * <p/>
+ * Note: to enable platform MBeanServer the following system property should be passet to the Sun JVM:
+ * <b>-Dcom.sun.management.jmxremote</b>.
+ *
+ * @author Mircea.Markus(a)jboss.com
+ * @see java.lang.management.ManagementFactory#getPlatformMBeanServer()
+ * @since 1.0
+ */
+@NonVolatile
+public class PlatformMBeanServerCacheRegistration {
+ private static final Log log = LogFactory.getLog(PlatformMBeanServerCacheRegistration.class);
+
+ private AdvancedCache cache;
+
+ @Inject
+ public void initialize(AdvancedCache cache) {
+ this.cache = cache;
+ }
+
+ /**
+ * Here is where the registration is being performed.
+ */
+ @Start(priority = 14)
+ public void registerToPlatformMBeanServer() {
+ if (cache == null)
+ throw new IllegalStateException("The cache should had been injected before a call to this method");
+ Configuration config = cache.getConfiguration();
+ if (config.isExposeManagementStatistics()) {
+ ComponentGroupJmxRegistration registrator = buildRegistrator();
+ registrator.registerMBeans();
+ log.info("MBeans were successfully registered to the platform mbean server.");
+ }
+ }
+
+ private ComponentGroupJmxRegistration buildRegistrator() {
+ Set<AbstractComponentRegistry.Component> components = cache.getComponentRegistry().getRegisteredComponents();
+ ComponentGroupJmxRegistration registrator = new ComponentGroupJmxRegistration(ManagementFactory.getPlatformMBeanServer(), components, getGroupName());
+ GlobalConfiguration globalConfiguration = cache.getConfiguration().getGlobalConfiguration();
+ if (globalConfiguration.getJmxDomain() != null) {
+ registrator.setJmxDomain(globalConfiguration.getJmxDomain());
+ }
+ return registrator;
+ }
+
+ private String getGroupName() {
+ return cache.getName() + "(" + cache.getConfiguration().getCacheModeString().toLowerCase() + ")";
+ }
+
+ /**
+ * Unregister when the cache is being stoped.
+ */
+ @Stop
+ public void unregisterMBeans() {
+ //this method might get called several times.
+ // After the first call the cache will become null, so we guard this
+ if (cache == null) return;
+ Configuration config = cache.getConfiguration();
+ if (config.isExposeManagementStatistics()) {
+ ComponentGroupJmxRegistration componentGroupJmxRegistration = buildRegistrator();
+ componentGroupJmxRegistration.unregisterCacheMBeans();
+ log.trace("MBeans were successfully unregistered from the platform mbean server.");
+ }
+ cache = null;
+ }
+}
Property changes on: core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerCacheRegistration.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerGlobalRegistration.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerGlobalRegistration.java (rev 0)
+++ core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerGlobalRegistration.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -0,0 +1,72 @@
+package org.horizon.jmx;
+
+import org.horizon.config.GlobalConfiguration;
+import org.horizon.factories.AbstractComponentRegistry;
+import org.horizon.factories.GlobalComponentRegistry;
+import org.horizon.factories.annotations.Inject;
+import org.horizon.factories.annotations.NonVolatile;
+import org.horizon.factories.annotations.Start;
+import org.horizon.factories.annotations.Stop;
+
+import javax.management.MBeanServer;
+import java.lang.management.ManagementFactory;
+import java.util.Set;
+
+/**
+ * // TODO: Mircea: Document this!
+ *
+ * @author
+ */
+@NonVolatile
+public class PlatformMBeanServerGlobalRegistration {
+
+ public static final String GLOBAL_JMX_GROUP = "[global]";
+ private GlobalComponentRegistry registry;
+ private GlobalConfiguration globalConfiguration;
+ private MBeanServer mBeanServer;
+
+ @Inject
+ public void init(GlobalComponentRegistry registry, GlobalConfiguration configuration) {
+ this.registry = registry;
+ this.globalConfiguration = configuration;
+ }
+
+ @Start(priority = 20)
+ public void start() {
+ if (globalConfiguration.isExposeGlobalManagementStatistics()) {
+ ComponentGroupJmxRegistration registrator = buildRegistrator();
+ registrator.registerMBeans();
+ }
+ }
+
+ public void setMBeanServer(MBeanServer mBeanServer) {
+ this.mBeanServer = mBeanServer;
+ }
+
+ @Stop
+ public void stop() {
+ //this method might get called several times.
+ // After the first call the cache will become null, so we guard this
+ if (registry == null) return;
+ if (globalConfiguration.isExposeGlobalManagementStatistics()) {
+ ComponentGroupJmxRegistration componentGroupJmxRegistration = buildRegistrator();
+ componentGroupJmxRegistration.unregisterCacheMBeans();
+ }
+ registry = null;
+ }
+
+ private ComponentGroupJmxRegistration buildRegistrator() {
+ Set<AbstractComponentRegistry.Component> components = registry.getRegisteredComponents();
+ MBeanServer platformMBeanServer;
+ if (this.mBeanServer != null) {
+ platformMBeanServer = this.mBeanServer;
+ } else {
+ platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
+ }
+ ComponentGroupJmxRegistration registrator = new ComponentGroupJmxRegistration(platformMBeanServer, components, GLOBAL_JMX_GROUP);
+ if (globalConfiguration.getJmxDomain() != null) {
+ registrator.setJmxDomain(globalConfiguration.getJmxDomain());
+ }
+ return registrator;
+ }
+}
Property changes on: core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerGlobalRegistration.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Deleted: core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerRegistration.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerRegistration.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/main/java/org/horizon/jmx/PlatformMBeanServerRegistration.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -1,86 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2000 - 2008, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.horizon.jmx;
-
-import org.horizon.AdvancedCache;
-import org.horizon.config.Configuration;
-import org.horizon.factories.annotations.Inject;
-import org.horizon.factories.annotations.NonVolatile;
-import org.horizon.factories.annotations.Start;
-import org.horizon.factories.annotations.Stop;
-import org.horizon.logging.Log;
-import org.horizon.logging.LogFactory;
-
-/**
- * If {@link Configuration#isExposeManagementStatistics()} is true, then class will register all the MBeans from the
- * ConfigurationRegistry to the pltform MBean server.
- * <p/>
- * Note: to enable platform MBeanServer the following system property should be passet to the Sun JVM:
- * <b>-Dcom.sun.management.jmxremote</b>.
- *
- * @author Mircea.Markus(a)jboss.com
- * @see java.lang.management.ManagementFactory#getPlatformMBeanServer()
- * @since 1.0
- */
-@NonVolatile
-public class PlatformMBeanServerRegistration {
- private static final Log log = LogFactory.getLog(PlatformMBeanServerRegistration.class);
-
- private AdvancedCache cache;
-
- @Inject
- public void initialize(AdvancedCache cache) {
- this.cache = cache;
- }
-
- /**
- * Here is where the registration is being performed.
- */
- @Start(priority = 14)
- public void registerToPlatformMBeanServer() {
- if (cache == null)
- throw new IllegalStateException("The cache should had been injected before a call to this method");
- Configuration config = cache.getConfiguration();
- if (config.isExposeManagementStatistics()) {
- JmxRegistrationManager jmxRegistrationManager = new JmxRegistrationManager(cache);
- jmxRegistrationManager.registerAllMBeans();
- log.info("MBeans were successfully registered to the platform mbean server.");
- }
- }
-
- /**
- * Unregister when the cache is being stoped.
- */
- @Stop
- public void unregisterMBeans() {
- //this method might get called several times.
- // After the first call the cache will become null, so we guard this
- if (cache == null) return;
- Configuration config = cache.getConfiguration();
- if (config.isExposeManagementStatistics()) {
- JmxRegistrationManager jmxRegistrationManager = new JmxRegistrationManager(cache);
- jmxRegistrationManager.unregisterAllMBeans();
- log.trace("MBeans were successfully unregistered from the platform mbean server.");
- }
- cache = null;
- }
-}
Modified: core/branches/flat/src/test/java/org/horizon/config/parsing/XmlFileParsingTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/config/parsing/XmlFileParsingTest.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/test/java/org/horizon/config/parsing/XmlFileParsingTest.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -112,9 +112,10 @@
assert tableManipulation.isDropTableOnExit();
assert !tableManipulation.isCreateTableOnStart();
- c = namedCaches.get("withJmxEnabled");
- assert c.isExposeManagementStatistics();
- assert c.getJmxNameBase().equals("horizonDomain:aKey=aValue");
+ //todo mmarkus re-enable test
+// c = namedCaches.get("withJmxEnabled");
+// assert c.isExposeManagementStatistics();
+// assert c.getJmxNameBase().equals("horizonDomain:aKey=aValue");
}
public void testConfigurationMerging() throws IOException {
Added: core/branches/flat/src/test/java/org/horizon/jmx/GlobalComponentsRegitrationTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/jmx/GlobalComponentsRegitrationTest.java (rev 0)
+++ core/branches/flat/src/test/java/org/horizon/jmx/GlobalComponentsRegitrationTest.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -0,0 +1,123 @@
+package org.horizon.jmx;
+
+import org.horizon.config.Configuration;
+import org.horizon.config.GlobalConfiguration;
+import org.horizon.factories.GlobalComponentRegistry;
+import org.horizon.manager.CacheManager;
+import org.horizon.test.TestingUtil;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.MBeanInfo;
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanOperationInfo;
+import java.lang.management.ManagementFactory;
+
+/**
+ * // TODO: Mircea: Document this!
+ *
+ * @author Mircea.Markus(a)jboss.com
+ */
+@Test(groups = "functional", testName = "jmx.GlobalComponentsRegitrationTest")
+public class GlobalComponentsRegitrationTest {
+
+ private MBeanServer mBeanServer;
+ private CacheManager cacheManager;
+ private PlatformMBeanServerGlobalRegistration registration;
+ private static final String JMX_NAME_BASE = "horizion-test:";
+
+ @BeforeMethod
+ public void setUp() {
+// mBeanServer = MBeanServerFactory.createMBeanServer();
+ mBeanServer = ManagementFactory.getPlatformMBeanServer();
+ GlobalConfiguration globalConfiguration = TestingUtil.getGlobalConfiguration();
+ globalConfiguration.setExposeGlobalManagementStatistics(false);
+ cacheManager = TestingUtil.createClusteredCacheManager(globalConfiguration);
+ cacheManager.start();
+
+ Configuration config = new Configuration();
+ config.setCacheMode(Configuration.CacheMode.REPL_SYNC);
+ config.setExposeManagementStatistics(false);
+ cacheManager.defineCache("a", config);
+ cacheManager.getCache("a");
+ registration = new PlatformMBeanServerGlobalRegistration();
+ registration.setMBeanServer(mBeanServer);
+
+ GlobalComponentRegistry componentRegistry = TestingUtil.extractGlobalComponentRegistry(cacheManager);
+ GlobalConfiguration configuration = GlobalConfiguration.getClusteredDefault();
+ configuration.setJmxDomain(JMX_NAME_BASE);
+ registration.init(componentRegistry, configuration);
+ registration.start();
+ }
+
+ @AfterMethod
+ public void tearDown() {
+ TestingUtil.killCacheManagers(cacheManager);
+ MBeanServerFactory.releaseMBeanServer(mBeanServer);
+ registration.stop();
+ }
+
+ public void testRpcManagerAttributes() throws Exception {
+ registration = new PlatformMBeanServerGlobalRegistration();
+ ObjectName rpcManagerObjectName = getObjectName("RPCManager");
+ assert mBeanServer.isRegistered(rpcManagerObjectName);
+ MBeanInfo beanInfo = mBeanServer.getMBeanInfo(rpcManagerObjectName);
+ assert attributeExists(beanInfo, "ReplicationCount");
+ assert attributeExists(beanInfo, "ReplicationFailures");
+ assert attributeExists(beanInfo, "StatisticsEnabled");
+ assert attributeExists(beanInfo, "SuccessRatio");
+
+ assert operationExists(beanInfo, "resetStatistics");
+ }
+
+ private boolean operationExists(MBeanInfo beanInfo, String opName) {
+ MBeanOperationInfo[] beanOperationInfos = beanInfo.getOperations();
+ for (MBeanOperationInfo attributeInfo: beanOperationInfos) {
+ if (attributeInfo.getName().equals(opName)) {
+ return true;
+ }
+ }
+ return false;
+
+ }
+
+ private boolean attributeExists(MBeanInfo beanInfo, String attrName) {
+ for (MBeanAttributeInfo attributeInfo: beanInfo.getAttributes()) {
+ if (attributeInfo.getName().equals(attrName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ //todo mmarkus enable test
+ @Test(enabled = false)
+ public void testCacheMgmtInterceptor() throws Exception {
+ ObjectName interceptor = getObjectName("CacheMgmtInterceptor");
+// sleepForever();
+ assert mBeanServer.isRegistered(interceptor);
+ }
+
+ private void sleepForever() {
+ while (true) {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private ObjectName getObjectName(String name) {
+ try {
+ return new ObjectName(ComponentGroupJmxRegistration.getObjectName(JMX_NAME_BASE,PlatformMBeanServerGlobalRegistration.GLOBAL_JMX_GROUP, name));
+ } catch (MalformedObjectNameException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+}
Property changes on: core/branches/flat/src/test/java/org/horizon/jmx/GlobalComponentsRegitrationTest.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: core/branches/flat/src/test/java/org/horizon/jmx/JmxRegistrationManagerTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/jmx/JmxRegistrationManagerTest.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/test/java/org/horizon/jmx/JmxRegistrationManagerTest.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -4,6 +4,7 @@
import org.horizon.Cache;
import org.horizon.config.Configuration;
import org.horizon.config.GlobalConfiguration;
+import org.horizon.factories.AbstractComponentRegistry;
import org.horizon.interceptors.CacheMgmtInterceptor;
import org.horizon.manager.CacheManager;
import org.horizon.manager.DefaultCacheManager;
@@ -17,9 +18,10 @@
import javax.management.ObjectName;
import java.util.ArrayList;
import java.util.List;
+import java.util.Set;
/**
- * Tester class for {@link JmxRegistrationManager}.
+ * Tester class for {@link ComponentGroupJmxRegistration}.
*
* @author Mircea.Markus(a)jboss.com
* @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
@@ -56,18 +58,24 @@
cm.defineCache("first", configuration);
Cache first = cm.getCache("first");
- JmxRegistrationManager regManager = new JmxRegistrationManager(mBeanServer, (AdvancedCache) first);
- regManager.registerAllMBeans();
- String name = regManager.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
+ ComponentGroupJmxRegistration regComponentGroup = buildRegistrator(first);
+ regComponentGroup.registerMBeans();
+ String name = regComponentGroup.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
ObjectName name1 = new ObjectName(name);
assert mBeanServer.isRegistered(name1);
- regManager.unregisterAllMBeans();
+ regComponentGroup.unregisterCacheMBeans();
assert !mBeanServer.isRegistered(name1);
assertCorrectJmxName(name1, first);
}
+ private ComponentGroupJmxRegistration buildRegistrator(Cache cache) {
+ AdvancedCache ac = (AdvancedCache) cache;
+ Set<AbstractComponentRegistry.Component> components = ac.getComponentRegistry().getRegisteredComponents();
+ return new ComponentGroupJmxRegistration(mBeanServer, components, cache.getName());
+ }
+
public void testRegisterReplicatedCache() throws Exception {
- GlobalConfiguration globalConfiguration = GlobalConfiguration.getClusteredDefault();
+ GlobalConfiguration globalConfiguration = TestingUtil.getGlobalConfiguration();
CacheManager cm = new DefaultCacheManager(globalConfiguration);
cacheManagers.add(cm);
cm.start();
@@ -76,18 +84,18 @@
cm.defineCache("first", configurationOverride);
Cache first = cm.getCache("first");
- JmxRegistrationManager regManager = new JmxRegistrationManager(mBeanServer, (AdvancedCache) first);
- regManager.registerAllMBeans();
- String name = regManager.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
+ ComponentGroupJmxRegistration regComponentGroup = buildRegistrator(first);
+ regComponentGroup.registerMBeans();
+ String name = regComponentGroup.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
ObjectName name1 = new ObjectName(name);
assertCorrectJmxName(name1, first);
assert mBeanServer.isRegistered(name1);
- regManager.unregisterAllMBeans();
+ regComponentGroup.unregisterCacheMBeans();
assert !mBeanServer.isRegistered(name1);
}
public void testLocalAndReplicatedCache() throws Exception {
- GlobalConfiguration globalConfiguration = GlobalConfiguration.getClusteredDefault();
+ GlobalConfiguration globalConfiguration = TestingUtil.getGlobalConfiguration();
CacheManager cm = new DefaultCacheManager(globalConfiguration);
cacheManagers.add(cm);
cm.start();
@@ -100,13 +108,13 @@
Cache replicatedCache = cm.getCache("replicated");
Cache localCache = cm.getCache("local");
- JmxRegistrationManager replicatedRegManager = new JmxRegistrationManager(mBeanServer, (AdvancedCache) replicatedCache);
- JmxRegistrationManager localRegManager = new JmxRegistrationManager(mBeanServer, (AdvancedCache) localCache);
- replicatedRegManager.registerAllMBeans();
- localRegManager.registerAllMBeans();
+ ComponentGroupJmxRegistration replicatedRegComponentGroup = buildRegistrator(replicatedCache);
+ ComponentGroupJmxRegistration localRegComponentGroup = buildRegistrator(localCache);
+ replicatedRegComponentGroup.registerMBeans();
+ localRegComponentGroup.registerMBeans();
- String replicatedtCMgmtIntName = replicatedRegManager.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
- String localCMgmtIntName = localRegManager.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
+ String replicatedtCMgmtIntName = replicatedRegComponentGroup.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
+ String localCMgmtIntName = localRegComponentGroup.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
ObjectName replObjectName = new ObjectName(replicatedtCMgmtIntName);
ObjectName localObjName = new ObjectName(localCMgmtIntName);
assertCorrectJmxName(replObjectName, replicatedCache);
@@ -115,46 +123,17 @@
assert mBeanServer.isRegistered(localObjName);
assert !localCMgmtIntName.equals(replicatedtCMgmtIntName);
- replicatedRegManager.unregisterAllMBeans();
- localRegManager.unregisterAllMBeans();
+ replicatedRegComponentGroup.unregisterCacheMBeans();
+ localRegComponentGroup.unregisterCacheMBeans();
assert !mBeanServer.isRegistered(new ObjectName(localCMgmtIntName));
assert !mBeanServer.isRegistered(new ObjectName(replicatedtCMgmtIntName));
}
- public void testCustomCacheName() throws Exception {
- CacheManager cm = TestingUtil.createLocalCacheManager();
- cacheManagers.add(cm);
- cm.start();
- Configuration configuration = config();
- configuration.setJmxNameBase("mircea:aKey=aValue,secondKey=secondValue");
- configuration.setCacheMode(Configuration.CacheMode.LOCAL);
- cm.defineCache("first", configuration);
- Cache cache = cm.getCache("first");
-
- JmxRegistrationManager regManager = new JmxRegistrationManager(mBeanServer, (AdvancedCache) cache);
- regManager.registerAllMBeans();
-
- String name = regManager.getObjectName(CacheMgmtInterceptor.class.getSimpleName());
- ObjectName name1 = new ObjectName(name);
- assert mBeanServer.isRegistered(name1);
- regManager.unregisterAllMBeans();
- assert !mBeanServer.isRegistered(name1);
-
-
- assert name1.getDomain().equals("mircea");
- assert name1.getKeyProperty("aKey").equals("aValue");
- assert name1.getKeyProperty("secondKey").equals("secondValue");
- assert name1.getKeyProperty(JmxRegistrationManager.JMX_RESOURCE_KEY) != null;
- }
-
private void assertCorrectJmxName(ObjectName objectName, Cache cache) {
- assert objectName.getKeyProperty(JmxRegistrationManager.CACHE_NAME_KEY).startsWith(cache.getName());
- String cacheModeStr = cache.getConfiguration().getCacheModeString().toLowerCase();
- assert objectName.getKeyProperty(JmxRegistrationManager.CACHE_NAME_KEY).contains(cacheModeStr);
- assert objectName.getKeyProperty(JmxRegistrationManager.JMX_RESOURCE_KEY) != null;
+ assert objectName.getKeyProperty(ComponentGroupJmxRegistration.CACHE_NAME_KEY).startsWith(cache.getName());
+ assert objectName.getKeyProperty(ComponentGroupJmxRegistration.JMX_RESOURCE_KEY) != null;
}
-
private Configuration config() {
Configuration configuration = new Configuration();
configuration.setExposeManagementStatistics(true);
Modified: core/branches/flat/src/test/java/org/horizon/test/TestingUtil.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/test/TestingUtil.java 2009-03-19 13:53:38 UTC (rev 7924)
+++ core/branches/flat/src/test/java/org/horizon/test/TestingUtil.java 2009-03-19 14:11:30 UTC (rev 7925)
@@ -621,11 +621,16 @@
}
public static CacheManager createClusteredCacheManager() {
+ GlobalConfiguration globalConfiguration = getGlobalConfiguration();
+ return new DefaultCacheManager(globalConfiguration);
+ }
+
+ public static GlobalConfiguration getGlobalConfiguration() {
GlobalConfiguration globalConfiguration = GlobalConfiguration.getClusteredDefault();
Properties newTransportProps = new Properties();
newTransportProps.put(JGroupsTransport.CONFIGURATION_STRING, JGroupsConfigBuilder.getJGroupsConfig());
globalConfiguration.setTransportProperties(newTransportProps);
- return new DefaultCacheManager(globalConfiguration);
+ return globalConfiguration;
}
public static CacheManager createLocalCacheManager() {
15 years, 9 months
JBoss Cache SVN: r7924 - searchable/trunk.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-19 09:53:38 -0400 (Thu, 19 Mar 2009)
New Revision: 7924
Modified:
searchable/trunk/pom.xml
Log:
Updated to JBC 3.0.3.GA
Modified: searchable/trunk/pom.xml
===================================================================
--- searchable/trunk/pom.xml 2009-03-19 10:28:45 UTC (rev 7923)
+++ searchable/trunk/pom.xml 2009-03-19 13:53:38 UTC (rev 7924)
@@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
- <jbosscache-searchable-version>1.0.0-SNAPSHOT</jbosscache-searchable-version>
+ <jbosscache-searchable-version>1.1.0-SNAPSHOT</jbosscache-searchable-version>
<!-- By default only generate Javadocs when we package the module. -->
<javadocPhase>package</javadocPhase>
</properties>
@@ -25,7 +25,7 @@
<dependency>
<groupId>org.jboss.cache</groupId>
<artifactId>jbosscache-core</artifactId>
- <version>3.0.2.GA</version>
+ <version>3.0.3.GA</version>
</dependency>
<dependency>
15 years, 9 months
JBoss Cache SVN: r7923 - in core/trunk/src/main: java/org/jboss/cache/lock and 1 other directories.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-19 06:28:45 -0400 (Thu, 19 Mar 2009)
New Revision: 7923
Modified:
core/trunk/src/main/docbook/userguide/en/modules/configuration_reference.xml
core/trunk/src/main/java/org/jboss/cache/lock/MVCCLockManager.java
core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementLockContainer.java
core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementOwnableReentrantLockContainer.java
core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementReentrantLockContainer.java
Log:
JBCACHE-1494 - concurrencyLevel to affect the non-striped lock container as well
Modified: core/trunk/src/main/docbook/userguide/en/modules/configuration_reference.xml
===================================================================
--- core/trunk/src/main/docbook/userguide/en/modules/configuration_reference.xml 2009-03-19 10:20:25 UTC (rev 7922)
+++ core/trunk/src/main/docbook/userguide/en/modules/configuration_reference.xml 2009-03-19 10:28:45 UTC (rev 7923)
@@ -498,7 +498,7 @@
<entry>500</entry>
<entry>Specifies the number of shared locks to use for write locks acquired. Only used if <literal>nodeLockingScheme</literal>
- is <literal>mvcc</literal>, and is ignored if <literal>useLockStriping</literal> is <literal>false</literal>.
+ is <literal>mvcc</literal>.
See the <link linkend="mvcc.impl">section on JBoss Cache's MVCC implementation</link> for a more detailed discussion.</entry>
</row>
</tbody>
Modified: core/trunk/src/main/java/org/jboss/cache/lock/MVCCLockManager.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/lock/MVCCLockManager.java 2009-03-19 10:20:25 UTC (rev 7922)
+++ core/trunk/src/main/java/org/jboss/cache/lock/MVCCLockManager.java 2009-03-19 10:28:45 UTC (rev 7923)
@@ -93,10 +93,13 @@
@Start
public void startLockManager()
{
+ // don't we all love nested ternary operators? :-)
lockContainer =
configuration.isUseLockStriping() ?
- transactionManager == null ? new ReentrantSharedLockContainer<Fqn>(configuration.getConcurrencyLevel()) : new OwnableReentrantSharedLockContainer<Fqn>(configuration.getConcurrencyLevel(), invocationContextContainer) :
- transactionManager == null ? new PerElementReentrantLockContainer<Fqn>() : new PerElementOwnableReentrantLockContainer<Fqn>(invocationContextContainer);
+ transactionManager == null ? new ReentrantSharedLockContainer<Fqn>(configuration.getConcurrencyLevel()) :
+ new OwnableReentrantSharedLockContainer<Fqn>(configuration.getConcurrencyLevel(), invocationContextContainer) :
+ transactionManager == null ? new PerElementReentrantLockContainer<Fqn>(configuration.getConcurrencyLevel()) :
+ new PerElementOwnableReentrantLockContainer<Fqn>(configuration.getConcurrencyLevel(), invocationContextContainer);
}
@Start
@@ -107,7 +110,7 @@
public boolean lock(Fqn fqn, LockType lockType, Object owner) throws InterruptedException
{
- if (lockType == READ) return true; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return true; // we don't support read locks.
if (trace) log.trace("Attempting to lock " + fqn);
return lockContainer.acquireLock(fqn, lockAcquisitionTimeout, MILLISECONDS);
@@ -115,7 +118,7 @@
public boolean lock(Fqn fqn, LockType lockType, Object owner, long timeoutMillis) throws InterruptedException
{
- if (lockType == READ) return true; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return true; // we don't support read locks.
if (trace) log.trace("Attempting to lock " + fqn);
return lockContainer.acquireLock(fqn, lockAcquisitionTimeout, MILLISECONDS);
@@ -123,7 +126,7 @@
public boolean lockAndRecord(Fqn fqn, LockType lockType, InvocationContext ctx) throws InterruptedException
{
- if (lockType == READ) return true; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return true; // we don't support read locks.
if (trace) log.trace("Attempting to lock " + fqn);
if (lockContainer.acquireLock(fqn, lockAcquisitionTimeout, MILLISECONDS))
@@ -220,25 +223,25 @@
public boolean lockAll(NodeSPI node, LockType lockType, Object owner) throws InterruptedException
{
- if (lockType == READ) return true; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return true; // we don't support read locks.
return lockRecursively(node.getDelegationTarget(), lockAcquisitionTimeout, false, null);
}
public boolean lockAll(NodeSPI node, LockType lockType, Object owner, long timeout) throws InterruptedException
{
- if (lockType == READ) return true; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return true; // we don't support read locks.
return lockRecursively(node.getDelegationTarget(), timeout, false, null);
}
public boolean lockAll(NodeSPI node, LockType lockType, Object owner, long timeout, boolean excludeInternalFqns) throws InterruptedException
{
- if (lockType == READ) return true; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return true; // we don't support read locks.
return lockRecursively(node.getDelegationTarget(), timeout, excludeInternalFqns, null);
}
public boolean lockAllAndRecord(NodeSPI node, LockType lockType, InvocationContext ctx) throws InterruptedException
{
- if (lockType == READ) return true; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return true; // we don't support read locks.
return lockRecursively(node.getDelegationTarget(), ctx.getLockAcquisitionTimeout(lockAcquisitionTimeout), false, ctx);
}
@@ -265,7 +268,7 @@
public boolean ownsLock(Fqn fqn, LockType lockType, Object owner)
{
- if (lockType == READ) return false; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return false; // we don't support read locks.
return lockContainer.ownsLock(fqn, owner);
}
@@ -281,7 +284,7 @@
public boolean isLocked(NodeSPI n, LockType lockType)
{
- if (lockType == READ) return false; // we don't support read locks. TODO: enforce this with an assertion
+ if (lockType == READ) return false; // we don't support read locks.
return lockContainer.isLocked(n.getFqn());
}
Modified: core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementLockContainer.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementLockContainer.java 2009-03-19 10:20:25 UTC (rev 7922)
+++ core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementLockContainer.java 2009-03-19 10:28:45 UTC (rev 7923)
@@ -13,8 +13,13 @@
*/
public abstract class PerElementLockContainer<E> implements LockContainer<E>
{
- ConcurrentMap<E, Lock> locks = new ConcurrentHashMap<E, Lock>();
+ protected final ConcurrentMap<E, Lock> locks;
+ protected PerElementLockContainer(int concurrencyLevel)
+ {
+ locks = new ConcurrentHashMap<E, Lock>(16, .75f, concurrencyLevel);
+ }
+
protected abstract Lock newLock();
public final Lock getLock(E object)
Modified: core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementOwnableReentrantLockContainer.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementOwnableReentrantLockContainer.java 2009-03-19 10:20:25 UTC (rev 7922)
+++ core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementOwnableReentrantLockContainer.java 2009-03-19 10:28:45 UTC (rev 7923)
@@ -14,8 +14,9 @@
{
private InvocationContextContainer icc;
- public PerElementOwnableReentrantLockContainer(InvocationContextContainer icc)
+ public PerElementOwnableReentrantLockContainer(int concurrencyLevel, InvocationContextContainer icc)
{
+ super(concurrencyLevel);
this.icc = icc;
}
Modified: core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementReentrantLockContainer.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementReentrantLockContainer.java 2009-03-19 10:20:25 UTC (rev 7922)
+++ core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementReentrantLockContainer.java 2009-03-19 10:28:45 UTC (rev 7923)
@@ -11,6 +11,11 @@
*/
public class PerElementReentrantLockContainer<E> extends PerElementLockContainer<E>
{
+ public PerElementReentrantLockContainer(int concurrencyLevel)
+ {
+ super(concurrencyLevel);
+ }
+
public boolean ownsLock(E object, Object owner)
{
ReentrantLock l = getLockFromMap(object);
15 years, 9 months
JBoss Cache SVN: r7922 - in core/branches/flat/src/main/java/org/horizon: util/concurrent/locks/containers and 1 other directory.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-19 06:20:25 -0400 (Thu, 19 Mar 2009)
New Revision: 7922
Modified:
core/branches/flat/src/main/java/org/horizon/lock/LockManagerImpl.java
core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/AbstractPerEntryLockContainer.java
core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/OwnableReentrantPerEntryLockContainer.java
core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/ReentrantPerEntryLockContainer.java
Log:
HORIZON-40 - Create a lock-per-entry lock manager
Modified: core/branches/flat/src/main/java/org/horizon/lock/LockManagerImpl.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/lock/LockManagerImpl.java 2009-03-19 10:06:23 UTC (rev 7921)
+++ core/branches/flat/src/main/java/org/horizon/lock/LockManagerImpl.java 2009-03-19 10:20:25 UTC (rev 7922)
@@ -69,7 +69,7 @@
public void startLockManager() {
lockContainer = configuration.isUseLockStriping() ?
transactionManager == null ? new ReentrantStripedLockContainer(configuration.getConcurrencyLevel()) : new OwnableReentrantStripedLockContainer(configuration.getConcurrencyLevel(), invocationContextContainer) :
- transactionManager == null ? new ReentrantPerEntryLockContainer() : new OwnableReentrantPerEntryLockContainer(invocationContextContainer);
+ transactionManager == null ? new ReentrantPerEntryLockContainer(configuration.getConcurrencyLevel()) : new OwnableReentrantPerEntryLockContainer(configuration.getConcurrencyLevel(), invocationContextContainer);
}
public Object getLockOwner(InvocationContext ctx) {
Modified: core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/AbstractPerEntryLockContainer.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/AbstractPerEntryLockContainer.java 2009-03-19 10:06:23 UTC (rev 7921)
+++ core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/AbstractPerEntryLockContainer.java 2009-03-19 10:20:25 UTC (rev 7922)
@@ -13,8 +13,12 @@
*/
public abstract class AbstractPerEntryLockContainer implements LockContainer {
- ConcurrentMap<Object, Lock> locks = new ConcurrentHashMap<Object, Lock>();
+ protected final ConcurrentMap<Object, Lock> locks;
+ protected AbstractPerEntryLockContainer(int concurrencyLevel) {
+ locks = new ConcurrentHashMap<Object, Lock>(16, .75f, concurrencyLevel);
+ }
+
protected abstract Lock newLock();
public final Lock getLock(Object key) {
Modified: core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/OwnableReentrantPerEntryLockContainer.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/OwnableReentrantPerEntryLockContainer.java 2009-03-19 10:06:23 UTC (rev 7921)
+++ core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/OwnableReentrantPerEntryLockContainer.java 2009-03-19 10:20:25 UTC (rev 7922)
@@ -15,7 +15,8 @@
private InvocationContextContainer icc;
- public OwnableReentrantPerEntryLockContainer(InvocationContextContainer icc) {
+ public OwnableReentrantPerEntryLockContainer(int concurrencyLevel, InvocationContextContainer icc) {
+ super(concurrencyLevel);
this.icc = icc;
}
Modified: core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/ReentrantPerEntryLockContainer.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/ReentrantPerEntryLockContainer.java 2009-03-19 10:06:23 UTC (rev 7921)
+++ core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/ReentrantPerEntryLockContainer.java 2009-03-19 10:20:25 UTC (rev 7922)
@@ -11,6 +11,10 @@
*/
public class ReentrantPerEntryLockContainer extends AbstractPerEntryLockContainer {
+ public ReentrantPerEntryLockContainer(int concurrencyLevel) {
+ super(concurrencyLevel);
+ }
+
protected Lock newLock() {
return new ReentrantLock();
}
15 years, 9 months
JBoss Cache SVN: r7921 - core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-19 06:06:23 -0400 (Thu, 19 Mar 2009)
New Revision: 7921
Modified:
core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/AbstractPerEntryLockContainer.java
Log:
HORIZON-40 - Create a lock-per-entry lock manager
Modified: core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/AbstractPerEntryLockContainer.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/AbstractPerEntryLockContainer.java 2009-03-19 09:59:50 UTC (rev 7920)
+++ core/branches/flat/src/main/java/org/horizon/util/concurrent/locks/containers/AbstractPerEntryLockContainer.java 2009-03-19 10:06:23 UTC (rev 7921)
@@ -17,11 +17,14 @@
protected abstract Lock newLock();
- public Lock getLock(Object key) {
- Lock l = newLock();
- Lock tmp = locks.putIfAbsent(key, l);
- if (tmp != null) l = tmp;
- return l;
+ public final Lock getLock(Object key) {
+ // this is an optimisation. It is not foolproof as we may still be creating new locks unnecessarily (thrown away
+ // when we do a putIfAbsent) but it minimises the chances somewhat, for the cost of an extra CHM get.
+ Lock lock = locks.get(key);
+ if (lock == null) lock = newLock();
+ Lock existingLock = locks.putIfAbsent(key, lock);
+ if (existingLock != null) lock = existingLock;
+ return lock;
}
public int getNumLocksHeld() {
@@ -33,13 +36,23 @@
}
public boolean acquireLock(Object key, long timeout, TimeUnit unit) throws InterruptedException {
- Lock l = getLock(key);
- boolean success = l.tryLock(timeout, unit);
- if (l != locks.get(key)) {
- l.unlock();
- success = acquireLock(key, timeout, unit); // todo avoid recursion here, this could get ugly!
+ while (true) {
+ Lock lock = getLock(key);
+ if (lock.tryLock(timeout, unit)) {
+ // lock acquired. Now check if it is the *correct* lock!
+ Lock existingLock = locks.putIfAbsent(key, lock);
+ if (existingLock != null && existingLock != lock) {
+ // we have the wrong lock! Unlock and retry.
+ lock.unlock();
+ } else {
+ // we got the right lock.
+ return true;
+ }
+ } else {
+ // we couldn't acquire the lock within the timeout period
+ return false;
+ }
}
- return success;
}
public void releaseLock(Object key) {
15 years, 9 months
JBoss Cache SVN: r7920 - core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2009-03-19 05:59:50 -0400 (Thu, 19 Mar 2009)
New Revision: 7920
Modified:
core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementLockContainer.java
Log:
JBCACHE-1494 - better impl
Modified: core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementLockContainer.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementLockContainer.java 2009-03-19 08:35:15 UTC (rev 7919)
+++ core/trunk/src/main/java/org/jboss/cache/util/concurrent/locks/PerElementLockContainer.java 2009-03-19 09:59:50 UTC (rev 7920)
@@ -17,9 +17,10 @@
protected abstract Lock newLock();
- public Lock getLock(E object)
+ public final Lock getLock(E object)
{
- Lock l = newLock();
+ Lock l = locks.get(object);
+ if (l == null) l = newLock();
Lock tmp = locks.putIfAbsent(object, l);
if (tmp != null) l = tmp;
return l;
@@ -53,30 +54,51 @@
public void acquireLock(E object)
{
- Lock l = getLock(object);
- l.lock();
- // now check that the lock is still valid...
- if (l != locks.get(object))
+ while (true)
{
- // we acquired the wrong lock!
- l.unlock();
- acquireLock(object);
+ Lock lock = getLock(object);
+ lock.lock();
+ // now check that the lock is still valid...
+ Lock currentLock = locks.putIfAbsent(object, lock);
+ if (currentLock != null && lock != currentLock)
+ {
+ // we acquired the wrong lock!
+ lock.unlock();
+ }
+ else
+ {
+ // we got the right lock!
+ break;
+ }
}
}
public boolean acquireLock(E object, long timeout, TimeUnit unit) throws InterruptedException
{
- Lock l = getLock(object);
- boolean result = l.tryLock(timeout, unit);
-
- // now check that the lock is still valid...
- if (result && l != locks.get(object))
+ while (true)
{
- // we acquired the wrong lock!
- l.unlock();
- result = acquireLock(object, timeout, unit);
+ Lock lock = getLock(object);
+ if (lock.tryLock(timeout, unit))
+ {
+ // now check that the lock is still valid...
+ Lock currentLock = locks.putIfAbsent(object, lock);
+ if (currentLock != null && lock != currentLock)
+ {
+ // we acquired the wrong lock!
+ lock.unlock();
+ }
+ else
+ {
+ // we got the right lock!
+ return true;
+ }
+ }
+ else
+ {
+ // if we haven't acquired the lock (i.e., timed out) return false
+ return false;
+ }
}
- return result;
}
public void releaseLock(E object)
15 years, 9 months