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/12 14:29:36 UTC

svn commit: r620792 - in /incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl: AbstractRepositoryEventHandler.java JobEventHandler.java TimedEventHandler.java

Author: cziegeler
Date: Tue Feb 12 05:29:33 2008
New Revision: 620792

URL: http://svn.apache.org/viewvc?rev=620792&view=rev
Log:
SLING-177: Don't bring the threads down just because of initial load errors.

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/JobEventHandler.java
    incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.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=620792&r1=620791&r2=620792&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 Tue Feb 12 05:29:33 2008
@@ -129,13 +129,21 @@
                     logger.error("Error during session starting.", e);
                     running = false;
                 }
-                processWriteQueue();
+                try {
+                    processWriteQueue();
+                } catch (Throwable t) {
+                    logger.error("Writer thread stopped with exception: " + t.getMessage(), t);
+                }
             }
         };
         t.start();
         final Thread t2 = new Thread() {
             public void run() {
-                runInBackground();
+                try {
+                    runInBackground();
+                } catch (Throwable t) {
+                    logger.error("Background thread stopped with exception: " + t.getMessage(), t);
+                }
             }
         };
         t2.start();
@@ -369,7 +377,7 @@
      * @throws ClassNotFoundException
      */
     protected Event readEvent(Node eventNode)
-    throws RepositoryException {
+    throws RepositoryException, ClassNotFoundException {
         final String topic = eventNode.getProperty(EventHelper.NODE_PROPERTY_TOPIC).getString();
         final Dictionary<String, Object> properties = new Hashtable<String, Object>();
         if ( eventNode.hasProperty(EventHelper.NODE_PROPERTY_PROPERTIES) ) {
@@ -383,8 +391,6 @@
                 }
             } catch (IOException ioe) {
                 throw new RepositoryException("Unable to deserialize event properties.", ioe);
-            } catch (ClassNotFoundException e) {
-                throw new RepositoryException("Unable to deserialize event properties.", e);
             }
         }
         this.addEventProperties(eventNode, properties);

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=620792&r1=620791&r2=620792&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 Tue Feb 12 05:29:33 2008
@@ -22,9 +22,11 @@
 import java.util.Collection;
 import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.UUID;
 
 import javax.jcr.ItemExistsException;
@@ -72,6 +74,9 @@
     /** Background session. */
     protected Session backgroundSession;
 
+    /** Unloaded jobs. */
+    protected Set<String>unloadedJobs = new HashSet<String>();
+
     /**
      * Activate this component.
      * @param context
@@ -452,14 +457,20 @@
                         if ( "jcr:lockOwner".equals(propertyName) ) {
                             final Node eventNode = (Node) s.getItem(nodePath);
                             if ( !eventNode.isLocked() ) {
-                                final EventInfo info = new EventInfo();
-                                info.event = this.readEvent(eventNode);
-                                info.nodePath = nodePath;
                                 try {
-                                    this.queue.put(info);
-                                } catch (InterruptedException e) {
-                                    // we ignore this exception as this should never occur
-                                    this.ignoreException(e);
+                                    final EventInfo info = new EventInfo();
+                                    info.event = this.readEvent(eventNode);
+                                    info.nodePath = nodePath;
+                                    try {
+                                        this.queue.put(info);
+                                    } catch (InterruptedException e) {
+                                        // we ignore this exception as this should never occur
+                                        this.ignoreException(e);
+                                    }
+                                } catch (ClassNotFoundException cnfe) {
+                                    // store path for lazy loading
+                                    this.unloadedJobs.add(nodePath);
+                                    this.ignoreException(cnfe);
                                 }
                             }
                         }
@@ -481,29 +492,42 @@
      * Load all active jobs from the repository.
      * @throws RepositoryException
      */
