activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r679347 - in /activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols: WhenOwnerDelegate.java WhenOwnerListener.java
Date Thu, 24 Jul 2008 10:43:25 GMT
Author: jstrachan
Date: Thu Jul 24 03:43:23 2008
New Revision: 679347

URL: http://svn.apache.org/viewvc?rev=679347&view=rev
Log:
updated the javadoc to better reflect the current implementation and added a helper class,
WhenOwnerDelegate to filter out unnecessary event noise so that the bare minimum methods are
invoked on the delegated WhenOwnerListener

Added:
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerDelegate.java
  (with props)
Modified:
    activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java

Added: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerDelegate.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerDelegate.java?rev=679347&view=auto
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerDelegate.java
(added)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerDelegate.java
Thu Jul 24 03:43:23 2008
@@ -0,0 +1,58 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zookeeper.protocols;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * A helper class which filters out unnecessary method calls so that the delegate {@link
WhenOwnerListener}
+ * will only be invoked once when it becomes the owner and once when it is no longer the
leader
+ * (after being previously the leader) which may help some implementators optimise their
code for responding to the
+ * start and stop of ownership.
+ *
+ * @version $Revision: 1.1 $
+ */
+public class WhenOwnerDelegate implements WhenOwnerListener {
+    private final WhenOwnerListener delegate;
+    private AtomicBoolean owner = new AtomicBoolean(false);
+
+    public WhenOwnerDelegate(WhenOwnerListener delegate) {
+        this.delegate = delegate;
+    }
+
+    /**
+     * Returns true if this instance currently is the owner of the lock or the leader of
the group
+     *
+     * @return true if this instance is the owner right now
+     */
+    public boolean isOwner() {
+        return owner.get();
+    }
+
+    public void whenOwner() {
+        if (owner.compareAndSet(false, true)) {
+            delegate.whenOwner();
+        }
+    }
+
+    public void whenNotOwner() {
+        if (owner.compareAndSet(true, false)) {
+            delegate.whenNotOwner();
+        }
+    }
+}

Propchange: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerDelegate.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java
URL: http://svn.apache.org/viewvc/activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java?rev=679347&r1=679346&r2=679347&view=diff
==============================================================================
--- activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java
(original)
+++ activemq/sandbox/zookeeper/zookeeper-protocols/src/main/java/org/apache/zookeeper/protocols/WhenOwnerListener.java
Thu Jul 24 03:43:23 2008
@@ -19,13 +19,13 @@
 
 /**
  * A listener used to respond to the event that the current znode has become the owner
- * of a lock or the leader of a group - or when due to a connection problem or session timeout
- * has no longer become the owner.
+ * of a lock or the leader of a group - or when due to a connection being closed or some
+ * connection problem or session timeout that the znode is not the owner.
  *
  * Implementors of this interface may be called first via the {@link #whenOwner()} method
- * and later on they may be called withe {@link #whenNotOwner()} if due to a failure condition
- * they are no longer the owner. However its worth remembering that neither method may be
invoked
- * if the znode never becomes the leader.
+ * if and when they do become the owner. They can be invoked multiple times with
+ * the {@link #whenNotOwner()} method if there are numerous connection errors or session
timeouts
+ * whether or not the znode is currently the owner or not.
  *
  * @version $Revision: 1.1 $
  */
@@ -37,12 +37,11 @@
     void whenOwner();
 
     /**
-     * Invoked when the owner of a lock or leader of a group is now no longer the owner
-     * due to some kind of failure scenario such as the connection to ZooKeeper failing
-     * or the session timing out.
+     * Invoked to notify the listener that it is not the owner of a lock or leader of a group
+     * due to the connection closing or some kind of failure scenario happening such as the
connection
+     * to ZooKeeper failing or the session timing out.
      *
-     * Note that this method is only invoked after a znode has become the owner; i.e. after
-     * a call to {@link #whenOwner()}
+     * This method can be invoked when the listener is not actually the owner.
      */
     void whenNotOwner();
 }



Mime
View raw message