]> source.dussan.org Git - archiva.git/commitdiff
merge -c 946618 from 1.3.x branch (MRM-1066)
authorMaria Odea B. Ching <oching@apache.org>
Thu, 20 May 2010 13:39:44 +0000 (13:39 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Thu, 20 May 2010 13:39:44 +0000 (13:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@946631 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/startup/ArchivaStartup.java

index 1f86c4e917656f64c81ef99da56389d3c5fb3c0c..b9cebd807dd69d92ed771a062a4e5bcf0a0d79e2 100644 (file)
@@ -19,19 +19,27 @@ package org.apache.maven.archiva.web.startup;
  * under the License.
  */
 
+import java.lang.reflect.Field;
+
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 
 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
 import org.apache.maven.archiva.common.ArchivaException;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.StoppingException;
+import org.codehaus.plexus.scheduler.DefaultScheduler;
 import org.codehaus.plexus.spring.PlexusToSpringUtils;
+import org.codehaus.plexus.spring.PlexusWebApplicationContext;
+import org.codehaus.plexus.taskqueue.Task;
 import org.codehaus.plexus.taskqueue.execution.TaskQueueExecutor;
-import org.springframework.context.ApplicationContext;
+import org.codehaus.plexus.taskqueue.execution.ThreadedTaskQueueExecutor;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.web.context.WebApplicationContext;
 import org.springframework.web.context.support.WebApplicationContextUtils;
 
+import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
+
 /**
  * ArchivaStartup - the startup of all archiva features in a deterministic order.
  *
@@ -40,6 +48,14 @@ import org.springframework.web.context.support.WebApplicationContextUtils;
 public class ArchivaStartup
     implements ServletContextListener
 {
+    private ThreadedTaskQueueExecutor tqeDbScanning;
+
+    private ThreadedTaskQueueExecutor tqeRepoScanning;
+
+    private ThreadedTaskQueueExecutor tqeIndexing;
+
+    private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
+    
     public void contextInitialized( ServletContextEvent contextEvent )
     {
         WebApplicationContext wac =
@@ -47,10 +63,19 @@ public class ArchivaStartup
 
         SecuritySynchronization securitySync =
             (SecuritySynchronization) wac.getBean( PlexusToSpringUtils.buildSpringId( SecuritySynchronization.class ) );
-        RepositoryArchivaTaskScheduler repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) wac.getBean(
-            PlexusToSpringUtils.buildSpringId( ArchivaTaskScheduler.class, "repository" ) );
-        wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class, "repository-scanning" ) );
-        wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class, "indexing" ) );
+
+        repositoryTaskScheduler =
+            (RepositoryArchivaTaskScheduler) wac.getBean( PlexusToSpringUtils.buildSpringId(
+                                                                                             ArchivaTaskScheduler.class,
+                                                                                             "repository" ) );
+
+        tqeRepoScanning =
+            (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
+                                                                                        "repository-scanning" ) );
+
+        tqeIndexing =
+            (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
+                                                                                        "indexing" ) );
 
         try
         {
@@ -66,11 +91,91 @@ public class ArchivaStartup
 
     public void contextDestroyed( ServletContextEvent contextEvent )
     {
-        ApplicationContext applicationContext =
+        WebApplicationContext applicationContext =
             WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
         if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
         {
             ( (ClassPathXmlApplicationContext) applicationContext ).close();
         }
+
+        if ( applicationContext != null && applicationContext instanceof PlexusWebApplicationContext )
+        {
+            // stop task queue executors
+            stopTaskQueueExecutor( tqeDbScanning );
+            stopTaskQueueExecutor( tqeRepoScanning );
+            stopTaskQueueExecutor( tqeIndexing );
+
+            // stop the DefaultArchivaTaskScheduler and its scheduler
+            if ( repositoryTaskScheduler != null )
+            {
+                try
+                {
+                    repositoryTaskScheduler.stop();
+                }
+                catch ( StoppingException e )
+                {
+                    e.printStackTrace();
+                }
+            }
+
+            try
+            {
+                // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
+                Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
+                schedulerField.setAccessible( true );
+
+                DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
+                scheduler.stop();
+            }
+            catch ( Exception e )
+            {   
+                e.printStackTrace();
+            }
+
+            // close the application context
+            ( (PlexusWebApplicationContext) applicationContext ).close();
+        }
+    }
+
+    private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor )
+    {
+        if ( taskQueueExecutor != null )
+        {
+            Task currentTask = taskQueueExecutor.getCurrentTask();
+            if ( currentTask != null )
+            {
+                taskQueueExecutor.cancelTask( currentTask );
+            }
+
+            try
+            {
+                taskQueueExecutor.stop();
+                ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor );
+                if ( service != null )
+                {
+                    service.shutdown();
+                }
+            }
+            catch ( Exception e )
+            {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe )
+    {
+        ExecutorService service = null;
+        try
+        {
+            Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
+            executorServiceField.setAccessible( true );
+            service = (ExecutorService) executorServiceField.get( ttqe );
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+        }
+        return service;
     }
 }