Author: cziegeler
Date: Tue Aug 31 07:39:08 2010
New Revision: 991105

URL: http://svn.apache.org/viewvc?rev=991105&view=rev
Log:
SLING-1685 : Method to force remove a job

Modified:
    sling/trunk/bundles/extensions/event/pom.xml
    
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java
    
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
    
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
    
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/JobEventHandlerTest.java

Modified: sling/trunk/bundles/extensions/event/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/pom.xml?rev=991105&r1=991104&r2=991105&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/pom.xml (original)
+++ sling/trunk/bundles/extensions/event/pom.xml Tue Aug 31 07:39:08 2010
@@ -54,11 +54,6 @@
                 <extensions>true</extensions>
                 <configuration>
                     <instructions>
-                        <!-- As we're using JCR 2.0 for testing we explicitly 
have to
-                             import version 1.0 here  -->
-                        <Import-Package>
-                            javax.jcr.*;version=1.0,*
-                        </Import-Package>
                         <Export-Package>
                             org.apache.sling.event;version=2.4.0
                         </Export-Package>
@@ -95,18 +90,16 @@
     </reporting>
     <dependencies>
         <dependency>
-            <groupId>commons-io</groupId>
-            <artifactId>commons-io</artifactId>
-            <version>1.4</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.scr.annotations</artifactId>
-            <version>1.2.0</version>
+            <version>1.3.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
         </dependency>
@@ -158,16 +151,12 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-api</artifactId>
-        </dependency>
-        <dependency>
             <groupId>org.apache.jackrabbit</groupId>
             <artifactId>jackrabbit-jcr-commons</artifactId>
             <version>2.0.0</version>
             <scope>provided</scope>
         </dependency>
-        <!-- Testing -->
+      <!-- Testing -->
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
@@ -186,5 +175,11 @@
             <version>2.0.0</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>1.4</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Modified: 
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java?rev=991105&r1=991104&r2=991105&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java
 (original)
+++ 
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java
 Tue Aug 31 07:39:08 2010
