public Registration run() throws Exception { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); final Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.setJobFactory(jobFactory.get()); for (Job j : jobProvider.get()) { JobDetail jobDetail = JobBuilder.newJob(j.getClass()) .withIdentity(j.getClass().getSimpleName() + "Job") .build(); if (shouldInstall(j.getClass())) { ScheduleInterval interval = j.getClass().getAnnotation(ScheduleInterval.class); if (interval != null) { Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(j.getClass().getSimpleName() + "TriggerId") .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(interval.value()) .repeatForever()) .build(); System.out.println( "Schedule " + j.getClass().getSimpleName() + " @ " + interval.value() + "sec"); scheduler.scheduleJob(jobDetail, trigger); } CronInterval cron = j.getClass().getAnnotation(CronInterval.class); if (cron != null) { Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(j.getClass().getSimpleName() + "TriggerId") .withSchedule(CronScheduleBuilder.cronSchedule(cron.value())) .build(); System.out.println("Schedule " + j.getClass().getSimpleName() + " @ " + cron.value()); scheduler.scheduleJob(jobDetail, trigger); } } } scheduler.start(); return new Registration() { public void unregister() { try { scheduler.shutdown(); } catch (Exception e) { } System.out.println("Schedular Stopped"); } }; }
public static void startJob(String jobName, Job job, String time) throws SchedulerException, ParseException { Scheduler sched = sf.getScheduler(); JobDetail jobDetail = new JobDetail(); jobDetail.setName(jobName); jobDetail.setGroup(JOB_GROUP_NAME); jobDetail.setJobClass(job.getClass()); CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME); trigger.setCronExpression(time); sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) { sched.start(); } }
/** * 添加一个定时任务 * * @param jobName 任务名 * @param jobGroupName 任务组名 * @param triggerName 触发器名 * @param triggerGroupName 触发器组名 * @param job 任务 * @throws SchedulerException * @throws ParseException */ public void addJob( String jobName, String jobGroupName, String triggerName, String triggerGroupName, Job job, String scheduletype) throws SchedulerException, ParseException { Scheduler sched = sf.getScheduler(); JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass()); // 任务名,任务组,任务执行类 jobDetail.getJobDataMap().put(JUST_SCHEDULE_RETURN, justSchedule); Trigger trigger = null; // 触发器 if (SCHEDULE_TYPE_TRIGGER_SIMPLE.equals(scheduletype)) { trigger = (SimpleTrigger) triggerManager.getSimpleTrigger(triggerName, triggerGroupName); } else if (SCHEDULE_TYPE_TRIGGER_CRON.equals(scheduletype)) { /* CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);//触发器名,触发器组 trigger.setCronExpression(time);//触发器时间设定 */ trigger = (CronTrigger) triggerManager.getCronTrigger(triggerName, triggerGroupName); } sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) sched.start(); }
private void registerJob( final String jobName, final Job jobInstance, final String cronExpression, final Date startAt, final Map<String, Object> jobMap) throws SchedulerException, ParseException { synchronized (scheduler.getScheduler()) { boolean jobAlreadyRunning = false; for (JobExecutionContext jobCtx : scheduler.getScheduler().getCurrentlyExecutingJobs()) { if (jobName.equals(jobCtx.getJobDetail().getKey().getName()) && Scheduler.DEFAULT_GROUP.equals(jobCtx.getJobDetail().getKey().getGroup())) { jobAlreadyRunning = true; LOG.debug("Job {} already running, cancel", jobCtx.getJobDetail().getKey()); } } if (jobAlreadyRunning) { return; } } // 0. unregister job unregisterJob(jobName); // 1. Job bean ApplicationContextProvider.getBeanFactory().registerSingleton(jobName, jobInstance); // 2. JobDetail bean JobBuilder jobDetailBuilder = JobBuilder.newJob(jobInstance.getClass()) .withIdentity(jobName) .usingJobData(new JobDataMap(jobMap)); // 3. Trigger if (cronExpression == null && startAt == null) { // Jobs added with no trigger must be durable scheduler.getScheduler().addJob(jobDetailBuilder.storeDurably().build(), true); } else { TriggerBuilder<?> triggerBuilder; if (cronExpression == null) { triggerBuilder = TriggerBuilder.newTrigger() .withIdentity(JobNamer.getTriggerName(jobName)) .startAt(startAt); } else { triggerBuilder = TriggerBuilder.newTrigger() .withIdentity(JobNamer.getTriggerName(jobName)) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)); if (startAt == null) { triggerBuilder = triggerBuilder.startNow(); } else { triggerBuilder = triggerBuilder.startAt(startAt); } } scheduler.getScheduler().scheduleJob(jobDetailBuilder.build(), triggerBuilder.build()); } }