jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1143510 - in /jackrabbit/sandbox/spi2microkernel/src: main/java/org/apache/jackrabbit/spi2microkernel/ test/java/org/apache/jackrabbit/spi2microkernel/
Date Wed, 06 Jul 2011 17:53:02 GMT
Author: mduerig
Date: Wed Jul  6 17:53:01 2011
New Revision: 1143510

URL: http://svn.apache.org/viewvc?rev=1143510&view=rev
Log:
observation: implement dispose for observation listeners

Modified:
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
    jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
    jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java?rev=1143510&r1=1143509&r2=1143510&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/RepositoryServiceImpl.java
Wed Jul  6 17:53:01 2011
@@ -373,7 +373,7 @@ public class RepositoryServiceImpl exten
 
     @Override
     public void dispose(Subscription subscription) throws RepositoryException {
-        super.dispose(subscription);    // fixme implement dispose
+        subscription(subscription).dispose();
     }
 
     //------------------------------------------< private >---

Modified: jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java?rev=1143510&r1=1143509&r2=1143510&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/SubscriptionImpl.java
Wed Jul  6 17:53:01 2011
@@ -56,6 +56,7 @@ public class SubscriptionImpl implements
     private final MicroKernel microKernel;
     private final SessionState sessionState;
     private final Queue<EventSource> eventSources = new LinkedList<EventSource>();
+    private volatile boolean disposed;
 
     public SubscriptionImpl(MicroKernel microKernel, SessionState sessionState) {
         this.microKernel = microKernel;
@@ -78,7 +79,7 @@ public class SubscriptionImpl implements
                 : eventSources.element().startRevision;
         }
 
-        String head = microKernel.waitForCommit(baseRevision, timeout);
+        String head = waitForCommit(baseRevision, timeout);
 
         synchronized (eventSources) {
             if (!eventSources.isEmpty()) {
@@ -98,8 +99,32 @@ public class SubscriptionImpl implements
         return eventBundles.toArray(new EventBundle[eventBundles.size()]);
     }
 
+    public void dispose() {
+        synchronized (eventSources) {
+            disposed = true;
+            Thread.currentThread().interrupt();
+            Thread.interrupted();  // clear interrupted flag right away. We might not get
a chance later
+        }
+    }
+
     //------------------------------------------< private >---
 
+    private String waitForCommit(String baseRevision, long timeout) {
+        long millis = timeout;
+        long start = System.currentTimeMillis();
+
+        do {
+            try {
+                return microKernel.waitForCommit(baseRevision, millis);
+            }
+            catch (InterruptedException e) {
+                millis = millis - System.currentTimeMillis() + start;
+            }
+        } while (!disposed && millis > 0);
+        
+        return baseRevision;
+    }
+
     private static JSONArray jsonArray(Object jsonArray) {
         if (jsonArray instanceof JSONArray) {
             return (JSONArray) jsonArray;

Modified: jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java?rev=1143510&r1=1143509&r2=1143510&view=diff
==============================================================================
--- jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java
(original)
+++ jackrabbit/sandbox/spi2microkernel/src/test/java/org/apache/jackrabbit/spi2microkernel/RepositoryTest.java
Wed Jul  6 17:53:01 2011
@@ -60,6 +60,11 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import static java.util.Arrays.asList;
 import static org.apache.jackrabbit.spi2microkernel.util.Arrays.contains;
@@ -911,7 +916,7 @@ public class RepositoryTest {
     }
 
     @Test
-    public void observation() throws RepositoryException, InterruptedException {
+    public void observation() throws RepositoryException {
         final Set<String> addNodes = Arrays.toSet(
                 testPath + "/1",
                 testPath + "/2",
@@ -1009,6 +1014,57 @@ public class RepositoryTest {
 
     }
 
+    @Test
+    public void observationNoEvents() throws RepositoryException, InterruptedException {
+        final List<Event> failedEvents = new ArrayList<Event>();
+
+        ObservationManager obsMgr = getSession().getWorkspace().getObservationManager();
+        obsMgr.setUserData("my user data");
+        obsMgr.addEventListener(new EventListener() {
+                public void onEvent(EventIterator events) {
+                    while (events.hasNext()) {
+                        failedEvents.add(events.nextEvent());
+                    }
+                }
+            },
+            Event.NODE_ADDED | Event.NODE_REMOVED | Event.NODE_MOVED | Event.PROPERTY_ADDED
|
+            Event.PROPERTY_REMOVED | Event.PROPERTY_CHANGED | Event.PERSIST, "/", true, null,
null, false);
+
+        Thread.sleep(5000);
+        assertTrue(failedEvents.isEmpty());
+    }
+
+    @Test
+    public void observationDispose() throws RepositoryException, ExecutionException, TimeoutException,
+                InterruptedException {
+        
+        final List<Event> failedEvents = new ArrayList<Event>();
+
+        final ObservationManager obsMgr = getSession().getWorkspace().getObservationManager();
+        obsMgr.setUserData("my user data");
+        final EventListener listener = new EventListener() {
+            public void onEvent(EventIterator events) {
+                while (events.hasNext()) {
+                    failedEvents.add(events.nextEvent());
+                }
+            }
+        };
+        obsMgr.addEventListener(listener, Event.NODE_ADDED | Event.NODE_REMOVED | Event.NODE_MOVED
|
+            Event.PROPERTY_ADDED | Event.PROPERTY_REMOVED | Event.PROPERTY_CHANGED | Event.PERSIST,
+            "/", true, null, null, false);
+
+        FutureTask<Object> disposer = new FutureTask<Object>(new Callable<Object>()
{
+            public Object call() throws Exception {
+                obsMgr.removeEventListener(listener);
+                return null;
+            }
+        });
+
+        disposer.run();
+        disposer.get(10000, TimeUnit.MILLISECONDS);
+        assertTrue(failedEvents.isEmpty());
+    }
+
     //------------------------------------------< private >---
 
     private void addProperty(Node parentNode, String name, Value value) throws RepositoryException
{



Mime
View raw message