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 a47122784efa05965643bdc7b736c3b2c93a6633
Author: Bertrand Delacretaz <[email protected]>
AuthorDate: Wed Jul 30 13:23:12 2014 +0000

    SLING-3744 - optionally schedule execution of health checks via cron-like 
service property. Contributed by Georg Henzler, thanks!
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1614655 
13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../hc/samples/impl/AsyncHealthCheckSample.java    | 50 ++++++++++------------
 ...g.hc.samples.impl.AsyncHealthCheckSample-1.json |  3 +-
 3 files changed, 25 insertions(+), 30 deletions(-)

diff --git a/pom.xml b/pom.xml
index e290da8..0edd9e0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -85,7 +85,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.hc.core</artifactId>
-            <version>1.0.4</version>
+            <version>1.1.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
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 67af69b..0da48dd 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
@@ -43,39 +43,23 @@ import org.slf4j.LoggerFactory;
     @Property(name=HealthCheck.NAME),
     @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY),
     @Property(name=HealthCheck.MBEAN_NAME),
-    
-    // Period *must* be a Long
-    @Property(name="scheduler.period", 
longValue=AsyncHealthCheckSample.PERIOD_SECONDS, propertyPrivate=true),
-    // Concurrent=false avoids reentrant calls to run()
-    @Property(name="scheduler.concurrent", boolValue=false)
+    @Property(name=HealthCheck.ASYNC_CRON_EXPRESSION)
+
 })
-@Service(value={HealthCheck.class,Runnable.class})
-public class AsyncHealthCheckSample implements HealthCheck, Runnable {
+@Service
+public class AsyncHealthCheckSample implements HealthCheck {
 
-    private final Logger log = LoggerFactory.getLogger(getClass());
-    private final AtomicInteger counter = new AtomicInteger();
+    private final Logger log = 
LoggerFactory.getLogger(AsyncHealthCheckSample.class);
+    
+    // static because for factories, not always the same instance is returned 
for 
+    // the same service reference
+    private static final AtomicInteger counter = new AtomicInteger();
     
     public static final int PERIOD_SECONDS = 5;
     
     @Override
     public Result execute() {
-        final FormattingResultLog resultLog = new FormattingResultLog();
-        final int value = counter.get();
-        resultLog.debug("{} - counter value is {}", this, value);
-        if(value % 2 != 0) {
-            resultLog.warn("Counter value ({}) is not even", value);
-        }
-        return new Result(resultLog);
-    }
- 
-    /** Called by the Sling scheduler, every {@link #SCHEDULER_PERIOD} 
seconds, without
-     *  reentrant calls, as configured by our scheduler.* service properties.
-     *  
-     *  Simulates an expensive operation by waiting a random time up to twice 
that period
-     *  before incrementing our counter.
-     */
-    @Override
-    public void run() {
+        
         final long toWait = (long)(Math.random() * 2 * PERIOD_SECONDS);
         log.info("{} - Waiting {} seconds to simulate an expensive 
operation...", this, toWait);
         try {
@@ -83,7 +67,17 @@ public class AsyncHealthCheckSample implements HealthCheck, 
Runnable {
         } catch(InterruptedException iex) {
             log.warn("Sleep interrupted", iex);
         }
-        counter.incrementAndGet();
-        log.info("{} - counter set to {}", this, counter.get());
+
+        final int value =  counter.incrementAndGet();
+        log.info("{} - counter set to {}", this, value);
+        
+        final FormattingResultLog resultLog = new FormattingResultLog();
+
+        resultLog.debug("{} - counter value is {}", this, value);
+        if(value % 2 != 0) {
+            resultLog.warn("Counter value ({}) is not even", value);
+        }
+        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.AsyncHealthCheckSample-1.json
 
b/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.samples.impl.AsyncHealthCheckSample-1.json
index 9984ce5..177f374 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
@@ -2,5 +2,6 @@
     "jcr:primaryType" : "sling:OsgiConfig",
     "hc.name" : "Asynchronous Health Check sample", 
     "hc.tags" : [async],
-    "hc.mbean.name" : "AsyncHealthCheckSample" 
+    "hc.mbean.name" : "AsyncHealthCheckSample", 
+    "hc.async.cronExpression" : "*/20 * * * * ?" 
 }

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to