Java Code Examples for org.quartz.Scheduler#getTriggerState()

The following examples show how to use org.quartz.Scheduler#getTriggerState() . 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: BatchJobOperatorImpl.java    From griffin with Apache License 2.0 6 votes vote down vote up
@Override
public JobState getState(AbstractJob job, String action)
    throws SchedulerException {
    JobState jobState = new JobState();
    Scheduler scheduler = factory.getScheduler();
    if (job.getGroup() == null || job.getName() == null) {
        return null;
    }
    TriggerKey triggerKey = triggerKey(job.getName(), job.getGroup());
    TriggerState triggerState = scheduler.getTriggerState(triggerKey);
    jobState.setState(triggerState.toString());
    jobState.setToStart(getStartStatus(triggerState));
    jobState.setToStop(getStopStatus(triggerState));
    setTriggerTime(job, jobState);
    return jobState;
}
 
Example 3
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 4
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 5
Source File: JobServiceImpl.java    From spring-boot-quartz-demo with MIT License 5 votes vote down vote up
/**
 * Get the current state of job
 */
public String getJobState(String jobName) {
	System.out.println("JobServiceImpl.getJobState()");

	try {
		String groupKey = "SampleGroup";
		JobKey jobKey = new JobKey(jobName, groupKey);

		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobDetail jobDetail = scheduler.getJobDetail(jobKey);

		List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobDetail.getKey());
		if(triggers != null && triggers.size() > 0){
			for (Trigger trigger : triggers) {
				TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());

				if (TriggerState.PAUSED.equals(triggerState)) {
					return "PAUSED";
				}else if (TriggerState.BLOCKED.equals(triggerState)) {
					return "BLOCKED";
				}else if (TriggerState.COMPLETE.equals(triggerState)) {
					return "COMPLETE";
				}else if (TriggerState.ERROR.equals(triggerState)) {
					return "ERROR";
				}else if (TriggerState.NONE.equals(triggerState)) {
					return "NONE";
				}else if (TriggerState.NORMAL.equals(triggerState)) {
					return "SCHEDULED";
				}
			}
		}
	} catch (SchedulerException e) {
		System.out.println("SchedulerException while checking job with name and group exist:"+e.getMessage());
		e.printStackTrace();
	}
	return null;
}
 
Example 6
Source File: QuartzAdapter.java    From javamelody with Apache License 2.0 4 votes vote down vote up
boolean isTriggerPaused(Trigger trigger, Scheduler scheduler) throws SchedulerException {
	return scheduler.getTriggerState(trigger.getName(),
			trigger.getGroup()) == Trigger.STATE_PAUSED;
}
 
Example 7
Source File: Quartz2Adapter.java    From javamelody with Apache License 2.0 4 votes vote down vote up
@Override
boolean isTriggerPaused(Trigger trigger, Scheduler scheduler) throws SchedulerException {
	return scheduler.getTriggerState(trigger.getKey()) == Trigger.TriggerState.PAUSED;
}
 
Example 8
Source File: StatisticsAdminController.java    From olat with Apache License 2.0 4 votes vote down vote up
private void refreshUIState() {
    boolean enabled = false;
    String cronExpression = "";
    if (CoreSpringFactory.containsBean("schedulerFactoryBean")) {
        log.info("refreshUIState: schedulerFactoryBean found");
        final Object schedulerFactoryBean = CoreSpringFactory.getBean("schedulerFactoryBean");
        if (schedulerFactoryBean != null && schedulerFactoryBean instanceof Scheduler) {
            final Scheduler schedulerBean = (Scheduler) schedulerFactoryBean;
            int triggerState;
            try {
                triggerState = schedulerBean.getTriggerState("updateStatisticsTrigger", null/* trigger group */);
                enabled = (triggerState != Trigger.STATE_NONE) && (triggerState != Trigger.STATE_ERROR);
                log.info("refreshUIState: updateStatisticsTrigger state was " + triggerState + ", enabled now: " + enabled);
            } catch (final SchedulerException e) {
                log.warn("refreshUIState: Got a SchedulerException while asking for the updateStatisticsTrigger's state", e);
            }
        }
        final CronTriggerBean triggerBean = (CronTriggerBean) CoreSpringFactory.getBean("updateStatisticsTrigger");
        final JobDetail jobDetail = triggerBean.getJobDetail();
        enabled &= jobDetail.getName().equals("statistics.job.enabled");
        log.info("refreshUIState: statistics.job.enabled check, enabled now: " + enabled);
        cronExpression = triggerBean.getCronExpression();
        final StatisticUpdateService statisticUpdateManager = getStatisticUpdateManager();
        if (statisticUpdateManager == null) {
            log.info("refreshUIState: statisticUpdateManager not configured");
            enabled = false;
        } else {
            enabled &= statisticUpdateManager.isEnabled();
            log.info("refreshUIState: statisticUpdateManager configured, enabled now: " + enabled);
        }
    } else {
        log.info("refreshUIState: schedulerFactoryBean not found");
    }
    if (enabled) {
        content.contextPut("status", getTranslator().translate("statistics.status.enabled", new String[] { cronExpression }));
    } else {
        content.contextPut("status", getTranslator().translate("statistics.status.disabled"));
    }
    content.contextPut("statisticEnabled", enabled);

    recalcLastUpdated();

    updateStatisticUpdateOngoingFlag();
}
 
