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:38:49 UTC

[sling-org-apache-sling-hc-samples] 14/42: SLING-3278 - add SlowHealthCheckSample to demonstrate timeouts and caching

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 ade1794f821084523c5b0baa02da01814747e4ba
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed Jan 15 14:40:06 2014 +0000

    SLING-3278 - add SlowHealthCheckSample to demonstrate timeouts and caching
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1558397 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  6 ++
 .../hc/samples/impl/SlowHealthCheckSample.java     | 91 ++++++++++++++++++++++
 ...ng.hc.samples.impl.SlowHealthCheckSample-1.json |  8 ++
 3 files changed, 105 insertions(+)

diff --git a/pom.xml b/pom.xml
index ac74d91..ac31eb9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -96,6 +96,12 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.osgi</artifactId>
+            <version>2.2.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.scripting.api</artifactId>
             <version>2.1.0</version>
             <scope>provided</scope>
diff --git a/src/main/java/org/apache/sling/hc/samples/impl/SlowHealthCheckSample.java b/src/main/java/org/apache/sling/hc/samples/impl/SlowHealthCheckSample.java
new file mode 100644
index 0000000..614b5b2
--- /dev/null
+++ b/src/main/java/org/apache/sling/hc/samples/impl/SlowHealthCheckSample.java
@@ -0,0 +1,91 @@
+/*
+ * 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 SF 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.hc.samples.impl;
+
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.PropertyUnbounded;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.util.FormattingResultLog;
+
+/** Sample Health Check that takes N msec to execute,
+ *  used to demonstrate execution timeouts and caching.
+ */
+@Component(
+        configurationFactory=true,
+        policy=ConfigurationPolicy.REQUIRE,
+        metatype=true)
+@Properties({
+    @Property(name=HealthCheck.NAME),
+    @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY),
+    @Property(name=HealthCheck.MBEAN_NAME),
+})
+@Service(value=HealthCheck.class)
+public class SlowHealthCheckSample implements HealthCheck{
+
+    private final AtomicInteger counter = new AtomicInteger();
+    private long minExecutionTime;
+    private long maxExecutionTime;
+    
+    @Property(label="Minimum execution time",
+            description="Shortest execution time in milliseconds",
+            intValue=SlowHealthCheckSample.DEFAULT_MIN_EXEC_TIME)
+    private static final String PROP_MIN_EXEC_TIME = "execution.time.min.msec";
+    private static final int DEFAULT_MIN_EXEC_TIME = 1000;
+    
+    @Property(label="Maximum execution time",
+            description="Longest execution time in milliseconds",
+            intValue=SlowHealthCheckSample.DEFAULT_MAX_EXEC_TIME)
+    private static final String PROP_MAX_EXEC_TIME = "execution.time.max.msec";
+    private static final int DEFAULT_MAX_EXEC_TIME = 5000;
+    
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + ", execution time=" + minExecutionTime + ".." + maxExecutionTime + " msec";
+    }
+    
+    @Activate
+    protected void activate(Map<String, Object> config) {
+        minExecutionTime = PropertiesUtil.toInteger(config.get(PROP_MIN_EXEC_TIME), DEFAULT_MIN_EXEC_TIME);
+        maxExecutionTime = PropertiesUtil.toInteger(config.get(PROP_MAX_EXEC_TIME), DEFAULT_MAX_EXEC_TIME);
+    }
+    
+    @Override
+    public Result execute() {
+        final FormattingResultLog resultLog = new FormattingResultLog();
+        try {
+            final long randomDelay = (long)(Math.random() * (maxExecutionTime - minExecutionTime));
+            final long toSleep = minExecutionTime + randomDelay;
+            resultLog.debug("Executing {} will last {} msec", this, toSleep);
+            Thread.sleep(toSleep);
+        } catch(InterruptedException iex) {
+            resultLog.warn("{} during execution", iex.getClass().getSimpleName());
+        }
+        resultLog.debug("Done executing, {} has been executed {} times", this, counter.incrementAndGet());
+        return new Result(resultLog);
+    }
+}
\ No newline at end of file
diff --git a/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.SlowHealthCheckSample-1.json b/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.SlowHealthCheckSample-1.json
new file mode 100644
index 0000000..f6cd0c1
--- /dev/null
+++ b/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.SlowHealthCheckSample-1.json
@@ -0,0 +1,8 @@
+{
+    "jcr:primaryType" : "sling:OsgiConfig",
+    "hc.name" : "Slow Health Check sample (1200-3700 msec)", 
+    "hc.tags" : [slow],
+    "hc.mbean.name" : "SlowHealthCheckSample",
+    "execution.time.min.msec" : "1200", 
+    "execution.time.max.msec" : "3700" 
+}

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