Author: chetanm
Date: Mon Nov 23 12:31:26 2015
New Revision: 1715785

URL: http://svn.apache.org/viewvc?rev=1715785&view=rev
Log:
OAK-3667 - Refactor executor closing logic as a utility class

Added:
    
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloser.java
   (with props)
    
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloserTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/metric/StatisticsProviderFactory.java
    
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/repository/RepositoryImpl.java

Added: 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloser.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloser.java?rev=1715785&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloser.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloser.java
 Mon Nov 23 12:31:26 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.io.Closeable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import javax.annotation.Nullable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to properly close any ExecutorService. It ensures
+ * that executor is properly closed once the call from close is returned
+ */
+public final class ExecutorCloser implements Closeable {
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final ExecutorService executorService;
+    private final int timeout;
+    private final TimeUnit timeUnit;
+
+    public ExecutorCloser(@Nullable ExecutorService executorService) {
+        this(executorService, 5, TimeUnit.SECONDS);
+    }
+
+    public ExecutorCloser(@Nullable ExecutorService executorService, int 
timeout, TimeUnit unit) {
+        this.executorService = executorService;
+        this.timeout = timeout;
+        this.timeUnit = unit;
+    }
+
+    @Override
+    public void close() {
+        if (executorService == null) {
+            return;
+        }
+        try {
+            executorService.shutdown();
+            executorService.awaitTermination(timeout, timeUnit);
+        } catch (InterruptedException e) {
+            log.error("Error while shutting down the executorService", e);
+            Thread.currentThread().interrupt();
+        } finally {
+            if (!executorService.isTerminated()) {
+                log.warn("ExecutorService didn't shutdown properly. Will be 
forced now.");
+            }
+            executorService.shutdownNow();
+        }
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
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=1715785&r1=1715784&r2=1715785&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/package-info.java
 Mon Nov 23 12:31:26 2015
@@ -22,7 +22,7 @@
  *
  * @version 1.0
  */
-@Version("1.0")
+@Version("1.1")
 @Export(optional = "provide:=true")
 package org.apache.jackrabbit.oak.commons.concurrent;
 

Added: 
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloserTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloserTest.java?rev=1715785&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloserTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloserTest.java
 Mon Nov 23 12:31:26 2015
@@ -0,0 +1,66 @@
+/*
+ * 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.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class ExecutorCloserTest {
+
+    @Test
+    public void simple() throws Exception{
+        ExecutorService executorService = Executors.newSingleThreadExecutor();
+        assertFalse(executorService.isTerminated());
+
+        new ExecutorCloser(executorService).close();
+        assertTrue(executorService.isTerminated());
+    }
+
+    @Test
+    public void nullExecutor() throws Exception{
+        new ExecutorCloser(null).close();
+    }
+
+    @Test
+    public void timeoutHandling() throws Exception{
+        ExecutorService executorService = Executors.newSingleThreadExecutor();
+
+        final CountDownLatch latch = new CountDownLatch(1);
+        executorService.submit(new Callable<Void>() {
+            @Override
+            public Void call() throws Exception {
+                latch.await();
+                return null;
+            }
+        });
+        new ExecutorCloser(executorService, 100, 
TimeUnit.MILLISECONDS).close();
+        assertFalse(executorService.isTerminated());
+        assertTrue(executorService.isShutdown());
+    }
+
+}

Propchange: 
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorCloserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java?rev=1715785&r1=1715784&r2=1715785&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java 
(original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java 
Mon Nov 23 12:31:26 2015
@@ -60,6 +60,7 @@ import org.apache.jackrabbit.oak.api.Des
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.jmx.QueryEngineSettingsMBean;
 import org.apache.jackrabbit.oak.api.jmx.RepositoryManagementMBean;
+import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
 import org.apache.jackrabbit.oak.core.ContentRepositoryImpl;
 import org.apache.jackrabbit.oak.management.RepositoryManager;
 import org.apache.jackrabbit.oak.plugins.commit.ConflictHook;
@@ -697,30 +698,6 @@ public class Oak {
         return createContentSession().getLatestRoot();
     }
 
-    private static class ExecutorCloser implements Closeable {
-        final ExecutorService executorService;
-
-        private ExecutorCloser(ExecutorService executorService) {
-            this.executorService = executorService;
-        }
-
-        @Override
-        public void close() throws IOException {
-            try {
-                executorService.shutdown();
-                executorService.awaitTermination(5, TimeUnit.SECONDS);
-            } catch (InterruptedException e) {
-                LOG.error("Error while shutting down the executorService", e);
-                Thread.currentThread().interrupt();
-            } finally {
-                if (!executorService.isTerminated()) {
-                    LOG.warn("executorService didn't shutdown properly. Will 
be forced now.");
-                }
-                executorService.shutdownNow();
-            }
-        }
-    }
-
     /**
      * CommitHook to ensure that commit only go through till repository is not
      * closed. Once repository is closed the commits would be failed

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/metric/StatisticsProviderFactory.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/metric/StatisticsProviderFactory.java?rev=1715785&r1=1715784&r2=1715785&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/metric/StatisticsProviderFactory.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/metric/StatisticsProviderFactory.java
 Mon Nov 23 12:31:26 2015
@@ -35,6 +35,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.jackrabbit.oak.commons.PropertiesUtil;
+import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
 import org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider;
 import org.apache.jackrabbit.oak.stats.StatisticsProvider;
 import org.osgi.framework.BundleContext;
@@ -100,11 +101,7 @@ public class StatisticsProviderFactory {
             ((Closeable) statisticsProvider).close();
         }
 
-        //TODO Refactor ExecutorCloser in Oak as a utility class and
-        //use that here
-        if (executor != null) {
-            executor.shutdown();
-        }
+        new ExecutorCloser(executor).close();
     }
 
     private StatisticsProvider createProvider(String providerType) {

Modified: 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/repository/RepositoryImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/repository/RepositoryImpl.java?rev=1715785&r1=1715784&r2=1715785&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/repository/RepositoryImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/repository/RepositoryImpl.java
 Mon Nov 23 12:31:26 2015
@@ -51,6 +51,7 @@ import org.apache.jackrabbit.commons.Sim
 import org.apache.jackrabbit.oak.api.ContentRepository;
 import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.jmx.SessionMBean;
+import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
 import org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate;
 import org.apache.jackrabbit.oak.jcr.session.RefreshStrategy;
 import org.apache.jackrabbit.oak.jcr.session.RefreshStrategy.Composite;
@@ -319,27 +320,12 @@ public class RepositoryImpl implements J
         statisticManager.dispose();
         gcMonitorRegistration.unregister();
         clock.close();
-        closeExecutor();
+        new ExecutorCloser(scheduledExecutor).close();
         if (contentRepository instanceof Closeable) {
             IOUtils.closeQuietly((Closeable) contentRepository);
         }
     }
 
-    private void closeExecutor() {
-        try {
-            scheduledExecutor.shutdown();
-            scheduledExecutor.awaitTermination(5, TimeUnit.SECONDS);
-        } catch (InterruptedException e) {
-            log.error("Error while shutting down the executorService", e);
-            Thread.currentThread().interrupt();
-        } finally {
-            if (!scheduledExecutor.isTerminated()) {
-                log.warn("executorService didn't shutdown properly. Will be 
forced now.");
-            }
-            scheduledExecutor.shutdownNow();
-        }
-    }
-
     //------------------------------------------------------------< internal 
>---
 
     /**


Reply via email to