You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:26:10 UTC

[sling-org-apache-sling-discovery-base] 13/20: SLING-5284 : use dedicated PeriodicBackgroundJob-thread instead of scheduler

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.discovery.base-1.1.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-discovery-base.git

commit a581e02af7c980b577e02f3bf2a995b93c6a6125
Author: Stefan Egli <st...@apache.org>
AuthorDate: Mon Nov 9 16:48:47 2015 +0000

    SLING-5284 : use dedicated PeriodicBackgroundJob-thread instead of scheduler
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/discovery/base@1713477 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../discovery/base/commons/BaseViewChecker.java    | 10 ++-
 .../base/commons/PeriodicBackgroundJob.java        | 99 ++++++++++++++++++++++
 .../discovery/base/its/AbstractClusterTest.java    |  2 +-
 4 files changed, 108 insertions(+), 5 deletions(-)

diff --git a/pom.xml b/pom.xml
index 521faff..53aad47 100644
--- a/pom.xml
+++ b/pom.xml
@@ -29,7 +29,7 @@
 
     <artifactId>org.apache.sling.discovery.base</artifactId>
     <packaging>bundle</packaging>
-    <version>1.0.3-SNAPSHOT</version>
+    <version>1.1.0-SNAPSHOT</version>
 
     <name>Apache Sling Discovery Base</name>
     <description>Contains Connector and Properties support that some implementations might choose to build upon</description>
diff --git a/src/main/java/org/apache/sling/discovery/base/commons/BaseViewChecker.java b/src/main/java/org/apache/sling/discovery/base/commons/BaseViewChecker.java
index ad1043a..ce15255 100644
--- a/src/main/java/org/apache/sling/discovery/base/commons/BaseViewChecker.java
+++ b/src/main/java/org/apache/sling/discovery/base/commons/BaseViewChecker.java
@@ -89,6 +89,8 @@ public abstract class BaseViewChecker implements ViewChecker, Runnable, StartupL
     /** SLING-4765 : store endpoints to /clusterInstances for more verbose duplicate slingId/ghost detection **/
     protected final Map<Long, String[]> endpoints = new HashMap<Long, String[]>();
 
+    protected PeriodicBackgroundJob periodicPingJob;
+    
     public void inform(StartupMode mode, boolean finished) {
     	if (finished) {
     		startupFinished(mode);
@@ -135,8 +137,7 @@ public abstract class BaseViewChecker implements ViewChecker, Runnable, StartupL
         try {
             final long interval = getConnectorConfig().getConnectorPingInterval();
             logger.info("doActivate: starting periodic connectorPing job for "+slingId+" with interval "+interval+" sec.");
-            getScheduler().addPeriodicJob(NAME, this,
-                    null, interval, false);
+            periodicPingJob = new PeriodicBackgroundJob(interval, NAME, this);
         } catch (Exception e) {
             logger.error("doActivate: Could not start connectorPing runner: " + e, e);
         }
@@ -148,7 +149,10 @@ public abstract class BaseViewChecker implements ViewChecker, Runnable, StartupL
         // SLING-3365 : dont synchronize on deactivate
         activated = false;
         logger.info("deactivate: deactivated slingId: {}, this: {}", slingId, this);
-    	getScheduler().removeJob(NAME);
+        if (periodicPingJob != null) {
+            periodicPingJob.stop();
+            periodicPingJob = null;
+        }
     }
     
     /** for testing only **/
diff --git a/src/main/java/org/apache/sling/discovery/base/commons/PeriodicBackgroundJob.java b/src/main/java/org/apache/sling/discovery/base/commons/PeriodicBackgroundJob.java
new file mode 100644
index 0000000..4d58ace
--- /dev/null
+++ b/src/main/java/org/apache/sling/discovery/base/commons/PeriodicBackgroundJob.java
@@ -0,0 +1,99 @@
+/*
+ * 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.sling.discovery.base.commons;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Simple executor of a background job that periodically
+ * invokes a particular runnable - catching RuntimeExceptions
+ * that might throw - but not catching Errors (that terminates
+ * the BackgroundJob).
+ */
+public class PeriodicBackgroundJob implements Runnable {
+
+    private final static Logger logger = LoggerFactory.getLogger(PeriodicBackgroundJob.class);
+
+    private final long intervalSeconds;
+    private final Runnable runnable;
+
+    private final String threadName;
+    
+    private volatile boolean stopping = false;
+    private volatile boolean stopped = false;
+
+    public PeriodicBackgroundJob(long intervalSeconds, String threadName, Runnable runnable) {
+        this.intervalSeconds = intervalSeconds;
+        this.runnable = runnable;
+        this.threadName = threadName;
+        Thread th = new Thread(this, threadName);
+        th.setDaemon(true);
+        th.start();
+    }
+    
+    public void stop() {
+        stopping = true;
+    }
+    
+    public boolean isStopping() {
+        return stopping;
+    }
+    
+    public boolean isStopped() {
+        return stopped;
+    }
+
+    @Override
+    public void run() {
+        try{
+            while(!stopping) {
+                // first sleep
+                try {
+                    Thread.sleep(intervalSeconds * 1000);
+                } catch (InterruptedException e) {
+                    logger.info("run: got interrupted: "+e, e);
+                }
+                if (stopping) {
+                    break;
+                }
+                // then execute if not stopping
+                safelyRun(runnable);
+            }
+        } finally {
+            stopped = true;
+        }
+    }
+
+    private void safelyRun(Runnable r) {
+        try{
+            r.run();
+        } catch(RuntimeException re) {
+            // for RuntimeExceptions it's ok to catch them
+            // so do so, log and continue
+            logger.error("safelyRun: got a RuntimeException executing '"+threadName+"': "+re, re);
+        } catch(Error er) {
+            // for Errors it is not ok to catch them,
+            // so log, but re-throw
+            logger.error("safelyRun: got an Error executing '"+threadName+"'. BackgroundJob will terminate! "+er, er);
+            throw er;
+        }
+    }
+    
+}
diff --git a/src/test/java/org/apache/sling/discovery/base/its/AbstractClusterTest.java b/src/test/java/org/apache/sling/discovery/base/its/AbstractClusterTest.java
index 6690bb0..0608440 100644
--- a/src/test/java/org/apache/sling/discovery/base/its/AbstractClusterTest.java
+++ b/src/test/java/org/apache/sling/discovery/base/its/AbstractClusterTest.java
@@ -1314,7 +1314,7 @@ public abstract class AbstractClusterTest {
         instance2.heartbeatsAndCheckView();
         instance3.heartbeatsAndCheckView();
         logger.info("testAdditionalInstance: 4th 2s sleep");
-        Thread.sleep(2000);
+        Thread.sleep(3000);
         assertEquals(1, acceptsMultiple.getEventCnt(Type.TOPOLOGY_CHANGING));
         assertEquals(1, acceptsMultiple.getEventCnt(Type.TOPOLOGY_CHANGED));
         logger.info("testAdditionalInstance: end");

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.