org.quartz.impl.triggers.CronTriggerImpl Java Examples

The following examples show how to use org.quartz.impl.triggers.CronTriggerImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScheduledInfo.java    From GOAi with GNU Affero General Public License v3.0 7 votes vote down vote up
/**
 * 设置触发器
 */
private void initCronTrigger() {
    if (!useful(this.cron)) {
        return;
    }
    try {
        CronTriggerImpl cronTrigger = new CronTriggerImpl();
        cronTrigger.setCronExpression(this.cron);
        this.tip = "cron: " + this.cron;
        this.trigger = cronTrigger;
        this.fixedRate = -1;
        this.delay = -1;
    } catch (ParseException e) {
        log.error("can not format {} to cron", this.cron);
    }
}
 
Example #2
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void storeTrigger() throws Exception {
    CronTriggerImpl trigger = getCronTrigger();
    trigger.getJobDataMap().put("foo", "bar");

    jobStore.storeTrigger(trigger, false);

    final String triggerHashKey = schema.triggerHashKey(trigger.getKey());
    Map<String, String> triggerMap = jedis.hgetAll(triggerHashKey);
    assertThat(triggerMap, hasKey("description"));
    assertEquals(trigger.getDescription(), triggerMap.get("description"));
    assertThat(triggerMap, hasKey("trigger_class"));
    assertEquals(trigger.getClass().getName(), triggerMap.get("trigger_class"));

    assertTrue("The trigger hash key is not a member of the triggers set.", jedis.sismember(schema.triggersSet(), triggerHashKey));
    assertTrue("The trigger group set key is not a member of the trigger group set.", jedis.sismember(schema.triggerGroupsSet(), schema.triggerGroupSetKey(trigger.getKey())));
    assertTrue(jedis.sismember(schema.triggerGroupSetKey(trigger.getKey()), triggerHashKey));
    assertTrue(jedis.sismember(schema.jobTriggersSetKey(trigger.getJobKey()), triggerHashKey));
    String triggerDataMapHashKey = schema.triggerDataMapHashKey(trigger.getKey());
    MatcherAssert.assertThat(jedis.exists(triggerDataMapHashKey), equalTo(true));
    MatcherAssert.assertThat(jedis.hget(triggerDataMapHashKey, "foo"), equalTo("bar"));
}
 
Example #3
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void replaceTriggerSingleTriggerNonDurableJob() throws Exception {
    // store trigger and job
    JobDetail job = getJobDetail();
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "group1", job.getKey());
    storeJobAndTriggers(job, trigger1);

    CronTriggerImpl newTrigger = getCronTrigger("newTrigger", "group1", job.getKey());

    assertTrue(jobStore.replaceTrigger(trigger1.getKey(), newTrigger));

    // ensure that the proper trigger was replaced
    assertThat(jobStore.retrieveTrigger(trigger1.getKey()), nullValue());

    List<OperableTrigger> jobTriggers = jobStore.getTriggersForJob(job.getKey());

    assertThat(jobTriggers, hasSize(1));

    // ensure that the job still exists
    assertThat(jobStore.retrieveJob(job.getKey()), not(nullValue()));
}
 
Example #4
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void resumeTriggersStartsWith() throws Exception {
    JobDetail job = getJobDetail();
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "mygroup1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "group1", job.getKey());
    CronTriggerImpl trigger3 = getCronTrigger("trigger3", "group2", job.getKey());
    CronTriggerImpl trigger4 = getCronTrigger("trigger4", "group3", job.getKey());
    storeJobAndTriggers(job, trigger1, trigger2, trigger3, trigger4);

    Collection<String> pausedGroups = jobStore.pauseTriggers(GroupMatcher.triggerGroupStartsWith("my"));

    assertThat(pausedGroups, hasSize(1));
    assertThat(pausedGroups, containsInAnyOrder("mygroup1"));

    // ensure that the triggers were actually paused
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger1.getKey()));

    // resume triggers
    Collection<String> resumedGroups = jobStore.resumeTriggers(GroupMatcher.triggerGroupStartsWith("my"));

    assertThat(resumedGroups, hasSize(1));
    assertThat(resumedGroups, containsInAnyOrder("mygroup1"));

    // ensure that the triggers were actually resumed
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger1.getKey()));
}
 
