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

The following examples show how to use org.quartz.Scheduler#getTriggersOfJob() . 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: SchedulerServiceSupplier.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Gets the job schedulation list.
 * 
 * @param jobName the job name
 * @param jobGroup the job group
 * 
 * @return the job schedulation list
 */
public   String getJobSchedulationList(String jobName, String jobGroup) {
	String xml = "";
	try {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 
		if (jobName == null || jobName.trim().equals("")) {
			SpagoBITracer.critical(SpagoBIConstants.NAME_MODULE, this.getClass().getName(), "getJobDefinition", 
								  "Missing job name request parameter!");
			throw new Exception("Job name not found !");
		}
		if (jobGroup == null || jobGroup.trim().equals("")) {
			SpagoBITracer.major(SpagoBIConstants.NAME_MODULE, this.getClass().getName(), "getJobDefinition", 
								"Missing job group name! Using default group...");
			jobGroup = Scheduler.DEFAULT_GROUP;
		}
		Trigger[] triggers = scheduler.getTriggersOfJob(jobName, jobGroup);
		xml = buildTriggersListXmlString(triggers);
	} catch (Exception e) {
		SpagoBITracer.major(SpagoBIConstants.NAME_MODULE, this.getClass().getName(), "getJobDefinition", 
							"Error while recovering job schedulation list ", e);
		xml = "<ROWS></ROWS>";
	}
	return xml;
}
 
Example 3
Source File: SiteScheduledJobsController.java    From engine with GNU General Public License v3.0 6 votes vote down vote up
@GetMapping(URL_LIST)
@SuppressWarnings("unchecked")
public List<Map<String, String>> listScheduledJobs() throws SchedulerException {
    List<Map<String, String>> jobs = new LinkedList<>();
    SiteContext siteContext = SiteContext.getCurrent();
    Scheduler scheduler = siteContext.getScheduler();
    if(scheduler != null) {
        List<String> groups = scheduler.getJobGroupNames();
        for (String group : groups) {
            Set<JobKey> keys = scheduler.getJobKeys(GroupMatcher.jobGroupEquals(group));
            for (JobKey key : keys) {
                List<Trigger> triggers = (List<Trigger>)scheduler.getTriggersOfJob(key);
                Map<String, String> job = new HashMap<>();
                job.put("name", key.getName());
                job.put("nextFireTime", triggers.get(0).getNextFireTime().toInstant().toString());
                jobs.add(job);
            }
        }
    }
    return jobs;
}
 
Example 4
Source File: JobServiceImpl.java    From griffin with Apache License 2.0 5 votes vote down vote up
List<? extends Trigger> getTriggers(String name, String group) throws
    SchedulerException {
    if (name == null || group == null) {
        return null;
    }
    JobKey jobKey = new JobKey(name, group);
    Scheduler scheduler = factory.getScheduler();
    return scheduler.getTriggersOfJob(jobKey);
}
 
Example 5
Source File: JobInstance.java    From griffin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void setJobStartTime(JobDetail jobDetail)
    throws SchedulerException {
    Scheduler scheduler = factory.getScheduler();
    JobKey jobKey = jobDetail.getKey();
    List<Trigger> triggers =
        (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
    Date triggerTime = triggers.get(0).getPreviousFireTime();
    jobStartTime = triggerTime.getTime();
}
 
Example 6
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 7
Source File: SchedulerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testScheduleMultipleTriggersForAJob() throws SchedulerException {
  JobDetail job = newJob(TestJob.class).withIdentity("job1", "group1").build();
  Trigger trigger1 = newTrigger()
      .withIdentity("trigger1", "group1")
      .startNow()
      .withSchedule(
          SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1)
              .repeatForever())
      .build();
  Trigger trigger2 = newTrigger()
      .withIdentity("trigger2", "group1")
      .startNow()
      .withSchedule(
          SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1)
              .repeatForever())
      .build();
  Set<Trigger> triggersForJob = new HashSet<Trigger>();
  triggersForJob.add(trigger1);
  triggersForJob.add(trigger2);

  Scheduler sched = createScheduler("testScheduleMultipleTriggersForAJob", 5);
  sched.scheduleJob(job, triggersForJob, true);

  List<? extends Trigger> triggersOfJob = sched.getTriggersOfJob(job.getKey());
  assertEquals(2, triggersOfJob.size());
  assertTrue(triggersOfJob.contains(trigger1));
  assertTrue(triggersOfJob.contains(trigger2));

  sched.shutdown(true);
}
 
