明:本文爲博主原創文章,未經博主容許不得轉載。java
[java] view plain copy服務器
- /**
- * 添加任務到任務隊列
- *
- * @param scheduleJob
- * @throws SchedulerException
- * @throws ClassNotFoundException
- */
- public ScheduleJob addJob(ScheduleJob job) throws SchedulerException, ClassNotFoundException {
- if (job == null || !ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) {
- return null;
- }
-
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
- log.debug(scheduler
- + ".......................................................................................add");
- TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
-
- CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
-
- // 不存在,建立一個
- if (null == trigger) {
- Class clazz = Class.forName(job.getBeanClass());
-
- JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(job.getJobName(), job.getJobGroup()).build();
-
- jobDetail.getJobDataMap().put("scheduleJob", job);
-
- CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
-
- trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())
- .withSchedule(scheduleBuilder).build();
-
- scheduler.scheduleJob(jobDetail, trigger);
- } else {
- // Trigger已存在,那麼更新相應的定時設置
- CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
-
- // 按新的cronExpression表達式從新構建trigger
- trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
-
- // 按新的trigger從新設置job執行
- scheduler.rescheduleJob(triggerKey, trigger);
- }
-
- // 獲得任務下一次的計劃執行時間
- Date nextProcessTime = trigger.getNextFireTime();
- job.setNextProcessTime(nextProcessTime);
- return job;
- }
-
- /**
- * 服務器啓動,加載job_status=1的任務到任務隊列中
- *
- * @throws Exception
- */
- @PostConstruct
- public void init() throws Exception {
-
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
-
- // 這裏獲取任務信息數據
- List<ScheduleJob> jobList = scheduleJobMapper.getAllJobs();
-
- for (ScheduleJob job : jobList) {
- addJob(job);
- }
- }
-
- /**
- * 獲取全部計劃中的任務列表
- *
- * @return
- * @throws SchedulerException
- */
- @Deprecated
- public List<ScheduleJob> getAllJob() throws SchedulerException {
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
- GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
- Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
- List<ScheduleJob> jobList = new ArrayList<ScheduleJob>();
- for (JobKey jobKey : jobKeys) {
- List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
- for (Trigger trigger : triggers) {
- ScheduleJob job = new ScheduleJob();
- job.setJobName(jobKey.getName());
- job.setJobGroup(jobKey.getGroup());
- job.setDescription("觸發器:" + trigger.getKey());
- Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
- job.setJobStatus(triggerState.name());
- if (trigger instanceof CronTrigger) {
- CronTrigger cronTrigger = (CronTrigger) trigger;
- String cronExpression = cronTrigger.getCronExpression();
- job.setCronExpression(cronExpression);
- }
- jobList.add(job);
- }
- }
- return jobList;
- }
-
- /**
- * 獲取全部正在運行的job
- *
- * @return
- * @throws SchedulerException
- */
- @Deprecated
- public List<ScheduleJob> getRunningJob() throws SchedulerException {
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
- List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
- List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(executingJobs.size());
- for (JobExecutionContext executingJob : executingJobs) {
- ScheduleJob job = new ScheduleJob();
- JobDetail jobDetail = executingJob.getJobDetail();
- JobKey jobKey = jobDetail.getKey();
- Trigger trigger = executingJob.getTrigger();
- job.setJobName(jobKey.getName());
- job.setJobGroup(jobKey.getGroup());
- job.setDescription("觸發器:" + trigger.getKey());
- Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
- job.setJobStatus(triggerState.name());
- if (trigger instanceof CronTrigger) {
- CronTrigger cronTrigger = (CronTrigger) trigger;
- String cronExpression = cronTrigger.getCronExpression();
- job.setCronExpression(cronExpression);
- }
- jobList.add(job);
- }
- return jobList;
- }
-
- /**
- * 暫停一個job
- *
- * @param scheduleJob
- * @throws SchedulerException
- */
- @Deprecated
- public void pauseJob(ScheduleJob scheduleJob) throws SchedulerException {
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
- JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- scheduler.pauseJob(jobKey);
- }
-
- /**
- * 恢復一個job
- *
- * @param scheduleJob
- * @throws SchedulerException
- */
- @Deprecated
- public void resumeJob(ScheduleJob scheduleJob) throws SchedulerException {
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
- JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- scheduler.resumeJob(jobKey);
- }
-
- /**
- * 刪除一個job
- *
- * @param scheduleJob
- * @throws SchedulerException
- */
- public void deleteJob(ScheduleJob scheduleJob) throws SchedulerException {
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
- JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- scheduler.deleteJob(jobKey);
- }
-
- /**
- * 當即執行job
- *
- * @param scheduleJob
- * @throws SchedulerException
- */
- public void runAJobNow(ScheduleJob scheduleJob) throws SchedulerException {
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
- JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
- scheduler.triggerJob(jobKey);
- }
-
- /**
- * 更新job時間表達式
- *
- * @param scheduleJob
- * @throws SchedulerException
- */
- public ScheduleJob updateJobCron(ScheduleJob scheduleJob) throws SchedulerException {
- Scheduler scheduler = schedulerFactoryBean.getScheduler();
-
- TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
-
- CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
-
- CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
-
- trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
-
- scheduler.rescheduleJob(triggerKey, trigger);
- // 獲得任務下一次的計劃執行時間
- Date nextProcessTime = trigger.getNextFireTime();
- scheduleJob.setNextProcessTime(nextProcessTime);
-
- return scheduleJob;
- }
[java] view plain copyapp
- Schdulejob實體類:
[java] view plain copyui
- <pre name="code" class="java">public class ScheduleJob {
-
- public static final String STATUS_RUNNING = "1";
- public static final String STATUS_NOT_RUNNING = "0";
-
- private Long jobId;
- /**
- * 任務建立時間
- */
- private Date createTime;
- /**
- * 任務更新時間
- */
- private Date updateTime;
- /**
- * 上一次執行時間
- */
- private Date lastProcessTime;
- /**
- * 下次計劃執行時間
- */
- private Date nextProcessTime;
- /**
- * 任務名稱
- */
- private String jobName;
- /**
- * 任務分組
- */
- private String jobGroup;
- /**
- * 任務狀態 是否啓動任務
- */
- private String jobStatus;
- /**
- * cron表達式
- */
- private String cronExpression;
- /**
- * 描述
- */
- private String description;
- /**
- * 任務執行時調用哪一個類的方法 包名+類名
- */
- private String beanClass;
-
- private List<JobParam> JobParamList;
-
- public Long getJobId() {
- return jobId;
- }
-
- public void setJobId(Long jobId) {
- this.jobId = jobId;
- }
-
- public Date getCreateTime() {
- return createTime;
- }
-
- public void setCreateTime(Date createTime) throws ParseException {
- this.createTime = createTime;
- }
-
- public Date getUpdateTime() {
- return updateTime;
- }
-
- public void setUpdateTime(Date updateTime) {
- this.updateTime = updateTime;
- }
-
- public String getJobName() {
- return jobName;
- }
-
- public void setJobName(String jobName) {
- this.jobName = jobName;
- }
-
- public String getJobGroup() {
- return jobGroup;
- }
-
- public void setJobGroup(String jobGroup) {
- this.jobGroup = jobGroup;
- }
-
- public String getJobStatus() {
- return jobStatus;
- }
-
- public void setJobStatus(String jobStatus) {
- this.jobStatus = jobStatus;
- }
-
- public String getCronExpression() {
- return cronExpression;
- }
-
- public void setCronExpression(String cronExpression) {
- this.cronExpression = cronExpression;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public String getBeanClass() {
- return beanClass;
- }
-
- public void setBeanClass(String beanClass) {
- this.beanClass = beanClass;
- }
-
- public List<JobParam> getJobParamList() {
- return JobParamList;
- }
-
- public void setJobParamList(List<JobParam> jobParamList) {
- JobParamList = jobParamList;
- }
-
- public Date getNextProcessTime() {
- return nextProcessTime;
- }
-
- public void setNextProcessTime(Date nextProcessTime) {
- this.nextProcessTime = nextProcessTime;
- }
-
- public Date getLastProcessTime() {
- return lastProcessTime;
- }
-
- public void setLastProcessTime(Date lastProcessTime) {
- this.lastProcessTime = lastProcessTime;
- }
-
- }