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/10/20 14:39:16 UTC

[sling-org-apache-sling-hc-samples] 41/42: SLING-6855 - second AsyncHealthCheckSample with sticky tag, configured for sticky results

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

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hc-samples.git

commit 57c1b6a8bf136a3e9bcfaf61bf83b041fd68c7af
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Jun 9 13:53:44 2017 +0000

    SLING-6855 - second AsyncHealthCheckSample with sticky tag, configured for sticky results
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1798219 13f79535-47bb-0310-9956-ffa450edef68
---
 .../hc/samples/impl/AsyncHealthCheckSample.java    | 30 +++++++++++-----------
 ...g.hc.samples.impl.AsyncHealthCheckSample-1.json |  2 +-
 ...g.hc.samples.impl.AsyncHealthCheckSample-2.json |  8 ++++++
 3 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/sling/hc/samples/impl/AsyncHealthCheckSample.java b/src/main/java/org/apache/sling/hc/samples/impl/AsyncHealthCheckSample.java
index 41fa248..670363e 100644
--- a/src/main/java/org/apache/sling/hc/samples/impl/AsyncHealthCheckSample.java
+++ b/src/main/java/org/apache/sling/hc/samples/impl/AsyncHealthCheckSample.java
@@ -18,7 +18,6 @@
 package org.apache.sling.hc.samples.impl;
 
 import java.util.Date;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.felix.scr.annotations.ConfigurationPolicy;
 import org.apache.felix.scr.annotations.Service;
@@ -41,32 +40,33 @@ import org.slf4j.LoggerFactory;
 public class AsyncHealthCheckSample implements HealthCheck {
 
     private final Logger log = LoggerFactory.getLogger(AsyncHealthCheckSample.class);
-    
-    private final AtomicInteger counter = new AtomicInteger();
-    
-    public static final int PERIOD_SECONDS = 5;
+    private static final long start = System.currentTimeMillis();
     
     @Override
     public Result execute() {
         
-        final long toWait = (long)(Math.random() * 2 * PERIOD_SECONDS);
-        log.info("{} - Waiting {} seconds to simulate an expensive operation...", this, toWait);
-        try {
-            Thread.sleep(toWait * 1000L);
-        } catch(InterruptedException iex) {
-            log.warn("Sleep interrupted", iex);
-        }
-
-        final int value =  counter.incrementAndGet();
+        final long value = secondsFromStart();
         log.info("{} - counter set to {}", this, value);
         
         final FormattingResultLog resultLog = new FormattingResultLog();
 
         resultLog.info("{} - counter value set to {} at {}", this, value, new Date());
         if(value % 2 != 0) {
-            resultLog.warn("Counter value ({}) is not even", value);
+            // Generate various states as examples
+            final String template = "Counter value ({}) is not {} at {}";
+            if(value % 3 == 0) {
+                resultLog.critical(template, value, "a multiple of 3 (critical)", new Date());
+            } else if(value % 5 == 0) {
+                resultLog.healthCheckError(template, value, "a multiple of 5 (healthCheckError)", new Date());
+            } else {
+                resultLog.warn(template, value, "even (warn)", new Date());
+            }
         }
         return new Result(resultLog);
     }
+    
+    private long secondsFromStart() {
+        return (System.currentTimeMillis() - start) / 1000L;
+    }
  
 }
\ No newline at end of file
diff --git a/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.AsyncHealthCheckSample-1.json b/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.AsyncHealthCheckSample-1.json
index 14ae4cd..a97588e 100644
--- a/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.AsyncHealthCheckSample-1.json
+++ b/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.AsyncHealthCheckSample-1.json
@@ -3,5 +3,5 @@
     "hc.name" : "Asynchronous Health Check sample", 
     "hc.tags" : ["async"],
     "hc.mbean.name" : "AsyncHealthCheckSample", 
-    "hc.async.cronExpression" : "*/5 * * * * ?" 
+    "hc.async.cronExpression" : "*/1 * * * * ?" 
 }
diff --git a/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.AsyncHealthCheckSample-2.json b/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.AsyncHealthCheckSample-2.json
new file mode 100644
index 0000000..6ace60f
--- /dev/null
+++ b/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.AsyncHealthCheckSample-2.json
@@ -0,0 +1,8 @@
+{
+    "jcr:primaryType" : "sling:OsgiConfig",
+    "hc.name" : "Sticky Asynchronous Health Check sample", 
+    "hc.tags" : ["async", "sticky"],
+    "hc.mbean.name" : "StickyAsyncHealthCheckSample", 
+    "hc.async.cronExpression" : "*/1 * * * * ?", 
+    "hc.warningsStickForMinutes" : 1
+}

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