Example 9
Source File: StatisticsAdminController.java    From olat with Apache License 2.0 4 votes vote down vote up
private void refreshUIState() {
    boolean enabled = false;
    String cronExpression = "";
    if (CoreSpringFactory.containsBean("schedulerFactoryBean")) {
        log.info("refreshUIState: schedulerFactoryBean found");
        final Object schedulerFactoryBean = CoreSpringFactory.getBean("schedulerFactoryBean");
        if (schedulerFactoryBean != null && schedulerFactoryBean instanceof Scheduler) {
            final Scheduler schedulerBean = (Scheduler) schedulerFactoryBean;
            int triggerState;
            try {
                triggerState = schedulerBean.getTriggerState("updateStatisticsTrigger", null/* trigger group */);
                enabled = (triggerState != Trigger.STATE_NONE) && (triggerState != Trigger.STATE_ERROR);
                log.info("refreshUIState: updateStatisticsTrigger state was " + triggerState + ", enabled now: " + enabled);
            } catch (final SchedulerException e) {
                log.warn("refreshUIState: Got a SchedulerException while asking for the updateStatisticsTrigger's state", e);
            }
        }
        final CronTriggerBean triggerBean = (CronTriggerBean) CoreSpringFactory.getBean("updateStatisticsTrigger");
        final JobDetail jobDetail = triggerBean.getJobDetail();
        enabled &= jobDetail.getName().equals("statistics.job.enabled");
        log.info("refreshUIState: statistics.job.enabled check, enabled now: " + enabled);
        cronExpression = triggerBean.getCronExpression();
        final StatisticUpdateService statisticUpdateManager = getStatisticUpdateManager();
        if (statisticUpdateManager == null) {
            log.info("refreshUIState: statisticUpdateManager not configured");
            enabled = false;
        } else {
            enabled &= statisticUpdateManager.isEnabled();
            log.info("refreshUIState: statisticUpdateManager configured, enabled now: " + enabled);
        }
    } else {
        log.info("refreshUIState: schedulerFactoryBean not found");
    }
    if (enabled) {
        content.contextPut("status", getTranslator().translate("statistics.status.enabled", new String[] { cronExpression }));
    } else {
        content.contextPut("status", getTranslator().translate("statistics.status.disabled"));
    }
    content.contextPut("statisticEnabled", enabled);

    recalcLastUpdated();

    updateStatisticUpdateOngoingFlag();
}
 
Example 10
Source File: ShowScheduler.java    From iaf with Apache License 2.0 4 votes vote down vote up
private Map<String, Object> getJobData(JobKey jobKey, boolean expanded) throws SchedulerException {
	Map<String, Object> jobData = new HashMap<String, Object>();
	Scheduler scheduler = getScheduler();
	String jobName = jobKey.getName();
	JobDetail job = scheduler.getJobDetail(jobKey);

	jobData.put("fullName", job.getKey().getGroup() + "." + job.getKey().getName());
	jobData.put("name", job.getKey().getName());
	jobData.put("group", job.getKey().getGroup());
	String description = "-";
	if (StringUtils.isNotEmpty(job.getDescription()))
		description = job.getDescription();
	jobData.put("description", description);
	jobData.put("stateful", job.isPersistJobDataAfterExecution() && job.isConcurrentExectionDisallowed());
	jobData.put("durable",job.isDurable());
	jobData.put("jobClass", job.getJobClass().getSimpleName());

	if(job instanceof IbisJobDetail) {
		jobData.put("type", ((IbisJobDetail) job).getJobType());
	}

	TriggerState state = scheduler.getTriggerState(TriggerKey.triggerKey(jobName, jobKey.getGroup()));
	jobData.put("state", state.name());

	jobData.put("triggers", getJobTriggers(scheduler.getTriggersOfJob(jobKey)));
	jobData.put("messages", getJobMessages(job));

	JobDataMap jobMap = job.getJobDataMap();
	jobData.put("properties", getJobData(jobMap));

	if(expanded) {
		JobDef jobDef = (JobDef) jobMap.get(ConfiguredJob.JOBDEF_KEY);
		jobData.put("adapter", jobDef.getAdapterName());
		jobData.put("receiver", jobDef.getReceiverName());
		jobData.put("message", jobDef.getMessage());
		
		Locker locker = jobDef.getLocker();
		if(locker != null) {
			jobData.put("locker", true);
			jobData.put("lockkey", locker.getObjectId());
		} else {
			jobData.put("locker", false);
		}
	}

	return jobData;
}
 