Example 8
Source File: ScheduleServiceImpl.java    From fixflow with Apache License 2.0 5 votes vote down vote up
public List<Trigger> getTriggerList(String jobName, String jobGroup) {
	if(!getIsEnabled()){
		throw new FixFlowScheduleException(ExceptionCode.QUARZTEXCEPTION_ISENABLE);
	}
	Scheduler scheduler = getScheduler();
	try{
		@SuppressWarnings("unchecked")
		List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(new JobKey(jobName,jobGroup));
		return triggers;
	}catch(Exception e){
		throw new FixFlowException(e.getMessage(),e);
	}
}
 
Example 9
Source File: QuartzUtils.java    From quartz-web with Apache License 2.0 4 votes vote down vote up
public static List<? extends Trigger> getTriggersOfJob(String jobName,String jobGroup, Scheduler scheduler) throws SchedulerException {
    JobKey jobKey = getJobKey(jobName, jobGroup);
    return scheduler.getTriggersOfJob(jobKey);
}
 
Example 10
Source File: JobServiceImpl.java    From spring-boot-quartz-demo with MIT License 4 votes vote down vote up
/**
 * Get all jobs
 */
@Override
public List<Map<String, Object>> getAllJobs() {
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	try {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();

		for (String groupName : scheduler.getJobGroupNames()) {
			for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {

				String jobName = jobKey.getName();
				String jobGroup = jobKey.getGroup();

				//get job's trigger
				List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
				Date scheduleTime = triggers.get(0).getStartTime();
				Date nextFireTime = triggers.get(0).getNextFireTime();
				Date lastFiredTime = triggers.get(0).getPreviousFireTime();
				
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("jobName", jobName);
				map.put("groupName", jobGroup);
				map.put("scheduleTime", scheduleTime);
				map.put("lastFiredTime", lastFiredTime);
				map.put("nextFireTime", nextFireTime);
				
				if(isJobRunning(jobName)){
					map.put("jobStatus", "RUNNING");
				}else{
					String jobState = getJobState(jobName);
					map.put("jobStatus", jobState);
				}

				/*					Date currentDate = new Date();
				if (scheduleTime.compareTo(currentDate) > 0) {
					map.put("jobStatus", "scheduled");

				} else if (scheduleTime.compareTo(currentDate) < 0) {
					map.put("jobStatus", "Running");

				} else if (scheduleTime.compareTo(currentDate) == 0) {
					map.put("jobStatus", "Running");
				}*/

				list.add(map);
				System.out.println("Job details:");
				System.out.println("Job Name:"+jobName + ", Group Name:"+ groupName + ", Schedule Time:"+scheduleTime);
			}

		}
	} catch (SchedulerException e) {
		System.out.println("SchedulerException while fetching all jobs. error message :"+e.getMessage());
		e.printStackTrace();
	}
	return list;
}
 
Example 11
Source File: Quartz2Adapter.java    From javamelody with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
List<Trigger> getTriggersOfJob(JobDetail jobDetail, Scheduler scheduler)
		throws SchedulerException {
	return (List<Trigger>) scheduler.getTriggersOfJob(jobDetail.getKey());
}
 
Example 12
Source File: SchedulerTool.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public String processRefreshJobs()
{
  try
  {
    Scheduler scheduler = schedulerManager.getScheduler();
    Set<JobKey> jobs = scheduler.getJobKeys(GroupMatcher.groupEquals(Scheduler.DEFAULT_GROUP));
    jobDetailWrapperList = new ArrayList<JobDetailWrapper>();
    for (JobKey key : jobs) {
      JobDetailWrapper jobDetailWrapper = new JobDetailWrapperImpl();
      jobDetailWrapper.setJobDetail(scheduler.getJobDetail(key));
      List<? extends Trigger> triggers = scheduler.getTriggersOfJob(key);
      
      List<TriggerWrapper> triggerWrapperList = new ArrayList<TriggerWrapper>();
      for (Trigger trigger : triggers) {
        TriggerWrapper tw = new TriggerWrapperImpl();
        tw.setTrigger(trigger);
        triggerWrapperList.add(tw);
      }

      jobDetailWrapper.setTriggerWrapperList(triggerWrapperList);
      jobDetailWrapperList.add(jobDetailWrapper);
    }
  }
  catch (SchedulerException e)
  {
    log.error("Failed to get job details.", e);
  }

  // test for select all
  if (isSelectAllJobsSelected)
  {
    for (Iterator<JobDetailWrapper> i = jobDetailWrapperList.iterator(); i.hasNext();)
    {
      if (isSelectAllJobsSelected)
      {
        ((JobDetailWrapper) i.next()).setIsSelected(true);
      }
      else
      {
        ((JobDetailWrapper) i.next()).setIsSelected(false);
      }
    }
  }
  return "jobs";
}
 
