Search in sources :

Example 1 with JobInfo

use of org.exoplatform.services.scheduler.JobInfo 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)

Example 2 with JobInfo

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

the class JobSchedulerServiceImpl method addCronJob.

public void addCronJob(JobInfo jinfo, String exp, JobDataMap jdatamap) throws Exception {
    JobInfo jobinfo = getJobInfo(jinfo);
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).forJob(jobinfo.getJobName(), jobinfo.getGroupName()).withSchedule(CronScheduleBuilder.cronSchedule(exp)).build();
    @SuppressWarnings("unchecked") JobBuilder jb = JobBuilder.newJob(jobinfo.getJob()).withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withDescription(jinfo.getDescription());
    JobDetail job = jdatamap == null ? jb.build() : jb.usingJobData(jdatamap).build();
    scheduleJob(job, trigger);
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) JobInfo(org.exoplatform.services.scheduler.JobInfo) JobBuilder(org.quartz.JobBuilder)

Example 3 with JobInfo

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

the class JobSchedulerServiceImpl method addJob.

public void addJob(JobInfo jinfo, Date date) throws Exception {
    JobInfo jobinfo = getJobInfo(jinfo);
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).startAt(date).build();
    @SuppressWarnings("unchecked") JobDetail job = JobBuilder.newJob(jobinfo.getJob()).withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withDescription(jinfo.getDescription()).build();
    scheduleJob(job, trigger);
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) JobInfo(org.exoplatform.services.scheduler.JobInfo)

Example 4 with JobInfo

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

the class JobSchedulerServiceImpl method getJobInfo.

private JobInfo getJobInfo(JobInfo jinfo) throws Exception {
    String gname = getGroupName(jinfo.getGroupName());
    JobInfo jobInfo = new JobInfo(jinfo.getJobName(), gname, jinfo.getJob());
    jobInfo.setDescription(jinfo.getDescription());
    return jobInfo;
}
Also used : JobInfo(org.exoplatform.services.scheduler.JobInfo)

Example 5 with JobInfo

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

the class JobSchedulerServiceImpl method addPeriodJob.

public void addPeriodJob(JobInfo jinfo, int repeatCount, long period) throws Exception {
    int repeat;
    if (repeatCount < 0)
        repeat = SimpleTrigger.REPEAT_INDEFINITELY;
    else if (repeatCount == 0)
        repeat = SimpleTrigger.REPEAT_INDEFINITELY;
    else
        repeat = repeatCount - 1;
    JobInfo jobinfo = getJobInfo(jinfo);
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(repeat).withIntervalInMilliseconds(period)).build();
    @SuppressWarnings("unchecked") JobDetail job = JobBuilder.newJob(jobinfo.getJob()).withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withDescription(jinfo.getDescription()).build();
    scheduleJob(job, trigger);
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) JobInfo(org.exoplatform.services.scheduler.JobInfo)

Aggregations

JobInfo (org.exoplatform.services.scheduler.JobInfo)11 JobDetail (org.quartz.JobDetail)8 SimpleTrigger (org.quartz.SimpleTrigger)6 Trigger (org.quartz.Trigger)6 Date (java.util.Date)5 JobBuilder (org.quartz.JobBuilder)2 PeriodInfo (org.exoplatform.services.scheduler.PeriodInfo)1 JobListener (org.quartz.JobListener)1 TriggerListener (org.quartz.TriggerListener)1