Example 11
Source File: JobServiceImpl.java    From fixflow with Apache License 2.0 4 votes vote down vote up
public Map<String, Object> getJobList(Map<String, Object> params) throws SchedulerException, SQLException {
	Map<String,Object> resultMap = new HashMap<String,Object>();
	String userId = StringUtil.getString(params.get("userId"));
	String queryId = StringUtil.getString(params.get("queryId"));
	ProcessEngine processEngine = getProcessEngine(userId);
	ScheduleService scheduleService = processEngine.getScheduleService();
	Scheduler scheduler = scheduleService.getScheduler();
	try{
		List<Map<String,Object>> jobList = new ArrayList<Map<String,Object>>();
		List<JobDetail> list = scheduleService.getJobList(queryId);
		for(JobDetail job :list){
			Map<String,Object> jobMap = new HashMap<String,Object>();
			jobMap.put("jobName", job.getKey().getName());
			jobMap.put("groupName", job.getKey().getGroup());
			jobMap.put("processName", job.getJobDataMap().get("processName"));
			jobMap.put("processId", job.getJobDataMap().get("processId"));
			jobMap.put("processKey", job.getJobDataMap().get("processKey"));
			jobMap.put("processInstanceId", job.getJobDataMap().get("processInstanceId"));
			jobMap.put("nodeId", job.getJobDataMap().get("nodeId"));
			jobMap.put("bizKey", job.getJobDataMap().get("bizKey"));
			jobMap.put("jobType", job.getJobDataMap().get("jobType"));
			jobMap.put("processId", job.getJobDataMap().get("processId"));
			jobMap.put("nodeName", job.getJobDataMap().get("nodeName"));
			jobMap.put("jobKeyGroup", job.getKey().getGroup());
			jobMap.put("jobKeyName", job.getKey().getName());
			//判断job下的trigger是否全为暂停状态
			List<Trigger> triggerList = (List<Trigger>) scheduler.getTriggersOfJob(job.getKey());
			boolean isPaused = false;
			for(Trigger t:triggerList){
				TriggerState ts = scheduler.getTriggerState(t.getKey());
				if(ts.equals(TriggerState.PAUSED)){
					isPaused = true;
					break;
				}
			}
			jobMap.put("isPaused", isPaused);
			jobList.add(jobMap);
		}
		resultMap.put("dataList", jobList);
	}finally{
		closeProcessEngine();
	}
	return resultMap;
}
 
Example 12
Source File: JobServiceImpl.java    From fixflow with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Map<String, Object> getJobTrigger(Map<String, Object> params) throws SQLException, SchedulerException {
	Map<String,Object> resultMap = new HashMap<String,Object>();
	String userId = StringUtil.getString(params.get("userId"));
	ProcessEngine processEngine = getProcessEngine(userId);
	ScheduleService scheduleService = processEngine.getScheduleService();
	Scheduler scheduler = scheduleService.getScheduler();
	try{
		String jobKeyName = StringUtil.getString(params.get("jobKeyName"));
		String jobKeyGroup = StringUtil.getString(params.get("jobKeyGroup"));
		List<Map<String,Object>> triggerList = new ArrayList<Map<String,Object>>();
		List<Trigger> triggers = scheduleService.getTriggerList(jobKeyName, jobKeyGroup);
		for(Trigger t : triggers){
			Map<String,Object> triggerMap = new HashMap<String,Object>();
			triggerMap.put("triggerName", t.getKey().getName());
			triggerMap.put("triggerGroup", t.getKey().getGroup());
			triggerMap.put("startTime", t.getStartTime());
			triggerMap.put("endTime", t.getEndTime());
			triggerMap.put("lastFireTime", t.getPreviousFireTime());
			triggerMap.put("nextFireTime", t.getNextFireTime());
			triggerMap.put("finalFireTime", t.getFinalFireTime());
			TriggerState ts = scheduler.getTriggerState(t.getKey());
			String triggerState = getTriggerStateByEmuType(ts);
			triggerMap.put("triggerState", triggerState);
			boolean isPaused = false;
			if(ts.equals(TriggerState.PAUSED)){
				isPaused = true;
			}
			triggerMap.put("isPaused", isPaused);
			triggerList.add(triggerMap);
		}
		resultMap.put("dataList", triggerList);
		Map<String,Object> jobMap = new HashMap<String,Object>();
		jobMap.put("jobKeyName", jobKeyName);
		jobMap.put("jobKeyGroup", jobKeyGroup);
		resultMap.put("job", jobMap);
	}finally{
		closeProcessEngine();
	}
	return resultMap;
}
 
Example 13
Source File: QuartzUtils.java    From quartz-web with Apache License 2.0 2 votes vote down vote up
/**
 * 获取
 * @param trigger
 * @param scheduler
 * @return
 * @throws SchedulerException
 */
public static TriggerState getTriggerState(Trigger trigger, Scheduler scheduler) throws SchedulerException {
    TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
    return triggerState;
}
 
Example 14
Source File: QuartzUtils.java    From quartz-web with Apache License 2.0 2 votes vote down vote up
/**
 * 对比两个Trigger是否相等
 * @param trigger
 * @param triggerState
 * @param scheduler
 * @return
 * @throws SchedulerException
 */
public static boolean triggerStateEquals(Trigger trigger, TriggerState triggerState, Scheduler scheduler)
        throws SchedulerException {
    TriggerState sourceTriggerState = scheduler.getTriggerState(trigger.getKey());
    return sourceTriggerState.ordinal() == triggerState.ordinal();
}