近期公司開發(fā)的數據交換系統(tǒng)嵌入了quartz任務調度功能,大概了解了任務調度的整個流程,項目中需要用到它來進行定時任務操作,對數據定時檢查以及及時交換。
Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,它可以與J2EE與J2SE應用程序相結合也可以單獨使用。Quartz可以用來創(chuàng)建簡單或為運行十個,百個,甚至是好幾萬個Jobs這樣復雜的程序。Jobs可以做成標準的java組件或 EJBs。Quartz的最新版本為Quartz 2.2.1。
任務調度使用:
public class InitServlet extends HttpServlet { @Overridepublic void init() {this.ChangeInfoTask();} /*** * 在線監(jiān)控定時監(jiān)控時間配置 */public void ChangeLogTask(){ /// 創(chuàng)建一個簡單的觸發(fā)器 CronTrigger cronTrigger = new CronTrigger("ChangeLogTask", "ChangeLogTask"); try { // 設置觸發(fā)時間 cronTrigger.setCronExPRession(new CronExpression(Configuration.getConfiguration(Constants.ChangeLogTime))); // 定義一個具體作業(yè)ChangeLogTask,并加入ChangeLogTask組,并且綁定到具體的作業(yè)類ChangeLogJob類上 JobDetail jobDetail = new JobDetail("ChangeLogTask", "ChangeLogTask",ChangeLogJob.class); // 首先創(chuàng)建一個調度程序工廠 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); //從工廠獲取一個調度程序實例 Scheduler scheduler = schedulerFactory.getScheduler(); // 設置調度的具體作業(yè)和相關的觸發(fā)器 scheduler.scheduleJob(jobDetail, cronTrigger); //啟動調度程序 scheduler.start();} catch (Exception e) {logger.error("配置ChangeLog查詢時間失敗", e);}}}在你的Job接口實現類里面,添加一些邏輯到execute()方法。一旦你配置好Job實現類并設定好調度時間表,Quartz將密切注意剩余時間。當調度程序確定該是通知你的作業(yè)的時候,Quartz框架將調用你Job實現類(作業(yè)類)上的execute()方法并允許做它該做的事情。無需報告任何東西給調度器或調用任何特定的東西。僅僅執(zhí)行任務和結束任務即可。如果配置你的作業(yè)在隨后再次被調用,Quartz框架將在恰當的時間再次調用它。
package com.appdept.task;import java.util.Date;import java.util.List;import java.util.UUID;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.appdept.common.constant.ServiceVariable;import com.appdept.entity.ServerInfo;import com.appdept.entity.exchange.ChangeLog;import com.appdept.entity.vo.ErrorMsg;import com.appdept.entity.vo.ServiceToActionMsg;import com.appdept.service.iface.ChangeLogService;import com.appdept.service.iface.ServerInfoService;import com.appdept.util.Configuration;import com.appdept.util.HttpClientUtil;import com.appdept.util.SpringContextUtil;@Service("ChangeLogService")@Transactionalpublic class ChangeLogJob implements Job{private ChangeLogService service;private ServerInfoService service2;private String changeLogConfigure =Configuration.getConfiguration("ChangeLogConfigure");public void execute(JobExecutionContext arg0) throws JobExecutionException {this.service =(ChangeLogService) SpringContextUtil.getBean(ServiceVariable.ChangeLogService);this.service2=(ServerInfoService) SpringContextUtil.getBean(ServiceVariable.ServerInfoService);this.allInformation();}public void addOnlineMonitoringInformation(String code,String http){ChangeLog changeLog=new ChangeLog();ServiceToActionMsg<ChangeLog> serviceToActionMsg = null;try {changeLog.setGuid(UUID.randomUUID().toString());changeLog.setXzqdm(code);changeLog.setJcsj(new Date());changeLog.setYxzt(this.runningState(http));changeLog.setBz("備用字段");serviceToActionMsg =service.addChangeLog(changeLog);}catch (Exception e) {e.printStackTrace();new ErrorMsg("在線監(jiān)控信息存儲失敗!", e);}}public int runningState(String http){HttpClientUtil util = new HttpClientUtil();try {Integer t=util.getRequest(http);if(t==200){return 0;}} catch (Exception e) {e.printStackTrace();new ErrorMsg("鏈路檢測失敗!", e);return 1;}return 1;}public void allInformation() {try {String[] list = changeLogConfigure.split(";");ServiceToActionMsg<ServerInfo> serviceToActionMsg = null;ServerInfo serverInfo=new ServerInfo();for(String info:list){serverInfo.setCityCode(info);serviceToActionMsg = service2.findServerInfo(serverInfo);this.addOnlineMonitoringInformation(info, serviceToActionMsg.getDataList().get(0).getTargetUrl());}} catch (Exception e) {e.printStackTrace();new ErrorMsg("查詢所有行政區(qū)存儲失敗!", e);}}public String getChangeLogConfigure() {return changeLogConfigure;}public void setChangeLogConfigure(String changeLogConfigure) {this.changeLogConfigure = changeLogConfigure;}}
新聞熱點
疑難解答