@@ -124,6 +124,28 @@ public interface JobStatusProvider {
     boolean removeJob(String topic, String jobId);
 
     /**
+     * Cancel this job.
+     * This method acts like {...@link #removeJob(String)} with the exception 
that it waits
+     * for a job to finish. The job will be removed when this method returns - 
however
+     * this method blocks until the job is finished!
+     * @param jobId The unique identifer as found in the property {...@link 
#PROPERTY_EVENT_ID}.
+     * @since 2.4.0
+     */
+    void forceRemoveJob(String jobId);
+
+    /**
+     * Cancel this job.
+     * This method acts like {...@link #removeJob(String, String)} with the 
exception that it waits
+     * for a job to finish. The job will be removed when this method returns - 
however
+     * this method blocks until the job is finished!
+     * This method can be used if the topic and the provided job id is known.
+     * @param topic The job topic as put into the property {...@link 
EventUtil#PROPERTY_JOB_TOPIC}.
+     * @param jobId The unique identifer as put into the property {...@link 
EventUtil#PROPERTY_JOB_ID}.
+     * @since 2.4.0
+     */
+    void forceRemoveJob(String topic, String jobId);
+
+    /**
      * Wake up the named job queue.
      * If a job failed, the job queue waits (sleeps) for a configured time. By 
calling this
      * method, the job queue can be woken up and force an immediate 
reprocessing.

Modified: 
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java?rev=991105&r1=991104&r2=991105&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
 (original)
+++ 
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
 Tue Aug 31 07:39:08 2010
@@ -1652,6 +1652,7 @@ public class JobEventHandler
     /**
      * @see 
org.apache.sling.event.JobStatusProvider#cancelJob(java.lang.String, 
java.lang.String)
      */
+    @Deprecated
     public void cancelJob(String topic, String jobId) {
         this.removeJob(topic, jobId);
     }
@@ -1659,6 +1660,7 @@ public class JobEventHandler
     /**
      * @see 
org.apache.sling.event.JobStatusProvider#cancelJob(java.lang.String)
      */
+    @Deprecated
     public void cancelJob(String jobId) {
         this.removeJob(jobId);
     }
@@ -1719,9 +1721,37 @@ public class JobEventHandler
 
 
     /**
+     * @see 
org.apache.sling.event.JobStatusProvider#forceRemoveJob(java.lang.String, 
java.lang.String)
+     */
+    public void forceRemoveJob(final String topic, final String jobId) {
+        while ( this.backgroundSession != null && !this.removeJob(topic, 
jobId) ) {
+            // instead of using complicated syncs, waits and notifies we 
simply poll
+            try {
+                Thread.sleep(50);
+            } catch (final InterruptedException ignore) {
+                this.ignoreException(ignore);
+            }
+        }
+    }
+
+    /**
+     * @see 
org.apache.sling.event.JobStatusProvider#forceRemoveJob(java.lang.String)
+     */
+    public void forceRemoveJob(final String jobId) {
+        while ( this.backgroundSession != null && !this.removeJob(jobId) ) {
+            // instead of using complicated syncs, waits and notifies we 
simply poll
+            try {
+                Thread.sleep(50);
+            } catch (final InterruptedException ignore) {
+                this.ignoreException(ignore);
+            }
+        }
+    }
+
+    /**
      * @see 
org.apache.sling.event.JobStatusProvider#wakeUpJobQueue(java.lang.String)
      */
-    public void wakeUpJobQueue(String jobQueueName) {
+    public void wakeUpJobQueue(final String jobQueueName) {
         if ( jobQueueName != null ) {
             synchronized ( this.jobQueues ) {
                 final JobBlockingQueue queue = 
this.jobQueues.get(jobQueueName);

Modified: 
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java?rev=991105&r1=991104&r2=991105&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
 (original)
+++ 
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
 Tue Aug 31 07:39:08 2010
@@ -70,6 +70,10 @@ public abstract class AbstractRepository
         session = 
repository.loginAdministrative(repository.getDefaultWorkspace());
         assertTrue(RepositoryTestUtil.registerNodeType(session, 
DistributingEventHandler.class.getResourceAsStream("/SLING-INF/nodetypes/event.cnd")));
         assertTrue(RepositoryTestUtil.registerNodeType(session, 
DistributingEventHandler.class.getResourceAsStream("/SLING-INF/nodetypes/folder.cnd")));
+        if ( session.itemExists(REPO_PATH) ) {
+            session.getItem(REPO_PATH).remove();
+            session.save();
+        }
     }
 
     @org.junit.AfterClass public static void shutdownRepository() throws 
Exception {

Modified: 
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/JobEventHandlerTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/JobEventHandlerTest.java?rev=991105&r1=991104&r2=991105&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/JobEventHandlerTest.java
 (original)
+++ 
sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/JobEventHandlerTest.java
 Tue Aug 31 07:39:08 2010
@@ -173,6 +173,13 @@ public class JobEventHandlerTest extends
                     }
                 });
         jeh.handleEvent(getJobEvent(null, "myid", null));
+        // sleep a little to give the job handler time write the job
+        try {
+            Thread.sleep(50);
+        } catch (InterruptedException e) {
+            // ignore
+        }
+
         assertEquals(1, jeh.getAllJobs("sling/test").size());
         cb.block();
         // job is currently sleeping, therefore cancel fails
@@ -188,6 +195,44 @@ public class JobEventHandlerTest extends
     }
 
     /**
+     * Test force cancelling a job
+     * The job execution always fails
+     */
+    @org.junit.Test public void testForceCancelJob() throws Exception {
+        final JobEventHandler jeh = (JobEventHandler)this.handler;
+        final Barrier cb = new Barrier(2);
+        jeh.eventAdmin = new SimpleEventAdmin(new String[] {"sling/test"},
+                new EventHandler[] {
+                    new EventHandler() {
+                        public void handleEvent(Event event) {
+                            EventUtil.acknowledgeJob(event);
+                            cb.block();
+                            try {
+                                Thread.sleep(400);
+                            } catch (InterruptedException e) {
+                                // ignore
+                            }
+                            EventUtil.rescheduleJob(event);
+                        }
+
+                    }
+                });
+        jeh.handleEvent(getJobEvent(null, "myid", null));
+        // sleep a little to give the job handler time write the job
+        try {
+            Thread.sleep(50);
+        } catch (InterruptedException e) {
+            // ignore
+        }
+        assertEquals(1, jeh.getAllJobs("sling/test").size());
+        cb.block();
+        // job is currently sleeping, but force cancel always waits!
+        jeh.forceRemoveJob("sling/test", "myid");
+        // the job is now removed
+        assertEquals(0, jeh.getAllJobs("sling/test").size());
+    }
+
+    /**
      * Reschedule test.
      * The job is rescheduled two times before it fails.
      */


Reply via email to