quartz任務調度初次使用記錄

來源:互聯網
上載者:User

標籤:

  近期公司開發的資料交換系統嵌入了quartz任務調度功能,大概瞭解了任務調度的整個流程,項目中需要用到它來進行定時任務操作,對資料定時檢查以及及時交換。

  Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,它可以與J2EE與J2SE應用程式相結合也可以單獨使用。Quartz可以用來建立簡單或為運行十個,百個,甚至是好幾萬個Jobs這樣複雜的程式。Jobs可以做成標準的Java組件或 EJBs。Quartz的最新版本為Quartz 2.2.1。

  任務調度使用:

public class InitServlet extends HttpServlet {        @Overridepublic void init() {this.ChangeInfoTask();}            /*** * 線上監控定時監控時間配置 */public void ChangeLogTask(){    /// 建立一個簡單的觸發器     CronTrigger cronTrigger = new CronTrigger("ChangeLogTask", "ChangeLogTask");    try {
              // 設定觸發時間  cronTrigger.setCronExpression(new CronExpression(Configuration.getConfiguration(Constants.ChangeLogTime)));
              // 定義一個具體作業ChangeLogTask,並加入ChangeLogTask組,並且綁定到具體的作業類ChangeLogJob類上  JobDetail jobDetail = new JobDetail("ChangeLogTask", "ChangeLogTask",ChangeLogJob.class);
               // 首先建立一個發送器工廠       SchedulerFactory schedulerFactory = new StdSchedulerFactory();
              //從工廠擷取一個發送器執行個體  Scheduler scheduler = schedulerFactory.getScheduler();
              // 設定調度的具體作業和相關的觸發器 scheduler.scheduleJob(jobDetail, cronTrigger);
              //啟動發送器 scheduler.start();} catch (Exception e) {logger.error("配置ChangeLog查詢時間失敗", e);}}}

 

  在你的Job介面實作類別裡面,添加一些邏輯到execute()方法。一旦你配置好Job實作類別並設定好調度時間表,Quartz將密切注意剩餘時間。當發送器確定該是通知你的作業的時候,Quartz架構將調用你Job實作類別(作業類)上的execute()方法並允許做它該做的事情。無需報告任何東西給調度器或調用任何特定的東西。僅僅執行任務和結束任務即可。如果配置你的作業在隨後再次被調用,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("線上監控資訊儲存失敗!", 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("查詢所有行政區儲存失敗!", e);}}public String getChangeLogConfigure() {return changeLogConfigure;}public void setChangeLogConfigure(String changeLogConfigure) {this.changeLogConfigure = changeLogConfigure;}}

  

  

quartz任務調度初次使用記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.