在項目中需要手動啟停某些服務,那么需要有一個控制這些任務的類。由於任務是有Quartz控制的,我們只需要通過Quartz的相關的API實現相關的功能即可。
- package com.easyway.app.quartz.mgr;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import org.quartz.JobDataMap;
- import org.quartz.JobDetail;
- import org.quartz.JobKey;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerException;
- import org.quartz.SchedulerFactory;
- import org.quartz.Trigger;
- import org.quartz.TriggerKey;
- import org.quartz.impl.StdSchedulerFactory;
- import org.quartz.impl.matchers.GroupMatcher;
- /**
- * 一個簡單的quartz任務管理器
- * @author longgangbai
- *
- */
- public class QuartzScheduleMgr {
- private static Scheduler scheduler=getScheduler();
- /**
- * 創建一個調度對象
- * @return
- * @throws SchedulerException
- */
- private static Scheduler getScheduler() {
- SchedulerFactory sf = new StdSchedulerFactory();
- Scheduler scheduler=null;
- try {
- scheduler = sf.getScheduler();
- } catch (SchedulerException e) {
- e.printStackTrace();
- }
- return scheduler;
- }
- public static Scheduler getInstanceScheduler(){
- return scheduler;
- }
- /**
- * 啟動一個調度對象
- * @throws SchedulerException
- */
- public void start() throws SchedulerException
- {
- scheduler.start();
- }
- /**
- * 檢查調度是否啟動
- * @return
- * @throws SchedulerException
- */
- public boolean isStarted() throws SchedulerException
- {
- return scheduler.isStarted();
- }
- /**
- * 關閉調度信息
- * @throws SchedulerException
- */
- public void shutdown() throws SchedulerException {
- scheduler.shutdown();
- }
- /**
- * 添加調度的job信息
- * @param jobdetail
- * @param trigger
- * @return
- * @throws SchedulerException
- */
- public Date scheduleJob(JobDetail jobdetail, Trigger trigger)
- throws SchedulerException{
- return scheduler.scheduleJob(jobdetail, trigger);
- }
- /**
- * 添加相關的觸發器
- * @param trigger
- * @return
- * @throws SchedulerException
- */
- public Date scheduleJob(Trigger trigger) throws SchedulerException{
- return scheduler.scheduleJob(trigger);
- }
- /**
- * 添加多個job任務
- * @param triggersAndJobs
- * @param replace
- * @throws SchedulerException
- */
- public void scheduleJobs(Map<JobDetail, List<Trigger>> triggersAndJobs, boolean replace) throws SchedulerException
- {
- scheduler.scheduleJobs(triggersAndJobs, replace);
- }
- /**
- * 停止調度Job任務
- * @param triggerkey
- * @return
- * @throws SchedulerException
- */
- public boolean unscheduleJob(TriggerKey triggerkey)
- throws SchedulerException{
- return scheduler.unscheduleJob(triggerkey);
- }
- /**
- * 停止調度多個觸發器相關的job
- * @param list
- * @return
- * @throws SchedulerException
- */
- public boolean unscheduleJobs(List<TriggerKey> triggerKeylist) throws SchedulerException{
- return scheduler.unscheduleJobs(triggerKeylist);
- }
- /**
- * 重新恢復觸發器相關的job任務
- * @param triggerkey
- * @param trigger
- * @return
- * @throws SchedulerException
- */
- public Date rescheduleJob(TriggerKey triggerkey, Trigger trigger)
- throws SchedulerException{
- return scheduler.rescheduleJob(triggerkey, trigger);
- }
- /**
- * 添加相關的job任務
- * @param jobdetail
- * @param flag
- * @throws SchedulerException
- */
- public void addJob(JobDetail jobdetail, boolean flag)
- throws SchedulerException {
- scheduler.addJob(jobdetail, flag);
- }
- /**
- * 刪除相關的job任務
- * @param jobkey
- * @return
- * @throws SchedulerException
- */
- public boolean deleteJob(JobKey jobkey) throws SchedulerException{
- return scheduler.deleteJob(jobkey);
- }
- /**
- * 刪除相關的多個job任務
- * @param jobKeys
- * @return
- * @throws SchedulerException
- */
- public boolean deleteJobs(List<JobKey> jobKeys)
- throws SchedulerException{
- return scheduler.deleteJobs(jobKeys);
- }
- /**
- *
- * @param jobkey
- * @throws SchedulerException
- */
- public void triggerJob(JobKey jobkey) throws SchedulerException {
- scheduler.triggerJob(jobkey);
- }
- /**
- *
- * @param jobkey
- * @param jobdatamap
- * @throws SchedulerException
- */
- public void triggerJob(JobKey jobkey, JobDataMap jobdatamap)
- throws SchedulerException {
- scheduler.triggerJob(jobkey, jobdatamap);
- }
- /**
- * 停止一個job任務
- * @param jobkey
- * @throws SchedulerException
- */
- public void pauseJob(JobKey jobkey) throws SchedulerException {
- scheduler.pauseJob(jobkey);
- }
- /**
- * 停止多個job任務
- * @param groupmatcher
- * @throws SchedulerException
- */
- public void pauseJobs(GroupMatcher<JobKey> groupmatcher)
- throws SchedulerException {
- scheduler.pauseJobs(groupmatcher);
- }
- /**
- * 停止使用相關的觸發器
- * @param triggerkey
- * @throws SchedulerException
- */
- public void pauseTrigger(TriggerKey triggerkey)
- throws SchedulerException {
- scheduler.pauseTrigger(triggerkey);
- }
- public void pauseTriggers(GroupMatcher<TriggerKey> groupmatcher)
- throws SchedulerException {
- scheduler.pauseTriggers(groupmatcher);
- }
- /**
- * 恢復相關的job任務
- * @param jobkey
- * @throws SchedulerException
- */
- public void resumeJob(JobKey jobkey) throws SchedulerException {
- scheduler.pauseJob(jobkey);
- }
- public void resumeJobs(GroupMatcher<JobKey> matcher)
- throws SchedulerException {
- scheduler.resumeJobs(matcher);
- }
- public void resumeTrigger(TriggerKey triggerkey)
- throws SchedulerException {
- scheduler.resumeTrigger(triggerkey);
- }
- public void resumeTriggers(GroupMatcher<TriggerKey> groupmatcher)
- throws SchedulerException
- {
- scheduler.resumeTriggers(groupmatcher);
- }
- /**
- * 暫停調度中所有的job任務
- * @throws SchedulerException
- */
- public void pauseAll() throws SchedulerException
- {
- scheduler.pauseAll();
- }
- /**
- * 恢復調度中所有的job的任務
- * @throws SchedulerException
- */
- public void resumeAll() throws SchedulerException
- {
- scheduler.resumeAll();
- }
- }
創建一個Job任務:
- /*
- * Copyright 2005 - 2009 Terracotta, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy
- * of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- */
- package com.easyway.app.quartz.mgr;
- import java.util.Date;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- /**
- * 一個簡單的quartz調用job
- * @author longgangbai
- *
- */
- public class HelloJob implements Job {
- private static Logger _log = LoggerFactory.getLogger(HelloJob.class);
- public HelloJob() {
- }
- public void execute(JobExecutionContext context)
- throws JobExecutionException {
- _log.info("Hello World! - " + new Date());
- }
- }
創建觸發器和調用相關的Job
- /*
- * Copyright 2005 - 2009 Terracotta, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy
- * of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- */
- package com.easyway.app.quartz.mgr;
- import static org.quartz.DateBuilder.evenMinuteDate;
- import static org.quartz.JobBuilder.newJob;
- import static org.quartz.TriggerBuilder.newTrigger;
- import java.util.Date;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.Trigger;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * 一個簡單的測試quartz任務管理器測試類
- * @author longgangbai
- *
- */
- public class QuartzScheduleMain {
- /**
- *
- * @throws Exception
- */
- public void run() throws Exception {
- Logger log = LoggerFactory.getLogger(QuartzScheduleMain.class);
- log.info("------- Initializing ----------------------");
- // First we must get a reference to a scheduler
- //從調度管理器中獲取調度對象
- Scheduler sched = QuartzScheduleMgr.getInstanceScheduler();
- log.info("------- Initialization Complete -----------");
- // computer a time that is on the next round minute
- Date runTime = evenMinuteDate(new Date());
- log.info("------- Scheduling Job -------------------");
- // define the job and tie it to our HelloJob class
- //創建相關的job信息
- JobDetail job = newJob(HelloJob.class)
- .withIdentity("job1", "group1")
- .build();
- // Trigger the job to run on the next round minute
- //創建一個觸發器的名稱
- Trigger trigger = newTrigger()
- .withIdentity("trigger1", "group1")
- .startAt(runTime)
- .build();
- // Tell quartz to schedule the job using our trigger
- //設置調度相關的Job
- sched.scheduleJob(job, trigger);
- log.info(job.getKey() + " will run at: " + runTime);
- // Start up the scheduler (nothing can actually run until the
- // scheduler has been started)
- //啟動調度任務
- sched.start();
- log.info("------- Started Scheduler -----------------");
- try {
- Thread.sleep(25L * 1000L);
- // executing...
- } catch (Exception e) {
- }
- //暫時停止Job任務開始執行
- log.info("-------pauseJob.. -------------");
- sched.pauseJob(job.getKey());
- try {
- Thread.sleep(10L * 1000L);
- } catch (Exception e) {
- }
- log.info("------- resumeJob... -------------");
- //恢復Job任務開始執行
- sched.resumeJob(job.getKey());
- try {
- Thread.sleep(10L * 1000L);
- // executing...
- } catch (Exception e) {
- }
- // wait long enough so that the scheduler as an opportunity to
- // run the job!
- log.info("------- Waiting 65 seconds... -------------");
- try {
- // wait 65 seconds to show job
- Thread.sleep(65L * 1000L);
- // executing...
- } catch (Exception e) {
- }
- // shut down the scheduler
- log.info("------- Shutting Down ---------------------");
- sched.shutdown(true);
- log.info("------- Shutdown Complete -----------------");
- }
- public static void main(String[] args) throws Exception {
- QuartzScheduleMain example = new QuartzScheduleMain();
- example.run();
- }
- }