/** * Refresh metadata. Schedules the job to retrieve metadata. */ @PostConstruct public void refreshMetadata() { final Thread thread = new Thread(new Runnable() { @Override public void run() { buildMetadataResolverAggregate(); } }); thread.start(); final JobDetail job = JobBuilder.newJob(this.getClass()) .withIdentity(this.getClass().getSimpleName().concat(UUID.randomUUID().toString())).build(); final Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(this.getClass().getSimpleName().concat(UUID.randomUUID().toString())) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInMinutes(this.refreshIntervalInMinutes) .repeatForever()).build(); try { scheduler.scheduleJob(job, trigger); } catch (final SchedulerException e) { throw new RuntimeException(e); } }
/** * <p> * Associate a listener with a job. * </p> * * @param conn * the DB Connection * @param job * the job to associate with the listener * @param listener * the listener to insert * @return the number of rows inserted */ public int insertJobListener(Connection conn, JobDetail job, String listener) throws SQLException { PreparedStatement ps = null; try { ps = conn.prepareStatement(rtp(INSERT_JOB_LISTENER)); ps.setString(1, job.getName()); ps.setString(2, job.getGroup()); ps.setString(3, listener); return ps.executeUpdate(); } finally { closeStatement(ps); } }
/** * @param cData * @return JobDetail */ public static JobDetail newJobDetail(CompositeData cData) throws ClassNotFoundException { JobDetailImpl jobDetail = new JobDetailImpl(); int i = 0; jobDetail.setName((String) cData.get(ITEM_NAMES[i++])); jobDetail.setGroup((String) cData.get(ITEM_NAMES[i++])); jobDetail.setDescription((String) cData.get(ITEM_NAMES[i++])); Class<?> jobClass = Class.forName((String) cData.get(ITEM_NAMES[i++])); @SuppressWarnings("unchecked") Class<? extends Job> jobClassTyped = (Class<? extends Job>)jobClass; jobDetail.setJobClass(jobClassTyped); jobDetail.setJobDataMap(JobDataMapSupport.newJobDataMap((TabularData) cData.get(ITEM_NAMES[i++]))); jobDetail.setDurability((Boolean) cData.get(ITEM_NAMES[i++])); jobDetail.setRequestsRecovery((Boolean) cData.get(ITEM_NAMES[i++])); return jobDetail; }
private boolean prvateAddQuartz(Class<? extends Job> jobClass,JobKey jobKey, CronScheduleBuilder builder, SimpleScheduleBuilder repeatForever) { if(scheduler==null){ init(); } //创建一个任务计划生成器 设置任务名称与分组 创建任务计划 JobDetail job = JobBuilder.newJob(jobClass).withIdentity(jobKey).build(); //创建一个触发生成器 设置触发器名称与分组 设置触发器出发条件 创建触发器 Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobKey.getName(),jobKey.getGroup()).withSchedule(builder==null?repeatForever:builder).build(); try { scheduler.scheduleJob(job, trigger); outLog("添加任务计划成功!"); return true; } catch (SchedulerException e) { outLog("添加任务计划失败!"); } return false; }
public int insertExtendedTriggerProperties(Connection conn, OperableTrigger trigger, String state, JobDetail jobDetail) throws SQLException, IOException { SimpleTrigger simpleTrigger = (SimpleTrigger)trigger; PreparedStatement ps = null; try { ps = conn.prepareStatement(Util.rtp(INSERT_SIMPLE_TRIGGER, tablePrefix, schedNameLiteral)); ps.setString(1, trigger.getKey().getName()); ps.setString(2, trigger.getKey().getGroup()); ps.setInt(3, simpleTrigger.getRepeatCount()); ps.setBigDecimal(4, new BigDecimal(String.valueOf(simpleTrigger.getRepeatInterval()))); ps.setInt(5, simpleTrigger.getTimesTriggered()); return ps.executeUpdate(); } finally { Util.closeStatement(ps); } }
public JobDetail updateMethodInovkeJob(String schedulerName, String jobName, String jobGroup, String jobClass, Object[] constructorArguments, String jobClassMethodName, Object[] jobClassMethodArgs, String description) throws SchedulerException { Assert.notNull(jobClass, "jobClass can not be null"); Assert.notEmpty(schedulerName, "schedulerName can not be empty"); Assert.notEmpty(jobName, "jobName can not be empty"); Assert.notEmpty(jobGroup, "jobGroup can not be empty"); Assert.notEmpty(jobClassMethodName, "jobClassMethodName can not be empty"); JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put("jobClass", jobClass); jobDataMap.put("constructorArguments", constructorArguments); jobDataMap.put("jobClassMethodName", jobClassMethodName); jobDataMap.put("jobClassMethodArgs", jobClassMethodArgs); JobDetail jobDetail = JobBuilder.newJob(MethodInvokeJob.class).withIdentity(jobName, jobGroup) .withDescription(description).setJobData(jobDataMap).storeDurably().build(); updateJob(schedulerName, jobDetail); return jobDetail; }
/** * @Description: 添加一个定时任务 * @param jobName 任务名 * @param jobGroupName 任务组名 * @param triggerName 触发器名 * @param triggerGroupName 触发器组名 * @param jobClass 任务 * @param time 时间设置,参考quartz说明文档 * */ @SuppressWarnings("unchecked") public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail job = newJob(jobClass) .withIdentity(jobName, jobGroupName) .build(); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(time); // 按新的cronExpression表达式构建一个新的trigger Trigger trigger = TriggerBuilder .newTrigger() .withIdentity(triggerName, triggerGroupName) .withSchedule(scheduleBuilder).build(); sched.scheduleJob(job, trigger); // 启动 if (!sched.isShutdown()) { sched.start(); } } catch (Exception e) { throw new RuntimeException(e); } }
/** * 创建定时任务 */ public static void createScheduleJob(Scheduler scheduler, ScheduleJobEntity scheduleJob) { try { //构建job信息 JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(getJobKey(scheduleJob.getJobId())).build(); //表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()) .withMisfireHandlingInstructionDoNothing(); //按新的cronExpression表达式构建一个新的trigger CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(scheduleJob.getJobId())).withSchedule(scheduleBuilder).build(); //放入参数,运行时的方法可以获取 jobDetail.getJobDataMap().put(ScheduleJobEntity.JOB_PARAM_KEY, new Gson().toJson(scheduleJob)); scheduler.scheduleJob(jobDetail, trigger); //暂停任务 if(scheduleJob.getStatus() == ScheduleStatus.PAUSE.getValue()){ pauseJob(scheduler, scheduleJob.getJobId()); } } catch (SchedulerException e) { throw new RRException("创建定时任务失败", e); } }
@Transactional(readOnly = true) public Optional<JobDescriptor> findJob(String group, String name) { // @formatter:off try { JobDetail jobDetail = scheduler.getJobDetail(jobKey(name, group)); if(Objects.nonNull(jobDetail)) return Optional.of( JobDescriptor.buildDescriptor(jobDetail, scheduler.getTriggersOfJob(jobKey(name, group)))); } catch (SchedulerException e) { log.error("Could not find job with key - {}.{} due to error - {}", group, name, e.getLocalizedMessage()); } // @formatter:on log.warn("Could not find job with key - {}.{}", group, name); return Optional.empty(); }
/** * 封装ScheduleJob对象 * * @param scheduleJob * @param scheduler * @param jobKey * @param trigger */ private void wrapScheduleJob(ScheduleJobEntity scheduleJob, Scheduler scheduler, JobKey jobKey, Trigger trigger) { try { scheduleJob.setJobName(jobKey.getName()); scheduleJob.setJobGroup(jobKey.getGroup()); JobDetail jobDetail = scheduler.getJobDetail(jobKey); scheduleJob.setJobClass(jobDetail.getJobClass()); scheduleJob.setJobDesc(jobDetail.getDescription()); Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); scheduleJob.setTriggerStatus(triggerState.name()); if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger) trigger; TriggerKey triggerKey = cronTrigger.getKey(); scheduleJob.setTriggerName(triggerKey.getName()); scheduleJob.setTriggerGroup(triggerKey.getGroup()); scheduleJob.setNextFireTime(cronTrigger.getNextFireTime()); scheduleJob.setCronExpression(cronTrigger.getCronExpression()); scheduleJob.setStartDate(cronTrigger.getStartTime()); scheduleJob.setEndDate(cronTrigger.getEndTime()); } } catch (SchedulerException e) { logger.error("获取触发器状态失败", e); throw new ServiceException(e); } }
/** * Ensures that jobs that are coded without a delay run without delay. * @throws Exception */ @Test public void testCodedCronTriggerBean() throws Exception { final String JOB_NAME = "codedCronJob"; List<Long> jobRuns = this.getRunList(JOB_NAME); assertEquals(0, jobRuns.size()); scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); CronTriggerBean ctBean = new CronTriggerBean(); ctBean.setBeanName("Dummy"); ctBean.setCronExpression("0/1 * * * * ? *"); ctBean.setEnabled(true); JobDetail jobDetail = new JobDetail(JOB_NAME, "DefaultGroup", DummyJob.class); ctBean.setJobDetail(jobDetail); ctBean.setScheduler(scheduler); ctBean.afterPropertiesSet(); assertJobRunsAfterInterval(jobRuns); scheduler.shutdown(); this.assertJobStopsAfterShutdown(jobRuns); }
public int updateExtendedTriggerProperties(Connection conn, OperableTrigger trigger, String state, JobDetail jobDetail) throws SQLException, IOException { CronTrigger cronTrigger = (CronTrigger)trigger; PreparedStatement ps = null; try { ps = conn.prepareStatement(Util.rtp(UPDATE_CRON_TRIGGER, tablePrefix, schedNameLiteral)); ps.setString(1, cronTrigger.getCronExpression()); ps.setString(2, cronTrigger.getTimeZone().getID()); ps.setString(3, trigger.getKey().getName()); ps.setString(4, trigger.getKey().getGroup()); return ps.executeUpdate(); } finally { Util.closeStatement(ps); } }
public List<ScheduleJob> getRunningJobList() throws SchedulerException{ List<JobExecutionContext> executingJobList = scheduler.getCurrentlyExecutingJobs(); List<ScheduleJob> jobList = new ArrayList<>(executingJobList.size()); for(JobExecutionContext executingJob : executingJobList){ ScheduleJob scheduleJob = new ScheduleJob(); JobDetail jobDetail = executingJob.getJobDetail(); JobKey jobKey = jobDetail.getKey(); Trigger trigger = executingJob.getTrigger(); this.wrapScheduleJob(scheduleJob,scheduler,jobKey,trigger); jobList.add(scheduleJob); } return jobList; }
private void startIndexerJob() throws SchedulerException { Scheduler scheduler = Singleton.getScheduler(); // Setup the indexer which runs forever indexing JobDetail job = newJob(IndexJob.class) .withIdentity("indexjob") .build(); SimpleTrigger trigger = newTrigger() .withIdentity("indexerjob") .withSchedule(simpleSchedule() .withIntervalInHours(24) .repeatForever() ) .build(); scheduler.scheduleJob(job, trigger); scheduler.start(); }
public void updateJob(String group, String name, JobDescriptor descriptor) { try { JobDetail oldJobDetail = scheduler.getJobDetail(jobKey(name, group)); if(Objects.nonNull(oldJobDetail)) { JobDataMap jobDataMap = oldJobDetail.getJobDataMap(); jobDataMap.put("subject", descriptor.getSubject()); jobDataMap.put("messageBody", descriptor.getMessageBody()); jobDataMap.put("to", descriptor.getTo()); jobDataMap.put("cc", descriptor.getCc()); jobDataMap.put("bcc", descriptor.getBcc()); JobBuilder jb = oldJobDetail.getJobBuilder(); JobDetail newJobDetail = jb.usingJobData(jobDataMap).storeDurably().build(); scheduler.addJob(newJobDetail, true); log.info("Updated job with key - {}", newJobDetail.getKey()); return; } log.warn("Could not find job with key - {}.{} to update", group, name); } catch (SchedulerException e) { log.error("Could not find job with key - {}.{} to update due to error - {}", group, name, e.getLocalizedMessage()); } }
/** * 查询所有定时任务 * * @param entity * @return Map * @throws SchedulerException */ @SuppressWarnings({ "rawtypes", "serial", "unchecked" }) @Override public Object selectByWhere(T entity) throws SchedulerException { List<String> groups = scheduler.getJobGroupNames(); List<HashMap<String, Object>> jobList = new ArrayList<HashMap<String, Object>>(); for (String group : groups) { if (null != entity.getGroup() && !group.contains(entity.getGroup())) { continue; } Set<JobKey> jobKeys = scheduler.getJobKeys(new GroupMatcher(group, StringOperatorName.EQUALS) { }); for (JobKey jobKey : jobKeys) { if (null != entity.getName() && !jobKey.toString().contains(entity.getName())) { continue; } JobDetail jobDetail = scheduler.getJobDetail(jobKey); HashMap<String, Object> jobInfoMap = new HashMap<String, Object>(); List<? extends Trigger> triggers = scheduler .getTriggersOfJob(jobKey); jobInfoMap.put("triggers", triggers); jobInfoMap.put("jobDetail", jobDetail); jobInfoMap.put("params", JSONArray.fromObject(jobDetail.getJobDataMap()) .toString()); jobInfoMap.put("type", "Kettle"); jobList.add(jobInfoMap); } } Map<String, Object> map = new HashMap<String, Object>(); map.put("jobList", jobList); map.put("scheduler", scheduler); return map; }
/** * Interrupt all instances of the identified InterruptableJob executing in * this Scheduler instance. * * <p> * This method is not cluster aware. That is, it will only interrupt * instances of the identified InterruptableJob currently executing in this * Scheduler instance, not across the entire cluster. * </p> * * @see org.quartz.core.RemotableQuartzScheduler#interrupt(JobKey) */ public boolean interrupt(JobKey jobKey) throws UnableToInterruptJobException { List<JobExecutionContext> jobs = getCurrentlyExecutingJobs(); JobDetail jobDetail = null; Job job = null; boolean interrupted = false; for(JobExecutionContext jec : jobs) { jobDetail = jec.getJobDetail(); if (jobKey.equals(jobDetail.getKey())) { job = jec.getJobInstance(); if (job instanceof InterruptableJob) { ((InterruptableJob)job).interrupt(); interrupted = true; } else { throw new UnableToInterruptJobException( "Job " + jobDetail.getKey() + " can not be interrupted, since it does not implement " + InterruptableJob.class.getName()); } } } return interrupted; }
public void notifySchedulerListenersJobAdded(JobDetail jobDetail) { // build a list of all scheduler listeners that are to be notified... List<SchedulerListener> schedListeners = buildSchedulerListenerList(); // notify all scheduler listeners for(SchedulerListener sl: schedListeners) { try { sl.jobAdded(jobDetail); } catch (Exception e) { getLog().error( "Error while notifying SchedulerListener of JobAdded.", e); } } }
public JobDetail getJobDetailByTriggerName(Trigger trigger) { try { return this.scheduler.getJobDetail(trigger.getJobKey()); } catch (Exception e) { logger.error(e.getMessage(), e); } return null; }
/** * @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 * @param jobName 任务名 * @param cls 任务 * @param time 时间设置,参考quartz说明文档 * */ @SuppressWarnings("unchecked") public static void addJob(String jobName, Class cls, String cron,String groovyName,String groovyMethod,String paramStr) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail job = newJob(cls) .withIdentity(jobName, JOB_GROUP_NAME) .build(); // 添加具体任务方法 job.getJobDataMap().put("groovyName", groovyName); job.getJobDataMap().put("groovyMethod", groovyMethod); job.getJobDataMap().put("paramStr", paramStr); // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron); // 按新的cronExpression表达式构建一个新的trigger Trigger trigger = TriggerBuilder .newTrigger() .withIdentity(jobName, TRIGGER_GROUP_NAME) .withSchedule(scheduleBuilder).build(); //交给scheduler去调度 sched.scheduleJob(job, trigger); // 启动 if (!sched.isShutdown()) { sched.start(); } } catch (Exception e) { throw new RuntimeException(e); } }
@Transactional @Override public boolean addJob(ScheduleJobEntity job) { try { Scheduler scheduler = schedulerFactoryBean.getScheduler(); JobDetail jobDetail = job.getJobDetail(); if(StringUtils.isEmpty(job.getTriggerGroup())) { //使用默认组名称:DEFAULT job.setTriggerGroup(Scheduler.DEFAULT_GROUP); } // 存储job jobDetail.getJobDataMap().put("scheduleJob", job); if(!StringUtils.isEmpty(job.getTriggerName())){ // 表达式调度构建器 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder .cronSchedule(job.getCronExpression()); // 按新的cronExpression表达式构建一个新的trigger CronTrigger trigger = newTrigger() .withIdentity(job.getTriggerName(),job.getTriggerGroup()) .startAt(job.getStartDate()) // job开始日期 .endAt(job.getEndDate())// job结束日期 .withSchedule(scheduleBuilder).build(); // 将job添加到quartz的scheduler容器 scheduler.scheduleJob(jobDetail, trigger); }else { scheduler.addJob(jobDetail, true); } return Boolean.TRUE; } catch (SchedulerException e) { throw new ServiceException(e); } }
public CompositeData getJobDetail(String jobName, String jobGroupName) throws Exception { try { JobDetail jobDetail = scheduler.getJobDetail(jobKey(jobName, jobGroupName)); return JobDetailSupport.toCompositeData(jobDetail); } catch (Exception e) { throw newPlainException(e); } }
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) { JobDetail jobDetail = context.getJobDetail(); if (!(jobDetail instanceof SpringBeanJobExecutorDetail)) { return; } JobDataMap map = jobDetail.getJobDataMap(); if (!map.containsKey(IJobDefinitionService.JOB_DEFINITION_ID)) { return; } Date end = new Date(); String exception = getExceptionStackMessage(jobException); JobHistory history = new JobHistory(); history.setSuccessful(exception == null ? true : false); if (exception != null) { history.setExceptionMessage(exception.length() > 1500 ? exception .substring(0, 1500) : exception); } history.setEndDate(end); history.setStartDate((Date)map.get(START_DATE_KEY)); history.setId(UUID.randomUUID().toString()); history.setJobId(map.getString(IJobDefinitionService.JOB_DEFINITION_ID)); Session session = getSessionFactory().openSession(); try { session.save(history); } finally { session.flush(); session.close(); } }
public int insertExtendedTriggerProperties(Connection conn, OperableTrigger trigger, String state, JobDetail jobDetail) throws SQLException, IOException { SimplePropertiesTriggerProperties properties = getTriggerProperties(trigger); PreparedStatement ps = null; try { ps = conn.prepareStatement(Util.rtp(INSERT_SIMPLE_PROPS_TRIGGER, tablePrefix, schedNameLiteral)); ps.setString(1, trigger.getKey().getName()); ps.setString(2, trigger.getKey().getGroup()); ps.setString(3, properties.getString1()); ps.setString(4, properties.getString2()); ps.setString(5, properties.getString3()); ps.setInt(6, properties.getInt1()); ps.setInt(7, properties.getInt2()); ps.setLong(8, properties.getLong1()); ps.setLong(9, properties.getLong2()); ps.setBigDecimal(10, properties.getDecimal1()); ps.setBigDecimal(11, properties.getDecimal2()); ps.setBoolean(12, properties.isBoolean1()); ps.setBoolean(13, properties.isBoolean2()); return ps.executeUpdate(); } finally { Util.closeStatement(ps); } }
public List<TaskScheduled> getAllJobDetail() { List<TaskScheduled> result = new LinkedList<TaskScheduled>(); try { GroupMatcher<JobKey> matcher = GroupMatcher.jobGroupContains(""); Set<JobKey> jobKeys = scheduler.getJobKeys(matcher); for (JobKey jobKey : jobKeys) { JobDetail jobDetail = scheduler.getJobDetail(jobKey); List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey); for (Trigger trigger : triggers) { TaskScheduled job = new TaskScheduled(); job.setTaskName(jobKey.getName()); job.setTaskGroup(jobKey.getGroup()); TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); job.setStatus(triggerState.name()); if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger)trigger; String cronExpression = cronTrigger.getCronExpression(); job.setTaskCron(cronExpression); } job.setPreviousFireTime(trigger.getPreviousFireTime()); job.setNextFireTime(trigger.getNextFireTime()); JobDataMap jobDataMap = trigger.getJobDataMap(); job.setTaskType(jobDataMap.getString("taskType")); job.setTargetSystem(jobDataMap.getString("targetSystem")); job.setTargetObject(jobDataMap.getString("targetObject")); job.setTargetMethod(jobDataMap.getString("targetMethod")); job.setContactName(jobDataMap.getString("contactName")); job.setContactEmail(jobDataMap.getString("contactEmail")); job.setTaskDesc(jobDetail.getDescription()); String jobClass = jobDetail.getJobClass().getSimpleName(); if (jobClass.equals("StatefulJob")) { job.setJobType("statefulJob"); } else if (jobClass.equals("DefaultJob")) { job.setJobType("job"); } result.add(job); } } } catch (Exception e) { logger.error("Try to load All JobDetail cause error : ", e); } return result; }
public TabularData getAllJobDetails(String instanceId) throws SchedulerException { SchedulingContext cntx = new SchedulingContext(instanceId); List<JobDetail> detailList = new ArrayList<JobDetail>(); for (String jobGroupName : scheduler.getJobGroupNames(cntx)) { for (String jobName : scheduler.getJobNames(cntx, jobGroupName)) { detailList.add(scheduler.getJobDetail(cntx, jobName, jobGroupName)); } } return JobDetailSupport.toTabularData(detailList .toArray(new JobDetail[detailList.size()])); }
public void execute(JobExecutionContext context) throws JobExecutionException { JobDetail jd = context.getJobDetail(); ScheduledJob scheduledJob = (ScheduledJob) jd.getJobDataMap().get("scheduledJob"); SchedulerManager schedulerManager = (SchedulerManager) jd.getJobDataMap().get("schedulerManager"); AbstractJob job = scheduledJob.getJob(); if (job != null) { if (schedulerManager.getRunningScheduledJobs().contains(scheduledJob)) { Engine.logScheduler.warn("No start " + jd.getName() + " because another still running."); } else { jd.getJobDataMap().put("running", true); executeJob(job, jd.getName()); } } }
public static TabularData toTabularData(JobDetail[] jobDetails) { TabularData tData = new TabularDataSupport(TABULAR_TYPE); if (jobDetails != null) { ArrayList<CompositeData> list = new ArrayList<CompositeData>(); for (JobDetail jobDetail : jobDetails) { list.add(toCompositeData(jobDetail)); } tData.putAll(list.toArray(new CompositeData[list.size()])); } return tData; }
/** * 获取所有的Trigger * @param schedulerName * @return * @throws SchedulerException */ public List<? extends Trigger> getAllTriggersOfScheduler(String schedulerName) throws SchedulerException { List<Trigger> triggersOfScheduler = new ArrayList<Trigger>(); Scheduler scheduler = this.getAssertScheduler(schedulerName); List<JobDetail> jobDetails = getAllJobsOfScheduler(schedulerName); for (JobDetail jobDetail : jobDetails) { List<? extends Trigger> triggersOfJob = QuartzUtils.getTriggersOfJob(jobDetail, scheduler); triggersOfScheduler.addAll(triggersOfJob); } return triggersOfScheduler; }
public TriggerFiredBundle(JobDetail job, Trigger trigger, Calendar cal, boolean jobIsRecovering, Date fireTime, Date scheduledFireTime, Date prevFireTime, Date nextFireTime) { this.job = job; this.trigger = trigger; this.cal = cal; this.jobIsRecovering = jobIsRecovering; this.fireTime = fireTime; this.scheduledFireTime = scheduledFireTime; this.prevFireTime = prevFireTime; this.nextFireTime = nextFireTime; }
/** * <p> * Store the given <code>{@link org.quartz.JobDetail}</code> and <code>{@link org.quartz.Trigger}</code>. * </p> * * @param newJob * The <code>JobDetail</code> to be stored. * @param newTrigger * The <code>Trigger</code> to be stored. * @throws ObjectAlreadyExistsException * if a <code>Job</code> with the same name/group already * exists. */ public void storeJobAndTrigger(final JobDetail newJob, final OperableTrigger newTrigger) throws JobPersistenceException { executeInLock( (isLockOnInsert()) ? LOCK_TRIGGER_ACCESS : null, new VoidTransactionCallback() { public void executeVoid(Connection conn) throws JobPersistenceException { storeJob(conn, newJob, false); storeTrigger(conn, newTrigger, newJob, false, Constants.STATE_WAITING, false, false); } }); }
@Override public void storeJobsAndTriggers(Map<JobDetail, Set<? extends Trigger>> arg0, boolean arg1) throws ObjectAlreadyExistsException, JobPersistenceException { try { realJobStore.storeJobsAndTriggers(arg0, arg1); } catch (RejoinException e) { throw new JobPersistenceException("Store jobs and triggers failed due to client rejoin", e); } }
public void updateJob(String schedulerName, JobDetail jobDetail) throws SchedulerException { if (existJob(schedulerName, jobDetail.getKey().getName(), jobDetail.getKey().getGroup())) { //替换 this.addJob(schedulerName, jobDetail, true); }else{ throw new IllegalArgumentException("job [" + jobDetail.getKey().getName() + ":" + jobDetail.getKey().getGroup() + "] not exist"); } }
public JobDetail updateMethodInovkeJob(String schedulerName, String jobName, String jobGroup, String description, MethodInvoker methodInvoker) throws SchedulerException { Assert.notNull(methodInvoker, "methodInvoker can not be null"); Assert.notEmpty(schedulerName, "schedulerName can not be empty"); Assert.notEmpty(jobName, "jobName can not be empty"); Assert.notEmpty(jobGroup, "jobGroup can not be empty"); JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put("methodInvoker", methodInvoker); JobDetail jobDetail = JobBuilder.newJob(MethodInvokeJob.class).withIdentity(jobName, jobGroup) .withDescription(description).setJobData(jobDataMap).storeDurably().build(); updateJob(schedulerName, jobDetail); return jobDetail; }
public JobDetail updateStatefulMethodJob(String schedulerName, String jobName, String jobGroup, String description, MethodInvoker methodInvoker) throws SchedulerException { Assert.notNull(methodInvoker, "methodInvoker can not be null"); JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put("methodInvoker", methodInvoker); JobDetail jobDetail = JobBuilder.newJob(StatefulMethodInvokeJob.class).withIdentity(jobName, jobGroup) .withDescription(description).setJobData(jobDataMap).storeDurably().build(); updateJob(schedulerName, jobDetail); return jobDetail; }
/** * <p> * Get the <code>{@link JobDetail}</code> for the <code>Job</code> * instance with the given name and group. * </p> */ public JobDetail getJobDetail(SchedulingContext ctxt, String jobName, String jobGroup) throws SchedulerException { validateState(); if(jobGroup == null) { jobGroup = Scheduler.DEFAULT_GROUP; } return resources.getJobStore().retrieveJob(ctxt, jobName, jobGroup); }
@Override public int updateJobData(Connection conn, JobDetail job) throws IOException, SQLException { ByteArrayOutputStream baos = serializeJobData(job.getJobDataMap()); byte[] data = baos.toByteArray(); PreparedStatement ps = null; PreparedStatement ps2 = null; ResultSet rs = null; try { ps = conn.prepareStatement(rtp(SELECT_ORACLE_JOB_DETAIL_BLOB)); ps.setString(1, job.getKey().getName()); ps.setString(2, job.getKey().getGroup()); rs = ps.executeQuery(); int res = 0; if (rs.next()) { Blob dbBlob = writeDataToBlob(rs, 1, data); ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_BLOB)); ps2.setBlob(1, dbBlob); ps2.setString(2, job.getKey().getName()); ps2.setString(3, job.getKey().getGroup()); res = ps2.executeUpdate(); } return res; } finally { closeResultSet(rs); closeStatement(ps); closeStatement(ps2); } }
@Bean(name="cronTrigger2") public CronTriggerFactoryBean createSimpleCronTrigger2(@Qualifier("cronJobDetail") JobDetail jobDetail) { Map<String, Object> map = new HashMap<>(1); map.put("myKey", "myValue"); return QuartzUtils.createCronTrigger(jobDetail, "CTName2", "Cron", "CTDesc2", "0 0 0/1 1/1 * ? *", CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY, map, 10000L, new Date(), "Europe/Berlin", 1234); }
public JobDescriptor createJob(String group, JobDescriptor descriptor) { descriptor.setGroup(group); JobDetail jobDetail = descriptor.buildJobDetail(); Set<Trigger> triggersForJob = descriptor.buildTriggers(); log.info("About to save job with key - {}", jobDetail.getKey()); try { scheduler.scheduleJob(jobDetail, triggersForJob, false); log.info("Job with key - {} saved sucessfully", jobDetail.getKey()); } catch (SchedulerException e) { log.error("Could not save job with key - {} due to error - {}", jobDetail.getKey(), e.getLocalizedMessage()); throw new IllegalArgumentException(e.getLocalizedMessage()); } return descriptor; }
/** * <p> * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>, * passing the <code>SchedulingContext</code> associated with this * instance. * </p> */ public JobDetail getJobDetail(String jobName, String jobGroup) throws SchedulerException { return (JobDetail)invoke( "getJobDetail", new Object[] { schedulingContext, jobName, jobGroup }, new String[] { SchedulingContext.class.getName(), String.class.getName(), String.class.getName() }); }