Wiki

Clone wiki

scm-manager / scheduler-plugin

scm-scheduler-plugin

The scheduler plugin provides an api for other plugins to execute scheduled jobs. The plugin is based on the quartz project.

Configuration

The plugin needs no further configuration.

API Usage

First you have to add the dependency to your pom.xml e.g.:

<dependency>
  <groupId>sonia.scm.plugins</groupId>
  <artifactId>scm-scheduler-plugin</artifactId>
  <version>1.0</version>
</dependency>

But note you should use at least version 1.22 of scm-plugins as parent for your plugin.

Now you can get a SchedulerFactory via injection.

For Example:

public class ReportHandler {

  private final SchedulerFactory schedulerFactory;

  @Inject
  public ReportContextListener(SchedulerFactory schedulerFactory){
    this.schedulerFactory = schedulerFactory;
  }

}

With the SchedulerFactory SchedulerFactory you can schedule Jobs. To simplify the steps to schedule a job, the scm-scheduler-plugin comes with a helper class called Schedulers.

Notes

  • Schedulers must always be executed by an administrator or in an administrative context. Jobs which are executed by a scheduler are executed with administrator privileges.
  • Each job is able to get dependencies via injection.

Further reading

Complete Example

In the following example a ReportJob is executed every minute since the start of scm-manager.

@Extension
public class ReportContextListener implements ServletContextListener {

  private final AdministrationContext adminContext;
  private final SchedulerFactory schedulerFactory;

  @Inject
  public ReportContextListener(AdministrationContext adminContext,
                               SchedulerFactory schedulerFactory){
    this.adminContext = adminContext;
    this.schedulerFactory = schedulerFactory;
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {}

  @Override
  public void contextInitialized(ServletContextEvent sce){
    Schedulers.schedule(adminContext, schedulerFactory, 
      SimpleScheduleBuilder.repeatMinutelyForever(), ReportJob.class);
  }

  private static class ReportJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
      // do something minutely
      System.out.println(new Date());
    }
  }
}

Updated