Example #5
Source File: StoreJobTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void removeJob() throws Exception {
    // attempt to remove a non-existent job
    assertFalse(jobStore.removeJob(JobKey.jobKey("foo", "bar")));

    // create and store a job with multiple triggers
    JobDetail job = getJobDetail("job1", "jobGroup1");
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "triggerGroup1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "triggerGroup1", job.getKey());
    Set<Trigger> triggersSet = new HashSet<>();
    triggersSet.add(trigger1);
    triggersSet.add(trigger2);
    Map<JobDetail, Set<? extends Trigger>> jobsAndTriggers = new HashMap<>();
    jobsAndTriggers.put(job, triggersSet);
    jobStore.storeJobsAndTriggers(jobsAndTriggers, false);

    assertTrue(jobStore.removeJob(job.getKey()));

    // ensure that the job and all of its triggers were removed
    assertThat(jobStore.retrieveJob(job.getKey()), nullValue());
    assertThat(jobStore.retrieveTrigger(trigger1.getKey()), nullValue());
    assertThat(jobStore.retrieveTrigger(trigger2.getKey()), nullValue());
    assertThat(jedis.get(schema.triggerHashKey(trigger1.getKey())), nullValue());
}
 
Example #6
Source File: StoreJobTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void pauseJob() throws Exception {
    // create and store a job with multiple triggers
    JobDetail job = getJobDetail("job1", "jobGroup1");
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "triggerGroup1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "triggerGroup1", job.getKey());
    Set<Trigger> triggersSet = new HashSet<>();
    triggersSet.add(trigger1);
    triggersSet.add(trigger2);
    Map<JobDetail, Set<? extends Trigger>> jobsAndTriggers = new HashMap<>();
    jobsAndTriggers.put(job, triggersSet);
    jobStore.storeJobsAndTriggers(jobsAndTriggers, false);

    // pause the job
    jobStore.pauseJob(job.getKey());

    // ensure that the job's triggers were paused
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger2.getKey()));
}
 
Example #7
Source File: StoreJobTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void pauseJobsStartsWith() throws Exception {
    JobDetail job1 = getJobDetail("job1", "jobGroup1");
    storeJobAndTriggers(job1, getCronTrigger("trigger1", "triggerGroup1", job1.getKey()), getCronTrigger("trigger2", "triggerGroup1", job1.getKey()));
    JobDetail job2 = getJobDetail("job2", "yobGroup1");
    CronTriggerImpl trigger3 = getCronTrigger("trigger3", "triggerGroup3", job2.getKey());
    CronTriggerImpl trigger4 = getCronTrigger("trigger4", "triggerGroup4", job2.getKey());
    storeJobAndTriggers(job2, trigger3, trigger4);

    // pause jobs with groups beginning with "yob"
    Collection<String> pausedJobs = jobStore.pauseJobs(GroupMatcher.jobGroupStartsWith("yob"));
    assertThat(pausedJobs, hasSize(1));
    assertThat(pausedJobs, containsInAnyOrder("yobGroup1"));

    // ensure that the job was added to the paused jobs set
    assertTrue(jedis.sismember(schema.pausedJobGroupsSet(), schema.jobGroupSetKey(job2.getKey())));

    // ensure that the job's triggers have been paused
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger3.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger4.getKey()));
}
 