-    protected void loadJobs() throws RepositoryException {
-        final QueryManager qManager = this.writerSession.getWorkspace().getQueryManager();
-        final StringBuffer buffer = new StringBuffer("/jcr:root");
-        buffer.append(this.repositoryPath);
-        buffer.append("//element(*, ");
-        buffer.append(this.getEventNodeType());
-        buffer.append(")");
-        final Query q = qManager.createQuery(buffer.toString(), Query.XPATH);
-        final NodeIterator result = q.execute().getNodes();
-        while ( result.hasNext() ) {
-            final Node eventNode = result.nextNode();
-            if ( !eventNode.isLocked() ) {
-                final Event event = this.readEvent(eventNode);
-                final EventInfo info = new EventInfo();
-                info.event = event;
-                info.nodePath = eventNode.getPath();
-                try {
-                    this.queue.put(info);
-                } catch (InterruptedException e) {
-                    // we ignore this exception as this should never occur
-                    this.ignoreException(e);
+    protected void loadJobs() {
+        try {
+            final QueryManager qManager = this.writerSession.getWorkspace().getQueryManager();
+            final StringBuffer buffer = new StringBuffer("/jcr:root");
+            buffer.append(this.repositoryPath);
+            buffer.append("//element(*, ");
+            buffer.append(this.getEventNodeType());
+            buffer.append(")");
+            final Query q = qManager.createQuery(buffer.toString(), Query.XPATH);
+            final NodeIterator result = q.execute().getNodes();
+            while ( result.hasNext() ) {
+                final Node eventNode = result.nextNode();
+                if ( !eventNode.isLocked() ) {
+                    final String nodePath = eventNode.getPath();
+                    try {
+                        final Event event = this.readEvent(eventNode);
+                        final EventInfo info = new EventInfo();
+                        info.event = event;
+                        info.nodePath = nodePath;
+                        try {
+                            this.queue.put(info);
+                        } catch (InterruptedException e) {
+                            // we ignore this exception as this should never occur
+                            this.ignoreException(e);
+                        }
+                    } catch (ClassNotFoundException cnfe) {
+                        // store path for lazy loading
+                        this.unloadedJobs.add(nodePath);
+                        this.ignoreException(cnfe);
+                    } catch (RepositoryException re) {
+                        this.logger.error("Unable to load stored job from " + nodePath);
+                    }
                 }
             }
+        } catch (RepositoryException re) {
+            this.logger.error("Exception during initial loading of stored jobs.", re);
         }
     }
 
@@ -546,10 +570,18 @@
             final Node eventNode = (Node) s.getItem(eventNodePath);
             try {
                 if ( !reschedule ) {
+                    // unlock node
+                    try {
+                        eventNode.unlock();
+                    } catch (RepositoryException e) {
+                        // if unlock fails, we silently ignore this
+                        this.ignoreException(e);
+                    }
                     // remove node from repository
                     final Node parentNode = eventNode.getParent();
                     eventNode.remove();
                     parentNode.save();
+                    lockToken = null;
                 }
             } catch (RepositoryException re) {
                 // if an exception occurs, we just log
@@ -561,12 +593,14 @@
                         this.processingMap.put(jobTopic, Boolean.FALSE);
                     }
                 }
-                // unlock node
-                try {
-                    eventNode.unlock();
-                } catch (RepositoryException e) {
-                    // if unlock fails, we silently ignore this
-                    this.ignoreException(e);
+                if ( lockToken != null ) {
+                    // unlock node
+                    try {
+                        eventNode.unlock();
+                    } catch (RepositoryException e) {
+                        // if unlock fails, we silently ignore this
+                        this.ignoreException(e);
+                    }
                 }
             }
             if ( reschedule ) {
@@ -656,8 +690,13 @@
             final NodeIterator iter = q.execute().getNodes();
             while ( iter.hasNext() ) {
                 final Node eventNode = iter.nextNode();
-                final Event event = this.readEvent(eventNode);
-                jobs.add(event);
+                try {
+                    final Event event = this.readEvent(eventNode);
+                    jobs.add(event);
+                } catch (ClassNotFoundException cnfe) {
+                    // in the case of a class not found exception we just ignore the exception
+                    this.ignoreException(cnfe);
+                }
             }
         } catch (RepositoryException e) {
             // in the case of an error, we return an empty list

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=620792&r1=620791&r2=620792&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 Tue Feb 12 05:29:33 2008
@@ -23,9 +23,11 @@
 import java.util.Date;
 import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.Set;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
@@ -66,6 +68,9 @@
     /** @scr.reference */
     protected Scheduler scheduler;
 
+    /** Unloaded events. */
+    protected Set<String>unloadedEvents = new HashSet<String>();
+
     /**
      * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#startWriterSession()
      */
@@ -307,14 +312,21 @@
                     try {
                         final Node eventNode = (Node) s.getItem(event.getPath());
                         if ( !eventNode.isLocked() ) {
-                            final EventInfo info = new EventInfo();
-                            info.event = this.readEvent(eventNode);
-                            info.nodePath = event.getPath();
+                            final String nodePath = event.getPath();
                             try {
-                                this.queue.put(info);
-                            } catch (InterruptedException e) {
-                                // we ignore this exception as this should never occur
-                                this.ignoreException(e);
+                                final EventInfo info = new EventInfo();
+                                info.event = this.readEvent(eventNode);
+                                info.nodePath =nodePath;
+                                try {
+                                    this.queue.put(info);
+                                } catch (InterruptedException e) {
+                                    // we ignore this exception as this should never occur
+                                    this.ignoreException(e);
+                                }
+                            } catch (ClassNotFoundException cnfe) {
+                                // add it to the unloaded set
+                                this.unloadedEvents.add(nodePath);
+                                this.ignoreException(cnfe);
                             }
                         }
                     } catch (RepositoryException re) {
@@ -413,34 +425,43 @@
      * Load all active timed events from the repository.
      * @throws RepositoryException
      */
-    protected void loadEvents() throws RepositoryException {
-        final QueryManager qManager = this.writerSession.getWorkspace().getQueryManager();
-        final StringBuffer buffer = new StringBuffer("/jcr:root");
-        buffer.append(this.repositoryPath);
-        buffer.append("//element(*, ");
-        buffer.append(this.getEventNodeType());
-        buffer.append(")");
-        final Query q = qManager.createQuery(buffer.toString(), Query.XPATH);
-        final NodeIterator result = q.execute().getNodes();
-        while ( result.hasNext() ) {
-            final Node eventNode = result.nextNode();
-            if ( !eventNode.isLocked() ) {
-                try {
-                    final Event event = this.readEvent(eventNode);
-                    final EventInfo info = new EventInfo();
-                    info.event = event;
-                    info.nodePath = eventNode.getPath();
+    protected void loadEvents() {
+        try {
+            final QueryManager qManager = this.writerSession.getWorkspace().getQueryManager();
+            final StringBuffer buffer = new StringBuffer("/jcr:root");
+            buffer.append(this.repositoryPath);
+            buffer.append("//element(*, ");
+            buffer.append(this.getEventNodeType());
+            buffer.append(")");
+            final Query q = qManager.createQuery(buffer.toString(), Query.XPATH);
+            final NodeIterator result = q.execute().getNodes();
+            while ( result.hasNext() ) {
+                final Node eventNode = result.nextNode();
+                if ( !eventNode.isLocked() ) {
+                    final String nodePath = eventNode.getPath();
                     try {
-                        this.queue.put(info);
-                    } catch (InterruptedException e) {
-                        // we ignore this exception as this should never occur
-                        this.ignoreException(e);
+                        final Event event = this.readEvent(eventNode);
+                        final EventInfo info = new EventInfo();
+                        info.event = event;
+                        info.nodePath = nodePath;
+                        try {
+                            this.queue.put(info);
+                        } catch (InterruptedException e) {
+                            // we ignore this exception as this should never occur
+                            this.ignoreException(e);
+                        }
+                    } catch (ClassNotFoundException cnfe) {
+                        // add it to the unloaded set
+                        this.unloadedEvents.add(nodePath);
+                        this.ignoreException(cnfe);
+                    } catch (RepositoryException re) {
+                        // if reading an event fails, we ignore this
+                        this.ignoreException(re);
                     }
-                } catch (RepositoryException re) {
-                    // if reading an event fails, we ignore this
-                    this.ignoreException(re);
                 }
             }
+        } catch (RepositoryException re) {
+            this.logger.error("Exception during initial loading of stored timed events.", re);
         }
     }