1 package org.apache.maven.archiva.scheduled;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.Configuration;
24 import org.apache.maven.archiva.repositories.ActiveManagedRepositories;
25 import org.apache.maven.archiva.scheduler.task.DataRefreshTask;
26 import org.apache.maven.archiva.scheduler.task.RepositoryTask;
27 import org.codehaus.plexus.logging.AbstractLogEnabled;
28 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Startable;
29 import org.codehaus.plexus.personality.plexus.lifecycle.phase.StartingException;
30 import org.codehaus.plexus.personality.plexus.lifecycle.phase.StoppingException;
31 import org.codehaus.plexus.registry.Registry;
32 import org.codehaus.plexus.registry.RegistryListener;
33 import org.codehaus.plexus.scheduler.Scheduler;
34 import org.codehaus.plexus.taskqueue.TaskQueue;
35 import org.codehaus.plexus.taskqueue.TaskQueueException;
36 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
37 import org.quartz.CronTrigger;
38 import org.quartz.JobDataMap;
39 import org.quartz.JobDetail;
40 import org.quartz.SchedulerException;
42 import java.text.ParseException;
45 * Default implementation of a scheduling component for the application.
47 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
48 * @plexus.component role="org.apache.maven.archiva.scheduler.RepositoryTaskScheduler"
50 public class DefaultRepositoryTaskScheduler
51 extends AbstractLogEnabled
52 implements RepositoryTaskScheduler, Startable, RegistryListener
57 private Scheduler scheduler;
60 * @plexus.requirement role-hint="data-refresh"
62 private TaskQueue datarefreshQueue;
67 private ArchivaConfiguration archivaConfiguration;
72 private ActiveManagedRepositories activeRepositories;
74 private static final String DISCOVERER_GROUP = "DISCOVERER";
76 private static final String DATA_REFRESH_JOB = "dataRefreshTask";
79 throws StartingException
81 Configuration configuration = archivaConfiguration.getConfiguration();
82 archivaConfiguration.addChangeListener( this );
86 scheduleJobs( configuration.getDataRefreshCronExpression() );
88 catch ( ParseException e )
90 throw new StartingException( "Invalid configuration: " + configuration.getDataRefreshCronExpression(), e );
92 catch ( SchedulerException e )
94 throw new StartingException( "Unable to start scheduler: " + e.getMessage(), e );
98 private void scheduleJobs( String indexerCronExpression )
99 throws ParseException, SchedulerException
101 JobDetail jobDetail = createJobDetail( DATA_REFRESH_JOB );
103 getLogger().info( "Scheduling data-refresh: " + indexerCronExpression );
104 CronTrigger trigger = new CronTrigger( DATA_REFRESH_JOB + "Trigger", DISCOVERER_GROUP, indexerCronExpression );
105 scheduler.scheduleJob( jobDetail, trigger );
111 catch ( org.codehaus.plexus.taskqueue.execution.TaskExecutionException e )
113 getLogger().error( "Error executing task first time, continuing anyway: " + e.getMessage(), e );
117 private JobDetail createJobDetail( String jobName )
119 JobDetail jobDetail = new JobDetail( jobName, DISCOVERER_GROUP, RepositoryTaskJob.class );
121 JobDataMap dataMap = new JobDataMap();
122 dataMap.put( RepositoryTaskJob.TASK_QUEUE, datarefreshQueue );
123 dataMap.put( RepositoryTaskJob.TASK_QUEUE_POLICY, RepositoryTask.QUEUE_POLICY_SKIP );
124 jobDetail.setJobDataMap( dataMap );
130 throws StoppingException
134 scheduler.unscheduleJob( DATA_REFRESH_JOB, DISCOVERER_GROUP );
136 catch ( SchedulerException e )
138 throw new StoppingException( "Unable to unschedule tasks", e );
143 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
148 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
150 if ( "dataRefreshCronExpression".equals( propertyName ) )
152 getLogger().debug( "Restarting task scheduler with new configuration after property change: " +
153 propertyName + " to " + propertyValue );
158 catch ( StoppingException e )
160 getLogger().warn( "Error stopping task scheduler: " + e.getMessage(), e );
165 Configuration configuration = archivaConfiguration.getConfiguration();
166 scheduleJobs( configuration.getDataRefreshCronExpression() );
168 catch ( ParseException e )
171 "Error restarting task scheduler after configuration change, due to configuration error: " +
174 catch ( SchedulerException e )
176 getLogger().error( "Error restarting task scheduler after configuration change: " + e.getMessage(), e );
182 "Not restarting task scheduler with new configuration after property change: " + propertyName );
186 public void runDataRefresh()
187 throws TaskExecutionException
189 DataRefreshTask task = new DataRefreshTask();
190 task.setJobName( "DATA_REFRESH_INIT" );
193 datarefreshQueue.put( task );
195 catch ( TaskQueueException e )
197 throw new TaskExecutionException( e.getMessage(), e );
201 public void queueNowIfNeeded()
202 throws TaskExecutionException
204 if ( activeRepositories.needsDataRefresh() )