Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7B9F991CF for ; Mon, 25 Jun 2012 09:20:18 +0000 (UTC) Received: (qmail 50467 invoked by uid 500); 25 Jun 2012 09:20:18 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 50329 invoked by uid 500); 25 Jun 2012 09:20:16 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 50291 invoked by uid 99); 25 Jun 2012 09:20:15 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jun 2012 09:20:15 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jun 2012 09:20:14 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E2A6D2388847; Mon, 25 Jun 2012 09:19:53 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1353440 - /sling/trunk/samples/slingbucks/src/main/java/org/apache/sling/slingbucks/server/ConfirmedOrdersObserver.java Date: Mon, 25 Jun 2012 09:19:53 -0000 To: commits@sling.apache.org From: bdelacretaz@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120625091953.E2A6D2388847@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bdelacretaz Date: Mon Jun 25 09:19:52 2012 New Revision: 1353440 URL: http://svn.apache.org/viewvc?rev=1353440&view=rev Log: SLING-2519 - use Sling Scheduling Modified: sling/trunk/samples/slingbucks/src/main/java/org/apache/sling/slingbucks/server/ConfirmedOrdersObserver.java Modified: sling/trunk/samples/slingbucks/src/main/java/org/apache/sling/slingbucks/server/ConfirmedOrdersObserver.java URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingbucks/src/main/java/org/apache/sling/slingbucks/server/ConfirmedOrdersObserver.java?rev=1353440&r1=1353439&r2=1353440&view=diff ============================================================================== --- sling/trunk/samples/slingbucks/src/main/java/org/apache/sling/slingbucks/server/ConfirmedOrdersObserver.java (original) +++ sling/trunk/samples/slingbucks/src/main/java/org/apache/sling/slingbucks/server/ConfirmedOrdersObserver.java Mon Jun 25 09:19:52 2012 @@ -35,7 +35,9 @@ import javax.jcr.observation.EventListen import javax.jcr.observation.ObservationManager; import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; import org.apache.sling.jcr.api.SlingRepository; import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; @@ -43,8 +45,20 @@ import org.slf4j.LoggerFactory; /** Move confirmed orders under CONFIRMED_ORDERS_PATH, * by observing changes under ORDERS_PATH + * + * To execute our run() method periodically to check + * if orders are ready to move, we use the Sling scheduler + * service with the whiteboard pattern: registering this + * class as a Runnable service and adding a property to + * define the running schedule is sufficient to execute + * the run() method regularly. */ @Component +@Service(value=Runnable.class) +@Properties({ + @org.apache.felix.scr.annotations.Property(name="scheduler.period", longValue=1), + @org.apache.felix.scr.annotations.Property(name="scheduler.concurrent", boolValue=false) +}) public class ConfirmedOrdersObserver implements EventListener, Runnable { private Logger log = LoggerFactory.getLogger(getClass()); @@ -56,15 +70,9 @@ public class ConfirmedOrdersObserver imp private Set changedPropertyPaths = new HashSet(); private static final long WAIT_AFTER_LAST_CHANGE_MSEC = 5000; - private boolean running; protected void activate(ComponentContext context) throws Exception { session = repository.loginAdministrative(null); - running = true; - - final Thread t = new Thread(this, getClass().getName()); - t.setDaemon(true); - t.start(); // Listen for changes to our orders if (repository.getDescriptor(Repository.OPTION_OBSERVATION_SUPPORTED).equals("true")) { @@ -80,7 +88,6 @@ public class ConfirmedOrdersObserver imp } protected void deactivate(ComponentContext componentContext) throws RepositoryException { - running = false; if(observationManager != null) { observationManager.removeEventListener(this); } @@ -92,7 +99,8 @@ public class ConfirmedOrdersObserver imp public void onEvent(EventIterator it) { while (it.hasNext()) { - // Accumulate the changed paths and store time of the last change event + // Just accumulate the changed paths - we'll do the actual work in + // a separate non-event method, as this should return quickly. try { final String path = it.nextEvent().getPath(); if(path.endsWith(SlingbucksConstants.CONFIRMED_ORDER_PROPERTY_NAME)) { @@ -107,7 +115,8 @@ public class ConfirmedOrdersObserver imp } } - private void runOneCycle() throws Exception { + /** Meant to be called often (every second maybe) by the Sling scheduler */ + public void run() { if(changedPropertyPaths.isEmpty()) { return; } @@ -143,6 +152,8 @@ public class ConfirmedOrdersObserver imp } } } + } catch(Exception e){ + log.error("Exception in run()", e); } finally { // Re-add any paths that we didn't process synchronized (changedPropertyPaths) { @@ -151,21 +162,4 @@ public class ConfirmedOrdersObserver imp } } } - - public void run() { - log.info("Background thread {} starting", Thread.currentThread().getName()); - while(running) { - try { - runOneCycle(); - } catch(Exception e) { - log.warn(e.getClass().getName() + " in background thread", e); - } - - try { - Thread.sleep(1000L); - } catch(InterruptedException ignore) { - } - } - log.info("Background thread {} done", Thread.currentThread().getName()); - } -} +} \ No newline at end of file