You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/02/13 14:15:38 UTC

svn commit: r627395 - in /incubator/sling/trunk/sling/event/src: main/java/org/apache/sling/event/impl/ test/java/org/apache/sling/event/impl/

Author: cziegeler
Date: Wed Feb 13 05:15:35 2008
New Revision: 627395

URL: http://svn.apache.org/viewvc?rev=627395&view=rev
Log:
SLING-177: Only schedule distributing event handler.

Modified:
    incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
    incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java
    incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
    incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java
    incubator/sling/trunk/sling/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java

Modified: incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java?rev=627395&r1=627394&r2=627395&view=diff
==============================================================================
--- incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java (original)
+++ incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java Wed Feb 13 05:15:35 2008
@@ -31,11 +31,9 @@
 import java.util.concurrent.LinkedBlockingQueue;
 
 import javax.jcr.Node;
-import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.observation.EventListener;
-import javax.jcr.query.Query;
 
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.sling.event.EventUtil;
@@ -52,14 +50,9 @@
  *
  * @scr.component abstract="true"
  * @scr.service interface="org.osgi.service.event.EventHandler"
- * We schedule the job event handler to run in the background and clean up
- * obsolete jobs:
- * @scr.service interface="java.lang.Runnable"
- * @scr.property name="scheduler.period" value="1800" type="Long"
- * @scr.property name="scheduler.concurrent" value="false" type="Boolean"
  */
 public abstract class AbstractRepositoryEventHandler
