Author: brett
Date: Mon Feb 25 05:26:35 2008
New Revision: 630841

URL: http://svn.apache.org/viewvc?rev=630841&view=rev
Log:
[MRM-691] ensure proper scheduling after configuration changes
Merged from: r630838

Modified:
    
maven/archiva/branches/archiva-1.0.x/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/DefaultArchivaTaskScheduler.java

Modified: 
maven/archiva/branches/archiva-1.0.x/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/DefaultArchivaTaskScheduler.java
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/archiva-1.0.x/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/DefaultArchivaTaskScheduler.java?rev=630841&r1=630840&r2=630841&view=diff
==============================================================================
--- 
maven/archiva/branches/archiva-1.0.x/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/DefaultArchivaTaskScheduler.java
 (original)
+++ 
maven/archiva/branches/archiva-1.0.x/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/DefaultArchivaTaskScheduler.java
 Mon Feb 25 05:26:35 2008
@@ -22,6 +22,8 @@
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.maven.archiva.common.ArchivaException;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
+import org.apache.maven.archiva.configuration.ConfigurationEvent;
+import org.apache.maven.archiva.configuration.ConfigurationListener;
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.maven.archiva.scheduled.tasks.ArchivaTask;
 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
@@ -31,8 +33,6 @@
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Startable;
 import 
org.codehaus.plexus.personality.plexus.lifecycle.phase.StartingException;
 import 
org.codehaus.plexus.personality.plexus.lifecycle.phase.StoppingException;
-import org.codehaus.plexus.registry.Registry;
-import org.codehaus.plexus.registry.RegistryListener;
 import org.codehaus.plexus.scheduler.CronExpressionValidator;
 import org.codehaus.plexus.scheduler.Scheduler;
 import org.codehaus.plexus.taskqueue.Task;
@@ -45,7 +45,9 @@
 import org.quartz.SchedulerException;
 
 import java.text.ParseException;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 /**
  * Default implementation of a scheduling component for archiva.
@@ -56,7 +58,7 @@
  */
 public class DefaultArchivaTaskScheduler
     extends AbstractLogEnabled
-    implements ArchivaTaskScheduler, Startable, RegistryListener
+    implements ArchivaTaskScheduler, Startable, ConfigurationListener
 {
     /**
      * @plexus.requirement
@@ -92,9 +94,13 @@
 
     public static final String CRON_HOURLY = "0 0 * * * ?";
 
+    private Set<String> jobs = new HashSet<String>();
+
     public void startup()
         throws ArchivaException
     {
+        archivaConfiguration.addListener( this );
+
         try
         {
             start();
@@ -129,7 +135,7 @@
         }
     }
 
-    private void scheduleRepositoryJobs( ManagedRepositoryConfiguration 
repoConfig )
+    private synchronized void scheduleRepositoryJobs( 
ManagedRepositoryConfiguration repoConfig )
         throws SchedulerException
     {
         if ( repoConfig.getRefreshCronExpression() == null )
@@ -164,6 +170,7 @@
             CronTrigger trigger =
                 new CronTrigger( REPOSITORY_JOB_TRIGGER + ":" + 
repoConfig.getId(), REPOSITORY_SCAN_GROUP, cronString );
 
+            jobs.add( REPOSITORY_JOB + ":" + repoConfig.getId() );
             scheduler.scheduleJob( repositoryJob, trigger );
         }
         catch ( ParseException e )
@@ -175,7 +182,7 @@
 
     }
 
-    private void scheduleDatabaseJobs()
+    private synchronized void scheduleDatabaseJobs()
         throws SchedulerException
     {
         String cronString = 
archivaConfiguration.getConfiguration().getDatabaseScanning().getCronExpression();
@@ -215,64 +222,16 @@
         try
         {
             scheduler.unscheduleJob( DATABASE_JOB, DATABASE_SCAN_GROUP );
-        }
-        catch ( SchedulerException e )
-        {
-            throw new StoppingException( "Unable to unschedule tasks", e );
-        }
-    }
-
-    public void beforeConfigurationChange( Registry registry, String 
propertyName, Object propertyValue )
-    {
-        // nothing to do
-    }
-
-    /**
-     *
-     */
-    public void afterConfigurationChange( Registry registry, String 
propertyName, Object propertyValue )
-    {
-        // cronExpression comes from the database scanning section
-        if ( "cronExpression".equals( propertyName ) )
-        {
-            getLogger().debug( "Restarting the database scheduled task after 
property change: " + propertyName );
-
-            try
-            {
-                scheduler.unscheduleJob( DATABASE_JOB, DATABASE_SCAN_GROUP );
 
-                scheduleDatabaseJobs();
-            }
-            catch ( SchedulerException e )
+            for ( String job : jobs )
             {
-                getLogger().error( "Error restarting the database scanning job 
after property change." );
+                scheduler.unscheduleJob( job, REPOSITORY_SCAN_GROUP );
             }
+            jobs.clear();
         }
-
-        // refreshCronExpression comes from the repositories section
-        // 
-        // currently we have to reschedule all repo jobs because we don't know 
where the changed one came from
-        if ( "refreshCronExpression".equals( propertyName ) )
+        catch ( SchedulerException e )
         {
-            List<ManagedRepositoryConfiguration> repositories = 
archivaConfiguration.getConfiguration()
-            .getManagedRepositories();
-
-            for ( ManagedRepositoryConfiguration repoConfig : repositories )
-            {
-                if ( repoConfig.getRefreshCronExpression() != null )
-                {
-                    try
-                    {
-                        // unschedule handles jobs that might not exist
-                        scheduler.unscheduleJob( REPOSITORY_JOB + ":" + 
repoConfig.getId(), REPOSITORY_SCAN_GROUP );
-                        scheduleRepositoryJobs( repoConfig );
-                    }
-                    catch ( SchedulerException e )
-                    {
-                        getLogger().error( "error restarting job: " + 
REPOSITORY_JOB + ":" + repoConfig.getId() );
-                    }
-                }
-            }
+            throw new StoppingException( "Unable to unschedule tasks", e );
         }
     }
 
@@ -351,5 +310,53 @@
         throws TaskQueueException
     {
         databaseUpdateQueue.put( task );
+    }
+
+    public void configurationEvent( ConfigurationEvent event )
+    {
+        if ( event.getType() == ConfigurationEvent.SAVED )
+        {
+            try
+            {
+                scheduler.unscheduleJob( DATABASE_JOB, DATABASE_SCAN_GROUP );
+
+                scheduleDatabaseJobs();
+            }
+            catch ( SchedulerException e )
+            {
+                log.error( "Error restarting the database scanning job after 
property change." );
+            }
+
+            for ( String job : jobs )
+            {
+                try
+                {
+                    scheduler.unscheduleJob( job, REPOSITORY_SCAN_GROUP );
+                }
+                catch ( SchedulerException e )
+                {
+                    log.error( "Error restarting the repository scanning job 
after property change." );
+                }
+            }
+            jobs.clear();
+
+            List<ManagedRepositoryConfiguration> repositories = 
archivaConfiguration.getConfiguration()
+            .getManagedRepositories();
+
+            for ( ManagedRepositoryConfiguration repoConfig : repositories )
+            {
+                if ( repoConfig.getRefreshCronExpression() != null )
+                {
+                    try
+                    {
+                        scheduleRepositoryJobs( repoConfig );
+                    }
+                    catch ( SchedulerException e )
+                    {
+                        log.error( "error restarting job: " + REPOSITORY_JOB + 
":" + repoConfig.getId() );
+                    }
+                }
+            }
+        }
     }
 }


Reply via email to