Example #8
Source File: StoreJobTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void pauseJobsEndsWith() throws Exception {
    JobDetail job1 = getJobDetail("job1", "jobGroup1");
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "triggerGroup1", job1.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "triggerGroup1", job1.getKey());
    storeJobAndTriggers(job1, trigger1, trigger2);
    JobDetail job2 = getJobDetail("job2", "yobGroup2");
    CronTriggerImpl trigger3 = getCronTrigger("trigger3", "triggerGroup3", job2.getKey());
    CronTriggerImpl trigger4 = getCronTrigger("trigger4", "triggerGroup4", job2.getKey());
    storeJobAndTriggers(job2, trigger3, trigger4);

    // pause job groups ending with "1"
    Collection<String> pausedJobs = jobStore.pauseJobs(GroupMatcher.jobGroupEndsWith("1"));
    assertThat(pausedJobs, hasSize(1));
    assertThat(pausedJobs, containsInAnyOrder("jobGroup1"));

    // ensure that the job was added to the paused jobs set
    assertTrue(jedis.sismember(schema.pausedJobGroupsSet(), schema.jobGroupSetKey(job1.getKey())));

    // ensure that the job's triggers have been paused
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger2.getKey()));
}
 
Example #9
Source File: StoreJobTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void resumeJob() throws Exception {
    // create and store a job with multiple triggers
    JobDetail job = getJobDetail("job1", "jobGroup1");
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "triggerGroup1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "triggerGroup1", job.getKey());
    storeJobAndTriggers(job, trigger1, trigger2);

    // pause the job
    jobStore.pauseJob(job.getKey());

    // ensure that the job's triggers have been paused
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger2.getKey()));

    // resume the job
    jobStore.resumeJob(job.getKey());

    // ensure that the triggers have been resumed
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger2.getKey()));
}
 
Example #10
Source File: JobController.java    From TAC with MIT License 6 votes vote down vote up
public void jobReschedule(String jobName, String jobGroupName, String cronExpression) throws Exception
{				
	try {
		TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
		// 表达式调度构建器
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

		// 按新的cronExpression表达式重新构建trigger
		trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
           ((CronTriggerImpl)trigger).setStartTime(new Date());
		// 按新的trigger重新设置job执行
		scheduler.rescheduleJob(triggerKey, trigger);
	} catch (SchedulerException e) {
		System.out.println("更新定时任务失败"+e);
		throw new Exception("更新定时任务失败");
	}
}
 
Example #11
Source File: TriggeredJobCompleteTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void triggeredJobCompleteNonConcurrent() throws JobPersistenceException {
    JobDetail job = JobBuilder.newJob(TestJobNonConcurrent.class)
            .withIdentity("testJobNonConcurrent1", "jobGroupNonConcurrent1")
            .usingJobData("timeout", 42)
            .withDescription("I am describing a job!")
            .build();
    CronTriggerImpl trigger1 = getCronTrigger("triggerNonConcurrent1", "triggerNonConcurrentGroup1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("triggerNonConcurrent2", "triggerNonConcurrentGroup1", job.getKey());
    storeJobAndTriggers(job, trigger1, trigger2);

    jobStore.triggeredJobComplete(trigger1, job, Trigger.CompletedExecutionInstruction.SET_TRIGGER_COMPLETE);

    assertEquals(Trigger.TriggerState.COMPLETE, jobStore.getTriggerState(trigger1.getKey()));

    final String jobHashKey = schema.jobHashKey(job.getKey());
    assertFalse(jedis.sismember(schema.blockedJobsSet(), jobHashKey));
}
 
Example #12
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void resumeTrigger() throws Exception {
    // create and store a job and trigger
    JobDetail job = getJobDetail();
    jobStore.storeJob(job, false);
    CronTriggerImpl trigger = getCronTrigger("trigger1", "group1", job.getKey());
    trigger.computeFirstFireTime(new WeeklyCalendar());
    jobStore.storeTrigger(trigger, false);

    // pause the trigger
    jobStore.pauseTrigger(trigger.getKey());
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger.getKey()));

    // resume the trigger
    jobStore.resumeTrigger(trigger.getKey());
    // the trigger state should now be NORMAL
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger.getKey()));

    // attempt to resume the trigger, again
    jobStore.resumeTrigger(trigger.getKey());
    // the trigger state should not have changed
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger.getKey()));
}
 
Example #13
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void pauseTriggersEndsWith() throws Exception {
    JobDetail job = getJobDetail();
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "group1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger1", "group2", job.getKey());
    CronTriggerImpl trigger3 = getCronTrigger("trigger1", "foogroup1", job.getKey());
    storeJobAndTriggers(job, trigger1, trigger2, trigger3);

    Collection<String> pausedGroups = jobStore.pauseTriggers(GroupMatcher.triggerGroupEndsWith("oup1"));

    assertThat(pausedGroups, hasSize(2));
    assertThat(pausedGroups, containsInAnyOrder("group1", "foogroup1"));

    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger2.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger3.getKey()));
}
 
Example #14
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void pauseTriggersStartsWith() throws Exception {
    JobDetail job = getJobDetail();
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "group1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger1", "group2", job.getKey());
    CronTriggerImpl trigger3 = getCronTrigger("trigger1", "foogroup1", job.getKey());
    storeJobAndTriggers(job, trigger1, trigger2, trigger3);

    Collection<String> pausedTriggerGroups = jobStore.pauseTriggers(GroupMatcher.triggerGroupStartsWith("group"));

    assertThat(pausedTriggerGroups, hasSize(2));
    assertThat(pausedTriggerGroups, containsInAnyOrder("group1", "group2"));

    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger2.getKey()));
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger3.getKey()));
}
 
Example #15
Source File: CleanHistorySettingsPanel.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
protected void afterChange(Form<?> form, AjaxRequestTarget target) {
	Settings settings = (Settings) form.getModelObject();
	if (!oldCronExpression.equals(settings.getCleanHistory().getCronExpression())) {
		// reschedule clean history
		StdScheduler scheduler = (StdScheduler) NextServerApplication.get().getSpringBean("scheduler");
		CronTriggerImpl cronTrigger = (CronTriggerImpl) NextServerApplication.get()
				.getSpringBean("cleanHistoryTrigger");
		try {
			cronTrigger.setCronExpression(settings.getCleanHistory().getCronExpression());
			scheduler.rescheduleJob(cronTrigger.getKey(), cronTrigger);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage(), e);
		}
	}
}
 
Example #16
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void getTriggerState() throws Exception {
    SchedulerSignaler signaler = mock(SchedulerSignaler.class);
    AbstractRedisStorage storageDriver = new RedisStorage(new RedisJobStoreSchema(), new ObjectMapper(), signaler, "scheduler1", 2000);

    // attempt to retrieve the state of a non-existent trigger
    Trigger.TriggerState state = jobStore.getTriggerState(new TriggerKey("foobar"));
    assertEquals(Trigger.TriggerState.NONE, state);

    // store a trigger
    JobDetail job = getJobDetail();
    CronTriggerImpl cronTrigger = getCronTrigger("trigger1", "group1", job.getKey());
    jobStore.storeTrigger(cronTrigger, false);

    // the newly-stored trigger's state should be NONE
    state = jobStore.getTriggerState(cronTrigger.getKey());
    assertEquals(Trigger.TriggerState.NORMAL, state);

    // set the trigger's state
    storageDriver.setTriggerState(RedisTriggerState.WAITING, 500, schema.triggerHashKey(cronTrigger.getKey()), jedis);

    // the trigger's state should now be NORMAL
    state = jobStore.getTriggerState(cronTrigger.getKey());
    assertEquals(Trigger.TriggerState.NORMAL, state);
}
 
Example #17
Source File: QuartzLTSProxyAgent.java    From light-task-scheduler with Apache License 2.0 6 votes vote down vote up
private Job buildCronJob(QuartzJobContext quartzJobContext) {

        CronTriggerImpl cronTrigger = (CronTriggerImpl) quartzJobContext.getTrigger();
        String cronExpression = cronTrigger.getCronExpression();
        String description = cronTrigger.getDescription();
        int priority = cronTrigger.getPriority();
        String name = quartzJobContext.getName();

        Job job = new Job();
        job.setTaskId(name);
        job.setPriority(priority);
        job.setCronExpression(cronExpression);
        job.setSubmitNodeGroup(quartzLTSConfig.getJobClientProperties().getNodeGroup());
        job.setTaskTrackerNodeGroup(quartzLTSConfig.getTaskTrackerProperties().getNodeGroup());
        job.setParam("description", description);
        setJobProp(job);

        return job;
    }
 
Example #18
Source File: QuartzSchedulerBeanTargetEditor.java    From light-task-scheduler with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public void setValue(Object value) {

    List<Object> nativeQuartzTriggers = new ArrayList<Object>();

    if (value != null && value instanceof Collection) {

        Collection<Trigger> triggers = (Collection<Trigger>) value;
        List<QuartzJobContext> quartzJobContexts = new ArrayList<QuartzJobContext>(triggers.size());
        for (Trigger trigger : triggers) {
            if (trigger instanceof CronTriggerImpl) {
                quartzJobContexts.add(buildQuartzCronJob((CronTriggerImpl) trigger));
            } else if (trigger instanceof SimpleTriggerImpl) {
                quartzJobContexts.add(buildQuartzSimpleJob((SimpleTriggerImpl) trigger));
            } else {
                LOGGER.warn("Can't Proxy " + trigger.getClass().getName() + " Then Use Quartz Scheduler");
                nativeQuartzTriggers.add(trigger);
            }
        }
        context.getAgent().startProxy(quartzJobContexts);
    }
    super.setValue(nativeQuartzTriggers);
}
 
Example #19
Source File: SchedulerServiceImpl.java    From uflo with Apache License 2.0 6 votes vote down vote up
private void initScanReminderJob(){
	CronTriggerImpl trigger=new CronTriggerImpl();
	trigger.setName("UfloScanReminderTrigger");
	trigger.setKey(new TriggerKey("UfloScanReminderTrigger"));
	try {
		trigger.setCronExpression(SCAN_REMINDER_CRON);
		ScanReminderJob job=new ScanReminderJob();
		ScanReminderJobDetail detail=new ScanReminderJobDetail();
		detail.setSchedulerService(this);
		detail.setTaskService(taskService);
		detail.setReminderTaskList(reminderTaskList);
		detail.setJobClass(job.getClass());
		detail.setKey(new JobKey("UfloScanReminderJob"));
		scheduler.scheduleJob(detail, trigger);
	} catch (Exception e1) {
		throw new RuntimeException(e1);
	}
}
 
Example #20
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void removeTrigger() throws Exception {
    JobDetail job = getJobDetail();
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "triggerGroup", job.getKey());
    trigger1.getJobDataMap().put("foo", "bar");
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "triggerGroup", job.getKey());

    jobStore.storeJob(job, false);
    jobStore.storeTrigger(trigger1, false);
    jobStore.storeTrigger(trigger2, false);

    jobStore.removeTrigger(trigger1.getKey());

    // ensure that the trigger was removed, but the job was not
    assertThat(jobStore.retrieveTrigger(trigger1.getKey()), nullValue());
    assertThat(jobStore.retrieveJob(job.getKey()), not(nullValue()));

    // remove the second trigger
    jobStore.removeTrigger(trigger2.getKey());

    //  ensure that both the trigger and job were removed
    assertThat(jobStore.retrieveTrigger(trigger2.getKey()), nullValue());
    assertThat(jobStore.retrieveJob(job.getKey()), nullValue());
    MatcherAssert.assertThat(jedis.exists(schema.triggerDataMapHashKey(trigger1.getKey())), equalTo(false));
}
 
Example #21
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 6 votes vote down vote up
@Test
public void retrieveTrigger() throws Exception {
    CronTriggerImpl cronTrigger = getCronTrigger();
    jobStore.storeJob(getJobDetail(), false);
    jobStore.storeTrigger(cronTrigger, false);

    OperableTrigger operableTrigger = jobStore.retrieveTrigger(cronTrigger.getKey());

    assertThat(operableTrigger, instanceOf(CronTriggerImpl.class));
    assertThat(operableTrigger.getFireInstanceId(), notNullValue());
    CronTriggerImpl retrievedTrigger = (CronTriggerImpl) operableTrigger;

    assertEquals(cronTrigger.getCronExpression(), retrievedTrigger.getCronExpression());
    assertEquals(cronTrigger.getTimeZone(), retrievedTrigger.getTimeZone());
    assertEquals(cronTrigger.getStartTime(), retrievedTrigger.getStartTime());
}
 
Example #22
Source File: TaskManager.java    From GOAi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 添加任务
 */
private ScheduleJob checkJob(ScheduledMethod mt, int number) {
    Method method = mt.getMethod();
    Trigger trigger = mt.getScheduledInfo().getTrigger();
    String description = this.id + "-" + method.getName() + "-" + number;
    this.log.info("{} {}", description, mt.getScheduledInfo().getTip());
    JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class)
            .withDescription(description)
            .withIdentity(this.taskClass.getSimpleName(), description)
            .build();
    if (trigger instanceof CronTriggerImpl) {
        ((CronTriggerImpl) trigger).setName(description);
    }
    if (trigger instanceof SimpleTriggerImpl) {
        ((SimpleTriggerImpl) trigger).setName(description);
    }
    return new ScheduleJob(jobDetail, trigger);
}
 
Example #23
Source File: ActionListEmailServiceImpl.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void addTriggerToScheduler(CronTriggerImpl trigger) throws SchedulerException {
    boolean triggerExists = (getScheduler().getTrigger(trigger.getKey()) != null);
    if (!triggerExists) {
        try {
            getScheduler().scheduleJob(trigger);
        } catch (ObjectAlreadyExistsException ex) {
            getScheduler().rescheduleJob(trigger.getKey(),trigger);
        }
    } else {
        getScheduler().rescheduleJob(trigger.getKey(),trigger);
    }
}
 
Example #24
Source File: ActionListEmailServiceImpl.java    From rice with Educational Community License v2.0 5 votes vote down vote up
@Override
public void scheduleBatchEmailReminders() throws Exception {
    String emailBatchGroup = "Email Batch";
    String dailyCron = ConfigContext.getCurrentContextConfig()
            .getProperty(KewApiConstants.DAILY_EMAIL_CRON_EXPRESSION);
    if (!StringUtils.isBlank(dailyCron)) {
        LOG.info("Scheduling Daily Email batch with cron expression: " + dailyCron);
        CronTriggerImpl dailyTrigger = new CronTriggerImpl(DAILY_TRIGGER_NAME, emailBatchGroup, dailyCron);
        JobDetailImpl dailyJobDetail = new JobDetailImpl(DAILY_JOB_NAME, emailBatchGroup, DailyEmailJob.class);
        dailyTrigger.setJobName(dailyJobDetail.getName());
        dailyTrigger.setJobGroup(dailyJobDetail.getGroup());
        addJobToScheduler(dailyJobDetail);
        addTriggerToScheduler(dailyTrigger);
    } else {
        LOG.warn("No " + KewApiConstants.DAILY_EMAIL_CRON_EXPRESSION
                + " parameter was configured.  Daily Email batch was not scheduled!");
    }

    String weeklyCron = ConfigContext.getCurrentContextConfig().getProperty(
            KewApiConstants.WEEKLY_EMAIL_CRON_EXPRESSION);
    if (!StringUtils.isBlank(weeklyCron)) {
        LOG.info("Scheduling Weekly Email batch with cron expression: " + weeklyCron);
        CronTriggerImpl weeklyTrigger = new CronTriggerImpl(WEEKLY_TRIGGER_NAME, emailBatchGroup, weeklyCron);
        JobDetailImpl weeklyJobDetail = new JobDetailImpl(WEEKLY_JOB_NAME, emailBatchGroup, WeeklyEmailJob.class);
        weeklyTrigger.setJobName(weeklyJobDetail.getName());
        weeklyTrigger.setJobGroup(weeklyJobDetail.getGroup());
        addJobToScheduler(weeklyJobDetail);
        addTriggerToScheduler(weeklyTrigger);
    } else {
        LOG.warn("No " + KewApiConstants.WEEKLY_EMAIL_CRON_EXPRESSION
                + " parameter was configured.  Weekly Email batch was not scheduled!");
    }
}
 
Example #25
Source File: QuartzSchedulerBeanTargetEditor.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
private QuartzJobContext buildQuartzCronJob(CronTriggerImpl cronTrigger) {
    QuartzJobContext quartzJobContext = new QuartzJobContext();
    quartzJobContext.setTrigger(cronTrigger);
    quartzJobContext.setType(QuartzJobType.CRON);
    quartzJobContext.setName(cronTrigger.getName());

    buildQuartzJobContext(quartzJobContext, cronTrigger);

    return quartzJobContext;
}
 
Example #26
Source File: StoreJobTest.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
@Test
public void pauseJobsContains() throws Exception {
    JobDetail job1 = getJobDetail("job1", "jobGroup1");
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "triggerGroup1", job1.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "triggerGroup1", job1.getKey());
    storeJobAndTriggers(job1, trigger1, trigger2);
    JobDetail job2 = getJobDetail("job2", "yobGroup2");
    CronTriggerImpl trigger3 = getCronTrigger("trigger3", "triggerGroup3", job2.getKey());
    CronTriggerImpl trigger4 = getCronTrigger("trigger4", "triggerGroup4", job2.getKey());
    storeJobAndTriggers(job2, trigger3, trigger4);

    // Pause job groups containing "foo". Should result in no jobs being paused.
    Collection<String> pausedJobs = jobStore.pauseJobs(GroupMatcher.jobGroupContains("foo"));
    assertThat(pausedJobs, hasSize(0));

    // pause jobs containing "Group"
    pausedJobs = jobStore.pauseJobs(GroupMatcher.jobGroupContains("Group"));
    assertThat(pausedJobs, hasSize(2));
    assertThat(pausedJobs, containsInAnyOrder("jobGroup1", "yobGroup2"));

    // ensure that both jobs were added to the paused jobs set
    assertTrue(jedis.sismember(schema.pausedJobGroupsSet(), schema.jobGroupSetKey(job1.getKey())));
    assertTrue(jedis.sismember(schema.pausedJobGroupsSet(), schema.jobGroupSetKey(job2.getKey())));

    // ensure that all triggers were paused
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger2.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger3.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger4.getKey()));
}
 
Example #27
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
@Test
public void getTriggersForJob() throws Exception {
    JobDetail job = getJobDetail();
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "triggerGroup", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "triggerGroup", job.getKey());

    jobStore.storeJob(job, false);
    jobStore.storeTrigger(trigger1, false);
    jobStore.storeTrigger(trigger2, false);

    List<OperableTrigger> triggers = jobStore.getTriggersForJob(job.getKey());
    assertThat(triggers, hasSize(2));
}
 
Example #28
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
@Test
public void resumeTriggersEndsWith() throws Exception {
    JobDetail job = getJobDetail();
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "group1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "group1", job.getKey());
    CronTriggerImpl trigger3 = getCronTrigger("trigger3", "group2", job.getKey());
    CronTriggerImpl trigger4 = getCronTrigger("trigger4", "group3", job.getKey());
    storeJobAndTriggers(job, trigger1, trigger2, trigger3, trigger4);

    Collection<String> pausedGroups = jobStore.pauseTriggers(GroupMatcher.triggerGroupEndsWith("1"));

    assertThat(pausedGroups, hasSize(1));
    assertThat(pausedGroups, containsInAnyOrder("group1"));

    // ensure that the triggers were actually paused
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(trigger2.getKey()));

    // resume triggers
    Collection<String> resumedGroups = jobStore.resumeTriggers(GroupMatcher.triggerGroupEndsWith("1"));

    assertThat(resumedGroups, hasSize(1));
    assertThat(resumedGroups, containsInAnyOrder("group1"));

    // ensure that the triggers were actually resumed
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger1.getKey()));
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(trigger2.getKey()));
}
 
