Search in sources :

Example 1 with PeriodInfo

use of org.exoplatform.services.scheduler.PeriodInfo in project kernel by exoplatform.

the class TestSchedulerService method testSeviceWithGlobalListener.

public void testSeviceWithGlobalListener() throws Exception {
    assertTrue("JobScheduler is not deployed correctly", service_ != null);
    Thread.sleep(1000);
    Date startTime = new Date(System.currentTimeMillis() + 1000);
    Date endTime = null;
    JobInfo jinfo;
    // Test addJob(JobInfo, Date startTime) and GlobalTriggerListener()
    resetTestEnvironment();
    jinfo = new JobInfo("TestAddJob", null, /* default group */
    AJob.class);
    AJob.expectExecuteTime_ = startTime;
    service_.addGlobalTriggerListener(new GlobalTriggerListener());
    service_.addJob(jinfo, startTime);
    Thread.sleep(1100);
    assertEquals("task has been run once", 1, AJob.counter_);
    // assertEquals("GlobalTriggerListener detect one task", 2,
    // GlobalTriggerListener.countTriggerFired_) ;
    assertTrue("The task is triggered  at the correct time  within 500ms margin", AJob.errorCounter_ == 0);
    /*
       * Test addJobPeriod(JobInfo jinfo,JobPeriodInfor pinfor) Job will fire
       * after 3 second, repeat forever, 2 second pause between repeat fire
       */
    resetTestEnvironment();
    jinfo = new JobInfo("TestJobPeriod", null, /* default group */
    AJob.class);
    startTime = new Date(System.currentTimeMillis() + 1000);
    PeriodInfo jpinfo = new PeriodInfo(startTime, null, /* endTime */
    2, /* repeatCount */
    1000);
    service_.addPeriodJob(jinfo, jpinfo);
    Thread.sleep(2100);
    assertEquals("task has been run exactly two times", 2, AJob.repeatCounter_);
    /*
       * Test addJobPeriod(JobInfo jinfo,JobPeriodInfor pinfor) Job will fire
       * after 1 second, stop after 2 second, 1 second pause between repeat fire
       */
    resetTestEnvironment();
    long currentTime = System.currentTimeMillis();
    jinfo = new JobInfo("TestJobPeriod", null, /* default group */
    AJob.class);
    startTime = new Date(currentTime + 1000);
    endTime = new Date(currentTime + 2100);
    jpinfo = new PeriodInfo(startTime, endTime, 0, /* repeatCount */
    1000);
    service_.addPeriodJob(jinfo, jpinfo);
    Thread.sleep(2500);
    assertEquals("task has been run exactly two times", 2, AJob.repeatCounter_);
    /*
       * test test addPeriodJob(JobInfo jinfo,JobperiodInfo pinfo) Job will run
       * imediately, and exactly two times, 1 second pause between repeat run
       */
    resetTestEnvironment();
    jinfo = new JobInfo("ImediatelyPeriodJob", null, /* default group */
    AJob.class);
    jpinfo = new PeriodInfo(null, /* startTime */
    null, /* endTime */
    2, /* repeatCount */
    500);
    service_.addPeriodJob(jinfo, jpinfo);
    Thread.sleep(1100);
    assertEquals("task has been run exactly two times", 2, AJob.repeatCounter_);
    service_.removeJob(jinfo);
    /*
       * test addCronJob(JobInfor jinfo, String exp); Job will fire at 10: am
       * every day "0 15 10 ? * *"
       */
    resetTestEnvironment();
    String exp = "0 25 11 ? * *";
    jinfo = new JobInfo("TestCronJob", "cronGroup", /* default group */
    AJob.class);
    service_.addCronJob(jinfo, exp);
    assertEquals("task has been run exactly one times at " + exp, 0, AJob.repeatCounter_);
    service_.removeJob(jinfo);
    /*
       * test test addPeriodJob(JobInfo jinfo,JobperiodInfo pinfo) Job will run
       * imediately, and forever, 1 second pause between repeat run
       */
    resetTestEnvironment();
    jinfo = new JobInfo("ForeverPeriodJob", null, /* default group */
    AJob.class);
    jpinfo = new PeriodInfo(null, /* startTime */
    null, /* endTime */
    0, /* repeatCount */
    500);
    service_.addPeriodJob(jinfo, jpinfo);
    Thread.sleep(1100);
    assertEquals("task has been run forever: ", 3, AJob.repeatCounter_);
    boolean b = service_.removeJob(jinfo);
    assertTrue("expect Job is removed", b);
    Thread.sleep(1000);
    assertEquals("task has been run forever: ", 3, AJob.repeatCounter_);
    b = service_.removeGlobaTriggerListener("GlobalTriggerListener");
    assertTrue("expect Global Trigger Listener is removed", b);
}
Also used : JobInfo(org.exoplatform.services.scheduler.JobInfo) PeriodInfo(org.exoplatform.services.scheduler.PeriodInfo) Date(java.util.Date)

Aggregations

Date (java.util.Date)1 JobInfo (org.exoplatform.services.scheduler.JobInfo)1 PeriodInfo (org.exoplatform.services.scheduler.PeriodInfo)1