[jboss-svn-commits] JBL Code SVN: r14198 - in labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku: cache/service and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Aug 14 06:26:12 EDT 2007
Author: adamw
Date: 2007-08-14 06:26:12 -0400 (Tue, 14 Aug 2007)
New Revision: 14198
Modified:
labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/RenewableCacheItem.java
labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/RenewableCacheItemConfiguration.java
labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/service/RenewableCacheService.java
labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/service/RenewableCacheServiceMBean.java
labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/tools/Tools.java
Log:
Some commetns & cleanup
Modified: labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/RenewableCacheItem.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/RenewableCacheItem.java 2007-08-14 06:15:51 UTC (rev 14197)
+++ labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/RenewableCacheItem.java 2007-08-14 10:26:12 UTC (rev 14198)
@@ -28,7 +28,7 @@
import org.jboss.shotoku.tools.Tools;
import org.jboss.shotoku.tools.ConcurrentSet;
import org.jboss.shotoku.tools.ConcurrentHashSet;
-import org.jboss.shotoku.cache.service.RenewableCacheService;
+import org.jboss.shotoku.cache.service.DummyRenewableCacheService;
import org.jboss.shotoku.cache.service.RenewableCacheServiceMBean;
import org.jboss.shotoku.exceptions.RenewableCacheException;
@@ -48,8 +48,8 @@
*
* The cache item will be auto-registered in the service upon construction
* --- so take care when constructing objects of this class. When you want to
- * remove all keys that are handled by this cache item, simply call
- * the {@link #unregister()} method.
+ * remove all keys, from the TreeCache node, that are handled by this cache item,
+ * simply call the {@link #unregister()} method.
*
* @param <K> Type of the key of the objects held in cache. The keys
* should bahave well as map keys (most probably, the hashCode() and
@@ -75,10 +75,10 @@
/**
* Creates and registeres a new cache item.
*
- * @param fqn An fqn of the node, where data should be held. If it is null,
+ * @param fqn An fqn of the node in TreeCache, where data should be held. If it is null,
* a unique fqn will be auto-generated for this cache item.
*
- * @param mbeanName A name of an {@link RenewableCacheService} mbean, which
+ * @param mbeanName A name of an {@link RenewableCacheServiceMBean} mbean, which
* should be used to perform cache operations. This mbeans holds a reference
* to a {@link org.jboss.cache.TreeCache}. If it is null, a default mbean
* name will be used.
@@ -99,26 +99,19 @@
this.mbeanName = mbeanName;
- try {
- service = Tools.getService(mbeanName);
- } catch (MalformedObjectNameException e) {
- log.error("No RenewableCacheService bound to " + mbeanName + " in cache item " + this.getClass() + "!");
- return;
- }
+ this.interval = interval;
+ this.timeout = timeout;
+ keysUpdates = new ConcurrentHashMap<K, Long>();
+ keysDuringUpdate = new ConcurrentHashSet<K>();
+
+ register();
+
if (fqn == null) {
this.fqn = service.generateNextFqn();
} else {
this.fqn = fqn;
}
-
- this.interval = interval;
- this.timeout = timeout;
-
- keysUpdates = new ConcurrentHashMap<K, Long>();
- keysDuringUpdate = new ConcurrentHashSet<K>();
-
- service.register(this);
}
/**
@@ -131,57 +124,126 @@
this(null, null, 0, 0);
}
+ /**
+ * Registers this cache item in the service. This method is automatically called
+ * by the constructor, so you will only need to use it if you previously
+ * manually unregistered the cache item using {@link #unregister()}.
+ */
+ public synchronized void register() {
+ synchronized (keysUpdates) {
+ synchronized (keysDuringUpdate) {
+ keysUpdates.clear();
+ keysDuringUpdate.clear();
+
+ try {
+ service = Tools.getService(mbeanName);
+ } catch (MalformedObjectNameException e) {
+ log.error("No RenewableCacheService bound to "
+ + mbeanName + " in cache item "
+ + this.getClass() + "!");
+ return;
+ }
+
+ service.register(this);
+ }
+ }
+ }
+
+ /**
+ * Removes all keys handled by this cache item from the associated TreeCache node and
+ * stops updates on this cache item.
+ */
+ public synchronized void unregister() {
+ try {
+ RenewableCacheServiceMBean oldService = service;
+ service = new DummyRenewableCacheService();
+
+ oldService.unregister(this);
+ } catch (CacheException e) {
+ log.error("Error while unregistering a cache item " + this.getClass() + ".", e);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.shotoku.cache.RenewableCacheItemConfiguration#getFqn()
+ */
public Fqn getFqn() {
return fqn;
}
- public void setFqn(Fqn fqn) {
- this.fqn = fqn;
- }
-
- public long getInterval() {
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.shotoku.cache.RenewableCacheItemConfiguration#getInterval()
+ */
+ public synchronized long getInterval() {
return interval;
}
- public void setInterval(long interval) {
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.shotoku.cache.RenewableCacheItemConfiguration#setInterval(long)
+ */
+ public synchronized void setInterval(long interval) {
this.interval = interval;
}
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.shotoku.cache.RenewableCacheItemConfiguration#getMbeanName()
+ */
public String getMbeanName() {
return mbeanName;
}
- public long getTimeout() {
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.shotoku.cache.RenewableCacheItemConfiguration#getTimeout()
+ */
+ public synchronized long getTimeout() {
return timeout;
}
- public void setTimeout(long timeout) {
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.shotoku.cache.RenewableCacheItemConfiguration#setTimeout(long)
+ */
+ public synchronized void setTimeout(long timeout) {
this.timeout = timeout;
}
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.shotoku.cache.RenewableCacheItemConfiguration#getKeysDuringUpdate()
+ */
public ConcurrentSet<K> getKeysDuringUpdate() {
return keysDuringUpdate;
}
+ /*
+ * (non-Javadoc)
+ * @see org.jboss.shotoku.cache.RenewableCacheItemConfiguration#getKeysUpdates()
+ */
public ConcurrentMap<K, Long> getKeysUpdates() {
return keysUpdates;
}
/**
* Binds the given key with the given object in the associated TreeCache
- * node. Should be called
+ * node. Should be only called
* from the {@link #update(Object, Object)} method to put new values in the
* cache.
* @param key Key of the object.
* @param object Object that should be bound.
- * @throws RenewableCacheException
+ * @throws RenewableCacheException Thrown if this method is not called from within
+ * the {@link #update(Object, Object)} method.
*/
- public final void put(K key, T object) throws RenewableCacheException {
- if (!keysUpdates.containsKey(key)) {
+ public synchronized final void put(K key, T object) throws RenewableCacheException {
+ if (!keysDuringUpdate.contains(key)) {
throw new RenewableCacheException("Key " + key + " is not handled by this " +
"ShotokuCacheItem.");
}
-
+
try {
service.put(fqn, key, object);
} catch (CacheException e) {
@@ -198,22 +260,26 @@
*/
@SuppressWarnings("unchecked")
public final T get(K key) {
- synchronized(this) {
- if (!keysUpdates.containsKey(key)) {
- T ret = init(key);
- try {
- service.put(fqn, key, ret);
- } catch (CacheException e) {
- log.error("Error while putting a value for key \"" + key + "\" in cache item " + this.getClass() + ".", e);
- return null;
- }
+ if (!keysUpdates.containsKey(key)) {
+ synchronized (this) {
+ if (!keysUpdates.containsKey(key)) {
+ T ret = init(key);
+ try {
+ service.put(fqn, key, ret);
+ } catch (CacheException e) {
+ log.error("Error while putting a value for key \""
+ + key + "\" in cache item " + this.getClass()
+ + ".", e);
+ return null;
+ }
- // We have to initialize a new object.
- keysUpdates.put(key, System.currentTimeMillis());
+ // We have to initialize a new object.
+ keysUpdates.put(key, System.currentTimeMillis());
- return ret;
- }
- }
+ return ret;
+ }
+ }
+ }
try {
return (T) service.get(fqn, key);
@@ -224,18 +290,7 @@
}
/**
- * Removes all keys handled by this cache item from the associated TreeCache node.
- */
- public final void unregister() {
- try {
- service.unregister(this);
- } catch (CacheException e) {
- log.error("Error while unregistering a cache item " + this.getClass() + ".", e);
- }
- }
-
- /**
- * Called by the update service. You shouldn't call it from inside your
+ * Called by the service update thread. You shouldn't call it from inside your
* code.
*/
public final void update() {
@@ -261,16 +316,6 @@
}
/**
- * Notifies the cache item that it should reset its set of keys that
- * are during update, most probably beacuse the managing service has
- * been restarted (so updates of all keys that have been updated got
- * interrupted).
- */
- //public void resetKeysDuringUpdate() {
- // keysDuringUpdate.clear();
- //}
-
- /**
* Called by the service periodically to update the object held in the
* cache.
* If the object in the cache should be changed, the implementing
@@ -283,7 +328,7 @@
/**
* Called when the user demands an object which hasn't been accessed
* before, and thus, which hasn't been yet initialized. Here,
- * {@link #put(Object, Object)} should <b>not</b> be called, as the returned object
+ * {@link #put(Object, Object)} should <b>not</b> be called, as the returned object
* is automatically placed in the cache.
* @param key Key of the object to initialize.
* @return Initial value of an object with the given key.
Modified: labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/RenewableCacheItemConfiguration.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/RenewableCacheItemConfiguration.java 2007-08-14 06:15:51 UTC (rev 14197)
+++ labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/RenewableCacheItemConfiguration.java 2007-08-14 10:26:12 UTC (rev 14198)
@@ -26,24 +26,72 @@
import java.util.Set;
import org.jboss.cache.Fqn;
+import org.jboss.shotoku.cache.service.RenewableCacheServiceMBean;
/**
- *
- * @param <K>
+ * Configuration data of a cache item.
+ * @param <K> Type of the key of the objects held in cache. The keys
+ * should bahave well as map keys (most probably, the hashCode() and
+ * equals() methods should be overriden).
* @author <a href="mailto:adam.warski at jboss.org">Adam Warski</a>
*/
public interface RenewableCacheItemConfiguration<K> {
+ /**
+ *
+ * @return A fqn of a TreeCache node associated with this cache item. This is a node in which
+ * data will be kept.
+ */
public Fqn getFqn();
- public void setFqn(Fqn fqn);
+ /**
+ *
+ * @return Interval at which the update operation will be executed.
+ * Effectively, the interval will be rounded to the nearest multiplicity of
+ * the service update thread interval. The interval should be given in milliseconds.
+ * If it is 0, the {@link RenewableCacheItem#update()} method will be executed on every service
+ * thread update.
+ */
public long getInterval();
+ /**
+ *
+ * @param interval Interval at which the update operation will be executed.
+ * Effectively, the interval will be rounded to the nearest multiplicity of
+ * the service update thread interval. The interval should be given in milliseconds.
+ * If it is 0, the {@link RenewableCacheItem#update()} method will be executed on every service
+ * thread update.
+ */
public void setInterval(long interval);
+ /**
+ *
+ * @return How long, at a maximum, an update method for a key can last. If this
+ * value is non-zero, and is exceeded, a monitoring thread will interupt the update.
+ */
public long getTimeout();
+ /**
+ *
+ * @param timeout How long, at a maximum, an update method for a key can last. If this
+ * value is non-zero, and is exceeded, a monitoring thread will interupt the update.
+ */
public void setTimeout(long timeout);
+ /**
+ *
+ * @return Name of an mbean implementing the {@link RenewableCacheServiceMBean} interface, associated
+ * with this cache item.
+ */
public String getMbeanName();
+ /**
+ *
+ * @return A map of keys, which are handeled by this cache item, and corresponding last
+ * update times.
+ */
public Map<K, Long> getKeysUpdates();
+
+ /**
+ *
+ * @return A set of keys, which are currently being updated.
+ */
public Set<K> getKeysDuringUpdate();
}
Modified: labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/service/RenewableCacheService.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/service/RenewableCacheService.java 2007-08-14 06:15:51 UTC (rev 14197)
+++ labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/service/RenewableCacheService.java 2007-08-14 10:26:12 UTC (rev 14198)
@@ -55,26 +55,21 @@
*/
public void start() {
- log.info("Starting main update thread...");
+ log.info("Starting main update thread.");
+
startUpdateThread();
-
- log.info("Starting update threads...");
- setUpdateThreadCount(updateThreadCount);
-
- //log.info("Reseting keys during update in cache items...");
- // Reseting just in case - if any thread unexpectadly died.
- //for (RenewableCacheItem sci : cacheItems) {
- // sci.resetKeysDuringUpdate();
- //}
}
public void stop() {
- signalExitAllUpdateThreads();
getUpdateThread().interrupt();
- log.info("All update threads stopped.");
+ log.info("Signaled the main update thread to stop.");
}
+ /*
+ *
+ */
+
public TreeCacheMBean getTreeCache() {
return treeCache;
}
@@ -131,7 +126,7 @@
sleep(getInterval());
} catch (InterruptedException e) {
// Quit.
- log.debug("Stopping update thread for " + getName() + " (interrupted).", e);
+ log.info("Stopping update thread for " + getName() + " (interrupted).");
return;
}
@@ -209,35 +204,25 @@
return updateThreadCount;
}
- public void setUpdateThreadCount(int n) {
- synchronized (updateThreadDataQueue) {
- if (updateThreadCount < n) {
- for (int i=updateThreadCount; i<n; i++) {
- UpdateThread ut = new UpdateThread(this, updateThreadDataQueue);
- ut.start();
- }
- } else if (n < updateThreadCount) {
- for (int i=updateThreadCount; i>n; i--) {
- updateThreadDataQueue.offer(new SignalExitUpdateThreadData());
- }
- }
- }
+ public synchronized void setUpdateThreadCount(int n) {
+ if (updateThreadCount < n) {
+ for (int i = updateThreadCount; i < n; i++) {
+ UpdateThread ut = new UpdateThread(this, updateThreadDataQueue);
+ ut.start();
+ }
+ } else if (n < updateThreadCount) {
+ for (int i = updateThreadCount; i > n; i--) {
+ updateThreadDataQueue.offer(new SignalExitUpdateThreadData());
+ }
+ }
- log.info("Update thread count set to: " + n + ".");
- updateThreadCount = n;
- }
+ log.info("Update thread count set to: " + n + ".");
+ updateThreadCount = n;
+ }
- private void signalExitAllUpdateThreads() {
- synchronized (updateThreadDataQueue) {
- for (int i=0; i<updateThreadCount; i++) {
- updateThreadDataQueue.offer(new SignalExitUpdateThreadData());
- }
- }
- }
-
/*
- * Update function.
- */
+ * Update function.
+ */
public void update() {
for (RenewableCacheItem sci : cacheItems) {
@@ -248,32 +233,4 @@
}
}
}
-
- /*
- * Description functions.
- */
-
- public String getServiceDescription() {
- /*long now = System.currentTimeMillis();
-
- StringBuffer sb = new StringBuffer("Cache service.<br />");
- sb.append("Currently storing ").append(cache.size()).append(" items in the cache ");
- sb.append("and ").append(cacheItems.size()).append(" ShotokuCacheItem objects.<br />");
- sb.append("Objects in cache:<br />");
- for (Object key : cache.keySet()) {
- sb.append(key.toString()).append(" : ").append(
- cache.get(key).getClass().getName()).append(
- ", last updated ").append((now - lastUpdates.get(key)) / 1000).append(
- " seconds ago.<br />");
- }
-
- sb.append("Cache items:<br />");
- for (RenewableCacheItem sci : cacheItems) {
- sb.append(sci.getClass().getName()).append("<br />");
- }
-
- return sb.toString();*/
-
- return null;
- }
}
Modified: labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/service/RenewableCacheServiceMBean.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/service/RenewableCacheServiceMBean.java 2007-08-14 06:15:51 UTC (rev 14197)
+++ labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/cache/service/RenewableCacheServiceMBean.java 2007-08-14 10:26:12 UTC (rev 14198)
@@ -35,25 +35,13 @@
* @author <a href="mailto:adam.warski at jboss.org">Adam Warski</a>
*/
public interface RenewableCacheServiceMBean {
- /*
- * CACHE
- */
-
public Object get(Fqn fqn, Object key) throws CacheException;
public void put(Fqn fqn, Object key, Object o) throws CacheException;
public void remove(Fqn fqn, Object key) throws CacheException;
- /**
- * Touch the last update time of a key, that is, notifies the
- * service that the given key is up to date.
- * @param key
- */
public void register(RenewableCacheItem cacheItem);
public void unregister(RenewableCacheItem cacheItem) throws CacheException;
- /**
- * Gets a next unique key base for a shotoku cache item.
- */
public Fqn generateNextFqn();
public void addUpdateThreadData(UpdateThreadData data);
@@ -75,8 +63,6 @@
public void update();
- public String getServiceDescription();
-
public void start();
public void stop();
}
Modified: labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/tools/Tools.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/tools/Tools.java 2007-08-14 06:15:51 UTC (rev 14197)
+++ labs/shotoku/trunk/shotoku-cache/src/java/org/jboss/shotoku/tools/Tools.java 2007-08-14 10:26:12 UTC (rev 14198)
@@ -34,10 +34,22 @@
* @author <a href="mailto:adam.warski at jboss.org">Adam Warski</a>
*/
public class Tools {
+ /**
+ * The first part of automatically generated fqn-s for cache item nodes.
+ */
public static final String GENERATED_FQN_BASE = "ShotokuRenewableCache";
+ /**
+ * Name of the default {@link RenewableCacheServiceMBean} mbean.
+ */
public static final String DEFAULT_RENEWABLE_CACHE_MBEAN = "shotoku:service=RenewableCache";
+ /**
+ * Gets a reference to an mbean with the given name.
+ * @param mbeanName Name of the mbean to get.
+ * @return A reference to the mbean.
+ * @throws MalformedObjectNameException
+ */
public static RenewableCacheServiceMBean getService(String mbeanName) throws MalformedObjectNameException {
return (RenewableCacheServiceMBean) MBeanProxyExt.create(
RenewableCacheServiceMBean.class, mbeanName,
@@ -47,8 +59,7 @@
/**
* Checks if the given string is empty (null or "").
*
- * @param s
- * String to check.
+ * @param s String to check.
* @return True iff the given string is null or equal to "".
*/
public static boolean isEmpty(String s) {
More information about the jboss-svn-commits
mailing list