[jboss-cvs] JBossCache/src/org/jboss/cache/notifications ...
Manik Surtani
manik at jboss.org
Wed May 23 11:22:04 EDT 2007
User: msurtani
Date: 07/05/23 11:22:04
Modified: src/org/jboss/cache/notifications Notifier.java
Log:
Performance enhancements, including a new invoke() signature for Interceptor
Revision Changes Path
1.21 +113 -87 JBossCache/src/org/jboss/cache/notifications/Notifier.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Notifier.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/notifications/Notifier.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- Notifier.java 23 May 2007 10:28:58 -0000 1.20
+++ Notifier.java 23 May 2007 15:22:04 -0000 1.21
@@ -8,7 +8,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.jboss.cache.CacheImpl;
+import org.jboss.cache.Cache;
import org.jboss.cache.CacheListener;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
@@ -37,6 +37,7 @@
// is empty
//
private boolean hasListeners = false;
+ private Cache cache;
// store this seperately from other listeners to avoid concurrency penalty of
// iterating through Concurrent Set - eviction listener is always there (or almost always)
@@ -44,8 +45,6 @@
private CacheListener evictionPolicyListener;
private final Set<CacheListener> listeners = new ConcurrentHashSet<CacheListener>();
- private CacheImpl cache;
- private InvocationContext tempCtx;
private static final Log log = LogFactory.getLog(Notifier.class);
// --- the java.lang.reflect.Methods of CacheListener
@@ -77,7 +76,7 @@
}
}
- public Notifier(CacheImpl cache)
+ public Notifier(Cache cache)
{
this.cache = cache;
}
@@ -142,15 +141,16 @@
*
* @param fqn
* @param pre
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyNodeCreated(Fqn fqn, boolean pre, boolean sendImmediately)
+ public void notifyNodeCreated(Fqn fqn, boolean pre, InvocationContext ctx, boolean sendImmediately)
{
- boolean originLocal = cache.getInvocationContext().isOriginLocal();
+ boolean originLocal = ctx.isOriginLocal();
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodeCreated(fqn, pre, originLocal);
@@ -162,12 +162,12 @@
listener.nodeCreated(fqn, pre, originLocal);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodeCreated, new Object[]{fqn, pre, originLocal});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -178,15 +178,16 @@
* @param pre
* @param modificationType
* @param data
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyNodeModified(Fqn fqn, boolean pre, CacheListener.ModificationType modificationType, Map<K, V> data, boolean sendImmediately)
+ public void notifyNodeModified(Fqn fqn, boolean pre, CacheListener.ModificationType modificationType, Map<K, V> data, InvocationContext ctx, boolean sendImmediately)
{
- boolean originLocal = cache.getInvocationContext().isOriginLocal();
+ boolean originLocal = ctx.isOriginLocal();
Map dataCopy = copy(data);
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodeModified(fqn, pre, originLocal, modificationType, dataCopy);
@@ -198,12 +199,12 @@
listener.nodeModified(fqn, pre, originLocal, modificationType, dataCopy);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodeModified, new Object[]{fqn, pre, originLocal, modificationType, dataCopy});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -213,15 +214,16 @@
* @param fqn
* @param pre
* @param data
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyNodeRemoved(Fqn fqn, boolean pre, Map<K, V> data, boolean sendImmediately)
+ public void notifyNodeRemoved(Fqn fqn, boolean pre, Map<K, V> data, InvocationContext ctx, boolean sendImmediately)
{
- boolean originLocal = cache.getInvocationContext().isOriginLocal();
+ boolean originLocal = ctx.isOriginLocal();
Map dataCopy = copy(data);
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodeRemoved(fqn, pre, originLocal, dataCopy);
@@ -233,12 +235,12 @@
listener.nodeRemoved(fqn, pre, originLocal, dataCopy);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodeRemoved, new Object[]{fqn, pre, originLocal, dataCopy});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -248,13 +250,14 @@
*
* @param fqn
* @param pre
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyNodeVisited(Fqn fqn, boolean pre, boolean sendImmediately)
+ public void notifyNodeVisited(Fqn fqn, boolean pre, InvocationContext ctx, boolean sendImmediately)
{
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodeVisited(fqn, pre);
@@ -266,22 +269,22 @@
listener.nodeVisited(fqn, pre);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodeVisited, new Object[]{fqn, pre});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
- public void notifyNodeMoved(Fqn originalFqn, Fqn newFqn, boolean pre, boolean sendImmediately)
+ public void notifyNodeMoved(Fqn originalFqn, Fqn newFqn, boolean pre, InvocationContext ctx, boolean sendImmediately)
{
- boolean originLocal = cache.getInvocationContext().isOriginLocal();
+ boolean originLocal = ctx.isOriginLocal();
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodeMoved(originalFqn, newFqn, pre, originLocal);
@@ -293,12 +296,12 @@
listener.nodeMoved(originalFqn, newFqn, pre, originLocal);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodeMoved, new Object[]{originalFqn, newFqn, pre, originLocal});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -308,14 +311,15 @@
*
* @param fqn
* @param pre
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyNodeEvicted(Fqn fqn, boolean pre, boolean sendImmediately)
+ public void notifyNodeEvicted(Fqn fqn, boolean pre, InvocationContext ctx, boolean sendImmediately)
{
- boolean originLocal = cache.getInvocationContext().isOriginLocal();
+ boolean originLocal = ctx.isOriginLocal();
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodeEvicted(fqn, pre, originLocal);
@@ -327,12 +331,12 @@
listener.nodeEvicted(fqn, pre, originLocal);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodeEvicted, new Object[]{fqn, pre, originLocal});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -342,14 +346,15 @@
* @param fqn
* @param pre
* @param data
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyNodeLoaded(Fqn fqn, boolean pre, Map<K, V> data, boolean sendImmediately)
+ public void notifyNodeLoaded(Fqn fqn, boolean pre, Map<K, V> data, InvocationContext ctx, boolean sendImmediately)
{
Map<K, V> dataCopy = copy(data);
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodeLoaded(fqn, pre, (Map<Object, Object>) dataCopy);
@@ -361,12 +366,12 @@
listener.nodeLoaded(fqn, pre, (Map<Object, Object>) dataCopy);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodeLoaded, new Object[]{fqn, pre, dataCopy});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -375,13 +380,14 @@
*
* @param fqn
* @param pre
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyNodeActivated(Fqn fqn, boolean pre, boolean sendImmediately)
+ public void notifyNodeActivated(Fqn fqn, boolean pre, InvocationContext ctx, boolean sendImmediately)
{
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodeActivated(fqn, pre);
@@ -393,12 +399,12 @@
listener.nodeActivated(fqn, pre);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodeActivated, new Object[]{fqn, pre});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -407,13 +413,14 @@
*
* @param fqn
* @param pre
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyNodePassivated(Fqn fqn, boolean pre, boolean sendImmediately)
+ public void notifyNodePassivated(Fqn fqn, boolean pre, InvocationContext ctx, boolean sendImmediately)
{
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.nodePassivated(fqn, pre);
@@ -425,26 +432,27 @@
listener.nodePassivated(fqn, pre);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(nodePassivated, new Object[]{fqn, pre});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
/**
* Notifies all registered listeners of a cacheStarted event.
*
- * @param cache
- * @param sendImmediately
+ * @param cache cache instance to notify
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyCacheStarted(CacheSPI cache, boolean sendImmediately)
+ public void notifyCacheStarted(CacheSPI cache, InvocationContext ctx, boolean sendImmediately)
{
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.cacheStarted(cache);
@@ -456,26 +464,27 @@
listener.cacheStarted(cache);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(cacheStarted, new Object[]{cache});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
/**
* Notifies all registered listeners of a cacheStopped event.
*
- * @param cache
- * @param sendImmediately
+ * @param cache cache instance to notify
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyCacheStopped(CacheSPI cache, boolean sendImmediately)
+ public void notifyCacheStopped(CacheSPI cache, InvocationContext ctx, boolean sendImmediately)
{
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.cacheStopped(cache);
@@ -487,12 +496,12 @@
listener.cacheStopped(cache);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(cacheStopped, new Object[]{cache});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -500,13 +509,14 @@
* Notifies all registered listeners of a viewChange event.
*
* @param new_view
- * @param sendImmediately
+ * @param ctx context of invocation
+ * @param sendImmediately whether notifications are deferred to the NotificationInterceptor (sendImmediately = false)
*/
- public void notifyViewChange(View new_view, boolean sendImmediately)
+ public void notifyViewChange(View new_view, InvocationContext ctx, boolean sendImmediately)
{
if (sendImmediately)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
if (evictionPolicyListener != null)
{
evictionPolicyListener.viewChange(new_view);
@@ -518,12 +528,12 @@
listener.viewChange(new_view);
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
else
{
MethodCall call = new MethodCall(viewChange, new Object[]{new_view});
- cache.getInvocationContext().addCacheListenerEvent(call);
+ ctx.addCacheListenerEvent(call);
}
}
@@ -533,22 +543,35 @@
return new MapCopy<K, V>(data);
}
- private void restoreInvocationContext()
+ private void restoreInvocationContext(InvocationContext backup)
{
- cache.getInvocationContext().setState(tempCtx);
+ cache.setInvocationContext(backup);
}
- private void resetInvocationContext()
- {
- try
- {
- tempCtx = cache.getInvocationContext().clone();
- }
- catch (CloneNotSupportedException e)
+ /**
+ * Resets the current (passed-in) invocation, and returns a temp InvocationContext containing its state so it can
+ * be restored later using {@link #restoreInvocationContext(org.jboss.cache.InvocationContext)}
+ *
+ * @param ctx the current context to be reset
+ * @return a clone of ctx, before it was reset
+ */
+ private InvocationContext resetInvocationContext(InvocationContext ctx)
{
- log.warn("Unable to clone Invocation Context " + cache.getInvocationContext(), e);
- }
- cache.getInvocationContext().reset();
+// InvocationContext tempCtx = null;
+// try
+// {
+// tempCtx = ctx.clone();
+// }
+// catch (CloneNotSupportedException e)
+// {
+// log.warn("Unable to clone Invocation Context " + ctx, e);
+// }
+// ctx.reset();
+// return tempCtx;
+
+ // wipe current context.
+ cache.setInvocationContext(null);
+ return ctx;
}
/**
@@ -571,11 +594,12 @@
/**
* Fires off all notifications for a given queue.
*
+ * @param ctx
* @param queue queue to process.
*/
- public void invokeQueuedNotifications(List<MethodCall> queue)
+ public void invokeQueuedNotifications(InvocationContext ctx, List<MethodCall> queue)
{
- resetInvocationContext();
+ InvocationContext backup = resetInvocationContext(ctx);
for (MethodCall c : queue)
{
if (evictionPolicyListener != null)
@@ -604,14 +628,16 @@
}
}
}
- restoreInvocationContext();
+ restoreInvocationContext(backup);
}
/**
* Fires off all notifications that have been registered within the current invocation, with sendImmediately set to false.
+ *
+ * @param ctx context of the invocation
*/
- public void invokeQueuedNotifications()
+ public void invokeQueuedNotifications(InvocationContext ctx)
{
- invokeQueuedNotifications(cache.getInvocationContext().getCacheListenerEvents());
+ invokeQueuedNotifications(ctx, ctx.getCacheListenerEvents());
}
}
More information about the jboss-cvs-commits
mailing list