Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/Concepts/Scheduler"

(New page: {{ScoutPage|cat=Concepts}} Scout provides a scheduler to run jobs. = Facts = *The instance for the Job is created exactly once. Every run of a job is executed by that instance. *The time...)
 
m
Line 2: Line 2:
 
Scout provides a scheduler to run jobs.
 
Scout provides a scheduler to run jobs.
  
= Facts =
+
== Facts ==
 
*The instance for the Job is created exactly once. Every run of a job is executed by that instance.  
 
*The instance for the Job is created exactly once. Every run of a job is executed by that instance.  
 
*The timer (when to start) is hardcoded (see example).
 
*The timer (when to start) is hardcoded (see example).
  
= Usage  =
+
== Usage  ==
  
 
#Derive a class from <code>AbstractSchedulerJob</code>  
 
#Derive a class from <code>AbstractSchedulerJob</code>  
Line 28: Line 28:
 
</source>
 
</source>
  
= Example =
+
== Example ==
 
<source lang="java">
 
<source lang="java">
 
public class MyJob extends AbstractSchedulerJob {
 
public class MyJob extends AbstractSchedulerJob {

Revision as of 13:10, 1 November 2011

The Scout documentation has been moved to https://eclipsescout.github.io/. Scout provides a scheduler to run jobs.

Facts

  • The instance for the Job is created exactly once. Every run of a job is executed by that instance.
  • The timer (when to start) is hardcoded (see example).

Usage

  1. Derive a class from AbstractSchedulerJob
  2. implement a constructor calling super(groupId, jobId);
  3. implement / override execAcceptTick
  4. implement / override run
  5. In the ServerApplication you need something like
public class ServerApplication implements IApplication{
  public Object start(IApplicationContext context) throws Exception {
    //start the scheduler
    Scheduler scheduler=new Scheduler(Activator.getDefault().getBackendSubject(),ServerSession.class);
    scheduler.addJob(new LoadJobs());
//    scheduler.addJob(new FetchMailSchedulerJob());
    scheduler.addJob(new LdapSchedulerJob());
    scheduler.addJob(new UpdatePLAOrdersJob());
    scheduler.start();
    Activator.getDefault().setScheduler(scheduler);
    ...

Example

public class MyJob extends AbstractSchedulerJob {
  private static IScoutLogger s_logger =ScoutLogManager.getLogger(MyJob.class);
  private final static String groupId = "MyGroup";
  private final static String jobId = "MyJob";
  /**
   * &lt;p&gt;&lt;code&gt;true&lt;/code&gt; the job is currently running, &lt;code&gt;false&lt;/code&gt; else&lt;/p&gt;
   * &lt;p&gt;Access needs to be guarded / synchronized by &lt;code&gt;this&lt;/code&gt;, because it is possible, that the same reference to the job
   * is called twice.&lt;/p&gt;.
   */
  private boolean m_running;
 
  public MyJob() {
    super(groupId, jobId);
  }
 
  @Override
  protected boolean execAcceptTick(TickSignal signal, int second, int minute, int hour, int day, int week, int month,
      int year, int dayOfWeek, int dayOfMonthReverse, int dayOfYear, int secondOfDay) {
    return (second==0 &amp;&amp; minute%10==0); /* start every 10 minutes */
  }
 
  @Override
  public void run(IScheduler scheduler, TickSignal signal) throws ProcessingException {
    synchronized (this) {
      if (m_running) { /* prevent the job from being started twice */
        s_logger.warn("The Job " + getGroupId() + "." + getJobId() + " is already running, but should be started. Job was not started.");
        return;
      }
      m_running = true;
    }
    try {
      s_logger.info("Started scheduled job: " + getGroupId() + "." + getJobId() + ", process all PLA Orders.");
      IXYZService service = SERVICES.getService(IXYZService.class);
      try {
        service.doStuff();
      } catch (Exception e) {
        s_logger.error("Error in Job " + getGroupId() + "." + getJobId(), e);
      }
      s_logger.info("Finished scheduled job: " + getGroupId() + "." + getJobId() + ", process all PLA Orders");
    } finally {
      synchronized (this) {
        m_running = false;
      }
    }
  }
}

Back to the top