标签:
近期公司开发的数据交换系统嵌入了quartz任务调度功能,大概了解了任务调度的整个流程,项目中需要用到它来进行定时任务操作,对数据定时检查以及及时交换。
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.2.1。
任务调度使用:
public class InitServlet extends HttpServlet {
@Override
public 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")
@Transactional
public 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;
}
}
标签:
原文地址:http://www.cnblogs.com/HEWU10/p/quartz.html