You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2014/01/15 15:40:06 UTC
svn commit: r1558397 - in
/sling/trunk/bundles/extensions/healthcheck/samples: ./
src/main/java/org/apache/sling/hc/samples/impl/
src/main/resources/SLING-CONTENT/apps/hc/demo/install/
Author: bdelacretaz
Date: Wed Jan 15 14:40:06 2014
New Revision: 1558397
URL: http://svn.apache.org/r1558397
Log:
SLING-3278 - add SlowHealthCheckSample to demonstrate timeouts and caching
Added:
sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/SlowHealthCheckSample.java
sling/trunk/bundles/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.SlowHealthCheckSample-1.json
Modified:
sling/trunk/bundles/extensions/healthcheck/samples/pom.xml
Modified: sling/trunk/bundles/extensions/healthcheck/samples/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/samples/pom.xml?rev=1558397&r1=1558396&r2=1558397&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/samples/pom.xml (original)
+++ sling/trunk/bundles/extensions/healthcheck/samples/pom.xml Wed Jan 15 14:40:06 2014
@@ -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>
Added: sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/SlowHealthCheckSample.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/SlowHealthCheckSample.java?rev=1558397&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/SlowHealthCheckSample.java (added)
+++ sling/trunk/bundles/extensions/healthcheck/samples/src/main/java/org/apache/sling/hc/samples/impl/SlowHealthCheckSample.java Wed Jan 15 14:40:06 2014
@@ -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
Added: sling/trunk/bundles/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.SlowHealthCheckSample-1.json
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.SlowHealthCheckSample-1.json?rev=1558397&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.SlowHealthCheckSample-1.json (added)
+++ sling/trunk/bundles/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.SlowHealthCheckSample-1.json Wed Jan 15 14:40:06 2014
@@ -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"
+}