You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ch...@apache.org on 2015/05/05 12:26:41 UTC
svn commit: r1677774 - in /jackrabbit/oak/trunk:
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/
oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/
oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/
Author: chetanm
Date: Tue May 5 10:26:41 2015
New Revision: 1677774
URL: http://svn.apache.org/r1677774
Log:
OAK-2826 - Refactor ListeneableFutureTask to commons
Added:
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTask.java (with props)
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java (with props)
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTaskTest.java (with props)
Modified:
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/BackgroundObserver.java
Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTask.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTask.java?rev=1677774&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTask.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTask.java Tue May 5 10:26:41 2015
@@ -0,0 +1,88 @@
+/*
+ * 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.jackrabbit.oak.commons.concurrent;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * A {@link Future} that accepts completion listener. The listener is invoked
+ * once the future's computation is {@linkplain Future#isDone() complete}.
+ * If the computation has already completed when the listener is added, the
+ * listener will execute immediately.
+ *
+ * <p>Listener is invoked synchronously on the same thread which is used to
+ * executed the Future</p>
+ */
+public class NotifyingFutureTask extends FutureTask<Void> {
+ private final AtomicBoolean completed = new AtomicBoolean(false);
+
+ private volatile Runnable onComplete;
+
+ public NotifyingFutureTask(Callable<Void> callable) {
+ super(callable);
+ }
+
+ public NotifyingFutureTask(Runnable task) {
+ super(task, null);
+ }
+
+ /**
+ * Set the on complete handler. The handler will run exactly once after
+ * the task terminated. If the task has already terminated at the time of
+ * this method call the handler will execute immediately.
+ * <p/>
+ * Note: there is no guarantee to which handler will run when the method
+ * is called multiple times with different arguments.
+ *
+ * @param onComplete listener to invoke upon completion
+ */
+ public void onComplete(Runnable onComplete) {
+ this.onComplete = onComplete;
+ if (isDone()) {
+ run(onComplete);
+ }
+ }
+
+ @Override
+ protected void done() {
+ run(onComplete);
+ }
+
+ private void run(Runnable onComplete) {
+ if (onComplete != null && completed.compareAndSet(false, true)) {
+ onComplete.run();
+ }
+ }
+
+ private static final Runnable NOP = new Runnable() {
+ @Override
+ public void run() {
+ }
+ };
+
+ public static NotifyingFutureTask completed() {
+ NotifyingFutureTask f = new NotifyingFutureTask(NOP);
+ f.run();
+ return f;
+ }
+}
\ No newline at end of file
Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTask.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java?rev=1677774&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java Tue May 5 10:26:41 2015
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provides annotation support to produce JMX metadata.
+ *
+ * @version 1.0
+ */
+@Version("1.0")
+@Export(optional = "provide:=true")
+package org.apache.jackrabbit.oak.commons.concurrent;
+
+import aQute.bnd.annotation.Export;
+import aQute.bnd.annotation.Version;
+
Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTaskTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTaskTest.java?rev=1677774&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTaskTest.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTaskTest.java Tue May 5 10:26:41 2015
@@ -0,0 +1,69 @@
+/*
+ * 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.jackrabbit.oak.commons.concurrent;
+
+import java.util.concurrent.Callable;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class NotifyingFutureTaskTest {
+
+ @Test
+ public void onCompletion() throws Exception{
+ CountingCallable callable = new CountingCallable();
+ CountingRunnable runnable = new CountingRunnable();
+ NotifyingFutureTask nft = new NotifyingFutureTask(callable);
+ nft.onComplete(runnable);
+ nft.run();
+ assertEquals(1, callable.count);
+ assertEquals(1, runnable.count);
+
+ nft.run();
+ assertEquals("Callback should be invoked only once", 1, runnable.count);
+ }
+
+ @Test
+ public void completed() throws Exception{
+ CountingRunnable runnable = new CountingRunnable();
+ NotifyingFutureTask nft = NotifyingFutureTask.completed();
+ nft.onComplete(runnable);
+ assertEquals("Callback should still be invoked if already done", 1, runnable.count);
+ }
+
+ private static class CountingRunnable implements Runnable {
+ int count;
+ @Override
+ public void run() {
+ count++;
+ }
+ }
+
+ private static class CountingCallable implements Callable<Void> {
+ int count;
+ @Override
+ public Void call() throws Exception {
+ count++;
+ return null;
+ }
+ }
+
+}
\ No newline at end of file
Propchange: jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/NotifyingFutureTaskTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/BackgroundObserver.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/BackgroundObserver.java?rev=1677774&r1=1677773&r2=1677774&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/BackgroundObserver.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/BackgroundObserver.java Tue May 5 10:26:41 2015
@@ -30,13 +30,12 @@ import java.lang.Thread.UncaughtExceptio
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
-import java.util.concurrent.FutureTask;
-import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.base.Predicate;
+import org.apache.jackrabbit.oak.commons.concurrent.NotifyingFutureTask;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -109,7 +108,7 @@ public class BackgroundObserver implemen
/**
* Current background task
*/
- private volatile ListenableFutureTask currentTask = ListenableFutureTask.completed();
+ private volatile NotifyingFutureTask currentTask = NotifyingFutureTask.completed();
/**
* Completion handler: set the current task to the next task and schedules that one
@@ -134,7 +133,7 @@ public class BackgroundObserver implemen
@Override
public void run() {
- currentTask = new ListenableFutureTask(task);
+ currentTask = new NotifyingFutureTask(task);
executor.execute(currentTask);
}
};
@@ -296,61 +295,4 @@ public class BackgroundObserver implemen
private static Logger getLogger(@Nonnull Observer observer) {
return LoggerFactory.getLogger(checkNotNull(observer).getClass());
}
-
- /**
- * A future task with a on complete handler.
- */
- private static class ListenableFutureTask extends FutureTask<Void> {
- private final AtomicBoolean completed = new AtomicBoolean(false);
-
- private volatile Runnable onComplete;
-
- public ListenableFutureTask(Callable<Void> callable) {
- super(callable);
- }
-
- public ListenableFutureTask(Runnable task) {
- super(task, null);
- }
-
- /**
- * Set the on complete handler. The handler will run exactly once after
- * the task terminated. If the task has already terminated at the time of
- * this method call the handler will execute immediately.
- * <p>
- * Note: there is no guarantee to which handler will run when the method
- * is called multiple times with different arguments.
- * @param onComplete
- */
- public void onComplete(Runnable onComplete) {
- this.onComplete = onComplete;
- if (isDone()) {
- run(onComplete);
- }
- }
-
- @Override
- protected void done() {
- run(onComplete);
- }
-
- private void run(Runnable onComplete) {
- if (onComplete != null && completed.compareAndSet(false, true)) {
- onComplete.run();
- }
- }
-
- private static final Runnable NOP = new Runnable() {
- @Override
- public void run() {
- }
- };
-
- public static ListenableFutureTask completed() {
- ListenableFutureTask f = new ListenableFutureTask(NOP);
- f.run();
- return f;
- }
- }
-
}