Example 13
Source File: SchedulerTool.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public String processRefreshJobs()
{
  try
  {
    Scheduler scheduler = schedulerManager.getScheduler();
    Set<JobKey> jobs = scheduler.getJobKeys(GroupMatcher.groupEquals(Scheduler.DEFAULT_GROUP));
    jobDetailWrapperList = new ArrayList<JobDetailWrapper>();
    for (JobKey key : jobs) {
      JobDetailWrapper jobDetailWrapper = new JobDetailWrapperImpl();
      jobDetailWrapper.setJobDetail(scheduler.getJobDetail(key));
      List<? extends Trigger> triggers = scheduler.getTriggersOfJob(key);
      
      List<TriggerWrapper> triggerWrapperList = new ArrayList<TriggerWrapper>();
      for (Trigger trigger : triggers) {
        TriggerWrapper tw = new TriggerWrapperImpl();
        tw.setTrigger(trigger);
        triggerWrapperList.add(tw);
      }

      jobDetailWrapper.setTriggerWrapperList(triggerWrapperList);
      jobDetailWrapperList.add(jobDetailWrapper);
    }
  }
  catch (SchedulerException e)
  {
    log.error("Failed to get job details.", e);
  }

  // test for select all
  if (isSelectAllJobsSelected)
  {
    for (Iterator<JobDetailWrapper> i = jobDetailWrapperList.iterator(); i.hasNext();)
    {
      if (isSelectAllJobsSelected)
      {
        ((JobDetailWrapper) i.next()).setIsSelected(true);
      }
      else
      {
        ((JobDetailWrapper) i.next()).setIsSelected(false);
      }
    }
  }
  return "jobs";
}
 
Example 14
Source File: SchedulerAdapter.java    From iaf with Apache License 2.0 4 votes vote down vote up
/**
 * Get all jobgroups, jobs within this group, the jobdetail and the
 * associated triggers in XML format.
 */
public XmlBuilder getJobGroupNamesWithJobsToXml(Scheduler theScheduler, IbisManager ibisManager) {
	XmlBuilder xbRoot = new XmlBuilder("jobGroups");

	try {
		// process groups
		List<String> jgnames = theScheduler.getJobGroupNames();

		for (int i = 0; i < jgnames.size(); i++) {
			XmlBuilder el = new XmlBuilder("jobGroup");
			String jobGroupName = jgnames.get(i);
			el.addAttribute("name", jobGroupName);

			// process jobs within group
			XmlBuilder jb = new XmlBuilder("jobs");
			Set<JobKey> jobKeys = theScheduler.getJobKeys(GroupMatcher.jobGroupEquals(jobGroupName));

			for (JobKey jobKey : jobKeys) {
				XmlBuilder jn = new XmlBuilder("job");
				String jobName = jobKey.getName();
				jn.addAttribute("name", jobName);

				// details for job
				JobDetail jobDetail = theScheduler.getJobDetail(jobKey);
				XmlBuilder jd = jobDetailToXmlBuilder(jobDetail);
				jn.addSubElement(jd);

				// get the triggers for this job
				List<? extends Trigger> triggers = theScheduler.getTriggersOfJob(jobKey);
				XmlBuilder tr = getJobTriggers(triggers);
				jn.addSubElement(tr);


				JobDataMap jobDataMap = jobDetail.getJobDataMap();
				XmlBuilder datamap = jobDataMapToXmlBuilder(jobDataMap);
				jn.addSubElement(datamap);
				jb.addSubElement(jn);

				JobDef jobDef = null;
				if(ibisManager != null) {
					for (Configuration configuration : ibisManager.getConfigurations()) {
						jobDef = configuration.getScheduledJob(jobName);
						if (jobDef != null) {
							break;
						}
					}
				}
				XmlBuilder ms = getJobMessages(jobDef);
				jn.addSubElement(ms);
				XmlBuilder jrs= getJobRunStatistics(jobDef);
				jn.addSubElement(jrs);
			}
			el.addSubElement(jb);
			xbRoot.addSubElement(el);
		}
	} catch (SchedulerException se) {
		log.error(se);
	}

	return xbRoot;
}
 
Example 15
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 16
Source File: QuartzUtils.java    From quartz-web with Apache License 2.0 2 votes vote down vote up
/**
 * 获取Job中所有的Trigger
 * @param jobDetail
 * @param scheduler
 * @return
 * @throws SchedulerException
 */
public static List<? extends Trigger> getTriggersOfJob(JobDetail jobDetail, Scheduler scheduler) throws SchedulerException {
    return scheduler.getTriggersOfJob(jobDetail.getKey());
}