时间:2021-07-01 10:21:17 帮助过:6人阅读
1.模块功能: 1.对web服务器进行ping操作,如果ping不通,则上报web服务器无法ping通. 如果可以ping通,继续测试web服务是否可以访问,如果不能访问则上报web服务无法访问故障. 2.对数据库服务器进行ping操作,如果ping不通,则上报数据库服务器无法ping通. 如果可
1.模块功能:同时注意用dom4j解析xml文件,必须依赖于jaxen类库.
package com.ilucky.malf.rule;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.ilucky.malf.rule.BaseRule;
/**
* @author IluckySi
* @date 20140727
*/
public class RuleBuilder {
private static RuleBuilder instance;
public static final String ID = "id";
public static final String NAME = "name";
public static final String TYPE = "type";
public static final String PROPERTY = "property";
private Map map = new HashMap();
public static RuleBuilder getInstance() {
if(instance == null) {
instance = new RuleBuilder();
}
return instance;
}
@SuppressWarnings("unchecked")
private RuleBuilder() {
String malfRulePath = "/com/ilucky/malf/malf_rule.xml";
InputStream is = null;
Document document = null;
try {
is = this.getClass().getResourceAsStream(malfRulePath);
SAXReader saxReader = new SAXReader();
document = saxReader.read(is);
Element root = document.getRootElement();
List rootChildren = root.elements();
for(int i = 0; rootChildren != null && i < rootChildren.size(); i++) {
Element rootChild = rootChildren.get(i);
String id = rootChild.attributeValue(ID);
map.put(id, build(rootChild));
}
} catch (DocumentException e) {
System.out.println("解析故障规则配置文件失败: " + e.toString());
}finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
System.out.println("关闭流发生问题!");
}
}
}
@SuppressWarnings("unchecked")
private BaseRule build(Element element) {
BaseRule baseRule = null;
try {
String ruleId = element.attributeValue(ID);
String ruleName = element.attributeValue(NAME);
String ruleType = element.attributeValue(TYPE);
baseRule = (BaseRule)Class.forName(ruleType).newInstance();
baseRule.setId(ruleId);
baseRule.setName(ruleName);
List propertyElements = element.selectNodes(PROPERTY);
for(int i = 0; propertyElements != null && i < propertyElements.size(); i++) {
Element propertyElement = propertyElements.get(i);
String propertyName = propertyElement.attributeValue(NAME);
List ruleElements = propertyElement.elements();
if(!ruleElements.isEmpty()){
Element ruleElement = ruleElements.get(0);
BeanUtils.setProperty(baseRule, propertyName, build(ruleElement));
}
}
} catch (Exception e) {
System.out.println("解析故障规则配置文件失败: " + e.toString());
}
return baseRule;
}
public BaseRule build(String id) {
return map.get(id);
}
}
3.通过使用quartz任务调度器创建简单定时任务.
package com.ilucky.malf.quartz;
import java.text.ParseException;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
/**
* @author IluckySi
* @date 20140727
*/
public class MalfQuartz {
private int cycle = 30;
private static final String TRIGGER_NAME= "triggerName";
private static final String TRIGGER_GROUP_NAME = "triggerGroupName";
private static final String JOB_NAME = "jobName";
private static final String JOB_GROUP_NAME = "jobGroupName";
public void start(){
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler;
try {
scheduler = schedulerFactory.getScheduler();
scheduler.scheduleJob(createJobDetail(), createTrigger(cycle));
scheduler.start();
System.out.println("故障检测定时任务启动成功!");
} catch (SchedulerException e) {
System.out.println("故障检测定时任务启动失败: "+e.getMessage());
}
}
private Trigger createTrigger(int cycle) {
String cron = "0/" + cycle + " * * * * ?";
CronTrigger trigger = new CronTrigger(TRIGGER_NAME, TRIGGER_GROUP_NAME);
try {
trigger.setCronExpression(cron);
} catch (ParseException e) {
e.printStackTrace();
}
return trigger;
}
private JobDetail createJobDetail() {
return new JobDetail(JOB_NAME, JOB_GROUP_NAME, MalfJob.class);
}
}
4.测试数据库是否可以正常连接. 1.故障规则配置文件支持多层配置,即多层嵌套,直接看配置文件:
2.条件之间可以建立多种关系(与,或,非,异或等).3.如果要加其他故障测试,只需要修改一下配置文件,然后写工具类,无需修改原有代码.
最后看测试类:
package com.ilucky.malf;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ilucky.malf.quartz.MalfQuartz;
/**
* @author IluckySi
* @date 20140727
*/
public class MainTest {
public static Map>> datasource= new HashMap>>();
public static void main(String[] arsg) {
//模拟web测试数据.
List
只把一部分代码贴出来了,看如下项目目录: