Java Code Examples for org.quartz.Trigger#TriggerState

The following examples show how to use org.quartz.Trigger#TriggerState . 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: JobTrigger.java    From spring-cloud-shop with MIT License 6 votes vote down vote up
/**
 * 获取所有job任务
 */
public static List<JobInfo> getJobs(Scheduler scheduler) throws SchedulerException {
    GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();

    List<JobInfo> jobs = Lists.newArrayList();
    Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);

    for (JobKey jobKey : jobKeys) {
        List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
        for (Trigger trigger : triggers) {

            JobInfo event = new JobInfo();
            event.setJobName(jobKey.getName());
            event.setJobGroup(jobKey.getGroup());
            event.setDescription(String.format("触发器 ======== %s", trigger.getKey()));
            Trigger.TriggerState state = scheduler.getTriggerState(trigger.getKey());
            event.setJobStatus(EnumUtils.getEnum(JobStatusEnums.class, state.name()).getCode());
            if (trigger instanceof CronTrigger) {
                CronTrigger cronTrigger = (CronTrigger) trigger;
                event.setCron(cronTrigger.getCronExpression());
                jobs.add(event);
            }
        }
    }
    return jobs;
}
 
Example 2
Source File: QssService.java    From seed with Apache License 2.0 6 votes vote down vote up
private ScheduleTask convertToScheduleTask(JobKey jobKey, Trigger trigger) throws SchedulerException {
    ScheduleTask task = new ScheduleTask();
    String[] jobname = jobKey.getName().split(":");
    task.setId(Long.parseLong(jobname[0]));
    task.setAppname(jobname[1]);
    task.setName(jobname[2]);
    //task.setGroup(jobKey.getGroup());
    //task.setDesc("触发器[" + trigger.getKey() + "]");
    //task.setStartTime(trigger.getStartTime());             //开始时间
    //task.setEndTime(trigger.getEndTime());                 //结束时间
    task.setNextFireTime(trigger.getNextFireTime());         //下次触发时间
    task.setPreviousFireTime(trigger.getPreviousFireTime()); //上次触发时间
    if(trigger instanceof CronTrigger){
        task.setCron(((CronTrigger)trigger).getCronExpression());
    }
    Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
    task.setStatus("N".equals(triggerState.name()) ? SeedConstants.QSS_STATUS_STOP : SeedConstants.QSS_STATUS_RUNNING);
    return task;
}
 
Example 3
Source File: DefaultClusteredJobStore.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>
 * Get the current state of the identified <code>{@link Trigger}</code>.
 * </p>
 * 
 * @see Trigger.TriggerState
 */
@Override
public Trigger.TriggerState getTriggerState(org.quartz.TriggerKey key) throws JobPersistenceException {

  TriggerWrapper tw;
  lock();
  try {
    tw = triggerFacade.get(key);
  } finally {
    unlock();
  }

  if (tw == null) { return Trigger.TriggerState.NONE; }

  if (tw.getState() == TriggerState.COMPLETE) { return Trigger.TriggerState.COMPLETE; }

  if (tw.getState() == TriggerState.PAUSED) { return Trigger.TriggerState.PAUSED; }

  if (tw.getState() == TriggerState.PAUSED_BLOCKED) { return Trigger.TriggerState.PAUSED; }

  if (tw.getState() == TriggerState.BLOCKED) { return Trigger.TriggerState.BLOCKED; }

  if (tw.getState() == TriggerState.ERROR) { return Trigger.TriggerState.ERROR; }

  return Trigger.TriggerState.NORMAL;
}
 
Example 4
Source File: ScheduleJobService.java    From springboot-quartz with MIT License 6 votes vote down vote up
private void wrapScheduleJob(ScheduleJob scheduleJob,Scheduler scheduler,JobKey jobKey,Trigger trigger){  
    try {  
        scheduleJob.setJobName(jobKey.getName());  
        scheduleJob.setJobGroup(jobKey.getGroup()); 
  
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);  
        ScheduleJob job = (ScheduleJob)jobDetail.getJobDataMap().get("scheduleJob");  
        scheduleJob.setDesc(job.getDesc());  
        scheduleJob.setJobId(job.getJobId());
  
        Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());  
        scheduleJob.setJobStatus(triggerState.name());  
        if(trigger instanceof CronTrigger){  
            CronTrigger cronTrigger = (CronTrigger)trigger;  
            String cronExpression = cronTrigger.getCronExpression();  
            scheduleJob.setCronExpression(cronExpression);  
        }  
    } catch (SchedulerException e) {  
        e.printStackTrace(); 
    }  
}
 
Example 5
Source File: JobTrigger.java    From spring-cloud-shop with MIT License 5 votes vote down vote up
/**
 * 更新定时任务
 *
 * @param scheduler      the scheduler
 * @param jobName        the job name
 * @param jobGroup       the job group
 * @param cronExpression the cron expression
 * @param param          the param
 */
private static void updateJob(Scheduler scheduler, String jobName, String jobGroup, String cronExpression, Object param) throws SchedulerException {

    // 同步或异步
    Class<? extends Job> jobClass = JobQuartzJobBean.class;
    JobDetail jobDetail = scheduler.getJobDetail(getJobKey(jobName, jobGroup));

    jobDetail = jobDetail.getJobBuilder().ofType(jobClass).build();

    // 更新参数 实际测试中发现无法更新
    JobDataMap jobDataMap = jobDetail.getJobDataMap();
    jobDataMap.put("JobAdapter", param);
    jobDetail.getJobBuilder().usingJobData(jobDataMap);

    TriggerKey triggerKey = getTriggerKey(jobName, jobGroup);

    // 表达式调度构建器
    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

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

    // 按新的cronExpression表达式重新构建trigger
    trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
    Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
    // 忽略状态为PAUSED的任务,解决集群环境中在其他机器设置定时任务为PAUSED状态后,集群环境启动另一台主机时定时任务全被唤醒的bug
    if (!JobEnums.PAUSE.name().equalsIgnoreCase(triggerState.name())) {
        // 按新的trigger重新设置job执行
        scheduler.rescheduleJob(triggerKey, trigger);
    }
}
 
Example 6
Source File: AbstractTerracottaJobStore.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Trigger.TriggerState getTriggerState(TriggerKey triggerKey) throws JobPersistenceException {
  try {
    return realJobStore.getTriggerState(triggerKey);
  } catch (RejoinException e) {
    throw new JobPersistenceException("Trigger state retrieval failed due to client rejoin", e);
  }
}
 
Example 7
Source File: QuartzManager.java    From quartz-web with Apache License 2.0 4 votes vote down vote up
public Trigger.TriggerState getTriggerState(String schedulerName, Trigger trigger) throws SchedulerException {
    Assert.notNull(trigger, "trigger can not be null");
    Scheduler scheduler = this.getAssertScheduler(schedulerName);
    return QuartzUtils.getTriggerState(trigger, scheduler);
}
 
Example 8
Source File: PlainTerracottaJobStore.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Trigger.TriggerState getTriggerState(final TriggerKey triggerKey) throws JobPersistenceException {
  return clusteredJobStore.getTriggerState(triggerKey);
}