-    implements EventHandler, EventListener, Runnable {
+    implements EventHandler, EventListener {
 
     /** FIXME - This is a copy from the sling core constants to avoid
      * a dependency just for the constant. We will move this into an
@@ -70,9 +63,6 @@
     /** Default log. */
     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
-    /** @scr.property value="30" type="Integer" */
-    protected static final String CONFIG_PROPERTY_CLEANUP_PERIOD = "cleanup.period";
-
     /** @scr.property value="/sling/events" */
     protected static final String CONFIG_PROPERTY_REPO_PATH = "repository.path";
 
@@ -91,9 +81,6 @@
     /** The path in the repository. */
     protected String repositoryPath;
 
-    /** We remove everything which is older than 30min by default. */
-    protected int cleanupPeriod = 30;
-
     /** Is the background task still running? */
     protected boolean running;
 
@@ -112,10 +99,6 @@
     throws RepositoryException {
         this.applicationId = context.getBundleContext().getProperty(SLING_ID);
         this.repositoryPath = (String)context.getProperties().get(CONFIG_PROPERTY_REPO_PATH);
-        final Integer i = (Integer)context.getProperties().get(CONFIG_PROPERTY_CLEANUP_PERIOD);
-        if ( i != null ) {
-            this.cleanupPeriod = i;
-        }
 
         // start background threads
         this.running = true;
@@ -149,54 +132,9 @@
         t2.start();
     }
 
-    /**
-     * This method is invoked periodically.
-     * @see java.lang.Runnable#run()
-     */
-    public void run() {
-        if ( this.cleanupPeriod > 0 ) {
-            this.logger.debug("Cleaning up repository, removing all entries older than {} minutes.", this.cleanupPeriod);
-
-            final String queryString = this.getCleanUpQueryString();
-            if ( queryString != null ) {
-                // we create an own session for concurrency issues
-                Session s = null;
-                try {
-                    s = this.createSession();
-                    final Node parentNode = (Node)s.getItem(this.repositoryPath);
-                    logger.debug("Executing query {}", queryString);
-                    final Query q = s.getWorkspace().getQueryManager().createQuery(queryString, Query.XPATH);
-                    final NodeIterator iter = q.execute().getNodes();
-                    int count = 0;
-                    while ( iter.hasNext() ) {
-                        final Node eventNode = iter.nextNode();
-                        eventNode.remove();
-                        count++;
-                    }
-                    parentNode.save();
-                    logger.debug("Removed {} entries from the repository.", count);
-
-                } catch (RepositoryException e) {
-                    // in the case of an error, we just log this as a warning
-                    this.logger.warn("Exception during repository cleanup.", e);
-                } finally {
-                    if ( s != null ) {
-                        s.logout();
-                    }
-                }
-            }
-        }
-    }
-
     protected abstract void runInBackground();
 
     protected abstract void processWriteQueue();
-
-    /**
-     * The query to detect old entries which can be removed.
-     * This method is invoked periodically from the {@link #run()} method.
-     */
-    protected abstract String getCleanUpQueryString();
 
     /**
      * Deactivate this component.

Modified: incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java?rev=627395&r1=627394&r2=627395&view=diff
==============================================================================
--- incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java (original)
+++ incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java Wed Feb 13 05:15:35 2008
@@ -22,12 +22,15 @@
 import java.util.Dictionary;
 
 import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.observation.EventIterator;
+import javax.jcr.query.Query;
 
 import org.apache.jackrabbit.util.ISO8601;
 import org.apache.sling.event.EventUtil;
+import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventAdmin;
 
@@ -37,9 +40,39 @@
  * @scr.property name="event.topics" value="*"
  * @scr.property name="event.filter" value="(event.distribute=*)"
  * @scr.property name="repository.path" value="/sling/events"
+ *
+ * We schedule this event handler to run in the background and clean up
+ * obsolete events.
+ * @scr.service interface="java.lang.Runnable"
+ * @scr.property name="scheduler.period" value="1800" type="Long"
+ * @scr.property name="scheduler.concurrent" value="false" type="Boolean"
  */
 public class DistributingEventHandler
-    extends AbstractRepositoryEventHandler {
+    extends AbstractRepositoryEventHandler
+    implements Runnable {
+
+    /** Default clean up time is 30 minutes. */
+    protected static final int DEFAULT_CLEANUP_PERIOD = 30;
+
+    /** @scr.property valueRef="DEFAULT_CLEANUP_PERIOD" type="Integer" */
+    protected static final String CONFIG_PROPERTY_CLEANUP_PERIOD = "cleanup.period";
+
+    /** We remove everything which is older than 30min by default. */
+    protected int cleanupPeriod = DEFAULT_CLEANUP_PERIOD;
+
+    /**
+     * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#activate(org.osgi.service.component.ComponentContext)
+     */
+    protected void activate(ComponentContext context)
+    throws RepositoryException {
+        final Integer i = (Integer)context.getProperties().get(CONFIG_PROPERTY_CLEANUP_PERIOD);
+        if ( i != null ) {
+            this.cleanupPeriod = i;
+        } else {
+            this.cleanupPeriod = DEFAULT_CLEANUP_PERIOD;
+        }
+        super.activate(context);
+    }
 
     /**
      * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#getCleanUpQueryString()
@@ -60,6 +93,45 @@
         buffer.append("')]");
 
         return buffer.toString();
+    }
+
+    /**
+     * This method is invoked periodically.
+     * @see java.lang.Runnable#run()
+     */
+    public void run() {
+        if ( this.cleanupPeriod > 0 ) {
+            this.logger.debug("Cleaning up repository, removing all entries older than {} minutes.", this.cleanupPeriod);
+
+            final String queryString = this.getCleanUpQueryString();
+            if ( queryString != null ) {
+                // we create an own session for concurrency issues
+                Session s = null;
+                try {
+                    s = this.createSession();
+                    final Node parentNode = (Node)s.getItem(this.repositoryPath);
+                    logger.debug("Executing query {}", queryString);
+                    final Query q = s.getWorkspace().getQueryManager().createQuery(queryString, Query.XPATH);
+                    final NodeIterator iter = q.execute().getNodes();
+                    int count = 0;
+                    while ( iter.hasNext() ) {
+                        final Node eventNode = iter.nextNode();
+                        eventNode.remove();
+                        count++;
+                    }
+                    parentNode.save();
+                    logger.debug("Removed {} entries from the repository.", count);
+
+                } catch (RepositoryException e) {
+                    // in the case of an error, we just log this as a warning
+                    this.logger.warn("Exception during repository cleanup.", e);
+                } finally {
+                    if ( s != null ) {
+                        s.logout();
+                    }
+                }
+            }
+        }
     }
 
     /**

Modified: incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java?rev=627395&r1=627394&r2=627395&view=diff
==============================================================================
--- incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java (original)
+++ incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java Wed Feb 13 05:15:35 2008
@@ -197,13 +197,6 @@
     }
 
     /**
-     * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#getCleanUpQueryString()
-     */
-    protected String getCleanUpQueryString() {
-        return null;
-    }
-
-    /**
      * This method runs in the background and processes the local queue.
      */
     protected void runInBackground() {
@@ -377,6 +370,7 @@
                                 final Set<String> newUnloadedJobs = new HashSet<String>();
                                 newUnloadedJobs.addAll(unloadedJobs);
                                 try {
+                                    s = createSession();
                                     for(String path : unloadedJobs ) {
                                         newUnloadedJobs.remove(path);
                                         try {
@@ -405,6 +399,9 @@
                                             ignoreException(re);
                                         }
                                     }
+                                } catch (RepositoryException re) {
+                                    // unable to create session, so we try it again next time
+                                    ignoreException(re);
                                 } finally {
                                     if ( s != null ) {
                                         s.logout();

Modified: incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java?rev=627395&r1=627394&r2=627395&view=diff
==============================================================================
--- incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java (original)
+++ incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java Wed Feb 13 05:15:35 2008
@@ -51,7 +51,7 @@
 /**
  * An event handler for timed events.
  *
- * @scr.component inherit="true"
+ * @scr.component
  * @scr.property name="event.topics" refValues="EventUtil.TOPIC_TIMED_EVENT"
  *               values.updated="org/osgi/framework/BundleEvent/UPDATED"
  *               values.started="org/osgi/framework/BundleEvent/STARTED"
@@ -85,14 +85,6 @@
     }
 
     /**
-     * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#getCleanUpQueryString()
-     */
-    protected String getCleanUpQueryString() {
-        // nothing to clean up for now
-        return null;
-    }
-
-    /**
      * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#processWriteQueue()
      */
     protected void processWriteQueue() {
@@ -426,13 +418,6 @@
      * @see org.apache.sling.scheduler.Job#execute(org.apache.sling.scheduler.JobContext)
      */
     public void execute(JobContext context) {
-        // as we are called periodically as well, we have to check the configuration first
-        if ( context.getConfiguration() == null ) {
-            // period call, just invoke run
-            this.run();
-            return;
-        }
-
         final String topic = (String) context.getConfiguration().get(JOB_TOPIC);
         @SuppressWarnings("unchecked")
         final Dictionary<Object, Object> properties = (Dictionary<Object, Object>) context.getConfiguration().get(JOB_CONFIG);

Modified: incubator/sling/trunk/sling/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java?rev=627395&r1=627394&r2=627395&view=diff
==============================================================================
--- incubator/sling/trunk/sling/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java (original)
+++ incubator/sling/trunk/sling/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java Wed Feb 13 05:15:35 2008
@@ -49,7 +49,6 @@
 
     protected static final String REPO_PATH = "/test/events";
     protected static final String SLING_ID = "4711";
-    protected static final int CLEANUP_PERIOD = 10000;
 
     protected static Session session;
 
@@ -58,7 +57,6 @@
     protected Dictionary<String, Object> getComponentConfig() {
         final Dictionary<String, Object> config = new Hashtable<String, Object>();
         config.put(AbstractRepositoryEventHandler.CONFIG_PROPERTY_REPO_PATH, REPO_PATH);
-        config.put(AbstractRepositoryEventHandler.CONFIG_PROPERTY_CLEANUP_PERIOD, CLEANUP_PERIOD);
 
         return config;
     }
@@ -124,7 +122,6 @@
 
     @org.junit.Test public void testSetup() throws RepositoryException {
         assertEquals(this.handler.applicationId, SLING_ID);
-        assertEquals(this.handler.cleanupPeriod, CLEANUP_PERIOD);
         assertEquals(this.handler.repositoryPath, REPO_PATH);
         assertNotNull(this.handler.writerSession);
         final EventListenerIterator iter = this.handler.writerSession.getWorkspace().getObservationManager().getRegisteredEventListeners();