Example #29
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
@Test
public void resumeTriggersEquals() throws Exception {
    // store triggers and job
    JobDetail job = getJobDetail();
    CronTriggerImpl trigger1 = getCronTrigger("trigger1", "group1", job.getKey());
    CronTriggerImpl trigger2 = getCronTrigger("trigger2", "group1", job.getKey());
    CronTriggerImpl trigger3 = getCronTrigger("trigger3", "group2", job.getKey());
    CronTriggerImpl trigger4 = getCronTrigger("trigger4", "group3", job.getKey());
    storeJobAndTriggers(job, trigger1, trigger2, trigger3, trigger4);

    // pause triggers
    Collection<String> pausedGroups = jobStore.pauseTriggers(GroupMatcher.triggerGroupEquals("group1"));

    assertThat(pausedGroups, hasSize(1));
    assertThat(pausedGroups, containsInAnyOrder("group1"));

    // ensure that the triggers were actually paused
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(new TriggerKey("trigger1", "group1")));
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(new TriggerKey("trigger2", "group1")));

    // resume triggers
    Collection<String> resumedGroups = jobStore.resumeTriggers(GroupMatcher.triggerGroupEquals("group1"));

    assertThat(resumedGroups, hasSize(1));
    assertThat(resumedGroups, containsInAnyOrder("group1"));

    // ensure that the triggers were resumed
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(new TriggerKey("trigger1", "group1")));
    assertEquals(Trigger.TriggerState.NORMAL, jobStore.getTriggerState(new TriggerKey("trigger2", "group1")));
}
 
Example #30
Source File: StoreTriggerTest.java    From quartz-redis-jobstore with Apache License 2.0 5 votes vote down vote up
@Test
public void pauseTrigger() throws Exception {
    SchedulerSignaler signaler = mock(SchedulerSignaler.class);
    AbstractRedisStorage storageDriver = new RedisStorage(new RedisJobStoreSchema(), new ObjectMapper(), signaler, "scheduler1", 2000);

    // store a trigger
    JobDetail job = getJobDetail();
    CronTriggerImpl cronTrigger = getCronTrigger("trigger1", "group1", job.getKey());
    cronTrigger.setNextFireTime(new Date(System.currentTimeMillis()));
    jobStore.storeTrigger(cronTrigger, false);

    // set the trigger's state to COMPLETED
    storageDriver.setTriggerState(RedisTriggerState.COMPLETED, 500, schema.triggerHashKey(cronTrigger.getKey()), jedis);
    jobStore.pauseTrigger(cronTrigger.getKey());

    // trigger's state should not have changed
    assertEquals(Trigger.TriggerState.COMPLETE, jobStore.getTriggerState(cronTrigger.getKey()));

    // set the trigger's state to BLOCKED
    storageDriver.setTriggerState(RedisTriggerState.BLOCKED, 500, schema.triggerHashKey(cronTrigger.getKey()), jedis);
    jobStore.pauseTrigger(cronTrigger.getKey());

    // trigger's state should be PAUSED
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(cronTrigger.getKey()));

    // set the trigger's state to ACQUIRED
    storageDriver.setTriggerState(RedisTriggerState.ACQUIRED, 500, schema.triggerHashKey(cronTrigger.getKey()), jedis);
    jobStore.pauseTrigger(cronTrigger.getKey());

    // trigger's state should be PAUSED
    assertEquals(Trigger.TriggerState.PAUSED, jobStore.getTriggerState(cronTrigger.getKey()));
}