1. module functions: 1. ping the web server. If the ping fails, the web server cannot be pinged. if you can ping the service, continue to test whether the web service can be accessed. If not, report that the web service cannot be accessed. 2. ping the database server. If the ping fails, the database server cannot be pinged. if yes
1. module functions: 1. ping the web server. If the ping fails, the web server cannot be pinged. if you can ping the service, continue to test whether the web service can be accessed. If not, report that the web service cannot be accessed. 2. ping the database server. If the ping fails, the database server cannot be pinged. if yes
1. module functions:
1. ping the web server. If the ping fails, report "The web server cannot be pinged ".
If you can ping the service, continue to test whether the web service can be accessed. If not, report "The web service cannot be accessed ".
2. ping the database server. If the ping fails, report "The database server cannot be pinged ".
If you can ping the service, continue to test whether the web service can be accessed. If not, Report "database connection failure ".
2. Main Technical points:
1. Solve the Problem of multi-thread (and each thread has its own variable) concurrency by using threadlocal thread variables.
2. Use dom4j to parse xml files to make the program more extensible (Multi-Layer Support, Convention is better than configuration: A property must have a rule ).
At the same time, pay attention to using dom4j to parse xml files, which must depend on the jaxen class library.
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. extends entexception; 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 (incluentexception e) {System. out. println ("failed to parse the fault rule configuration file:" + e. toString ();} finally {try {if (is! = Null) {is. close () ;}catch (IOException e) {System. out. println ("an error occurred when closing the stream! ") ;}}@ 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 ("failed to parse the fault rule configuration file:" + e. toString ();} return baseRule;} public BaseRule build (String id) {return map. get (id );}}
3. Use the quartz Task Scheduler to create a simple scheduled task.
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 schedfactory; try {scheduler = schedulerFactory. getScheduler (); scheduler. scheduleJob (createJobDetail (), CreateTrigger (cycle); scheduler. start (); System. out. println ("the fault detection task is started successfully! ");} Catch (SchedulerException e) {System. out. println ("failed to start the fault detection scheduled task:" + 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. test whether the database can be connected normally.
5. test whether the web service can be accessed normally.
6. Obtain the WEB-INF path in the java file.
7. Use BeanUtils's setProerpty method to ensure that bean provides the set Method for this property.
8. Check whether the web service can access the common-codec library in addition to the commons-httpclient class library. Otherwise, execute
HttpMethod method = new GetMethod (url) command will be stuck here.
9. There is a ProcessMonitor thread in pingUtil. The purpose of this thread is to ping the host thread for a long time,
So the maximum wait time is 10 seconds. After 10 seconds, the ProcessMonitor thread will kill it.
10. In addition, it is not necessary to test whether the host can be pinged by analyzing the returned results.
3. Function Expansion.
1. The fault rule configuration file supports multi-layer configuration, that is, multi-layer nesting. You can view the configuration file directly:
2. Multiple relationships (and, Or, non, exclusive, or, etc.) can be established between conditions ).
3. To add other fault tests, you only need to modify the configuration file and write the tool class without modifying the original code.
Finally, let's look at the test class:
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) {// simulate web test data. List
> Webs = new ArrayList
> (); Map
Map1 = new HashMap
(); Map1.put ("type", "web Service"); map1.put ("ip", "192.168.72.149"); map1.put ("url", "http: // 192.168.72.149: 8080 "); webs. add (map1); Map
Map2 = new HashMap
(); Map2.put ("type", "web Service"); map2.put ("ip", "192.168.72.153"); map2.put ("url", "http: // 192.168.72.153: 8080 "); webs. add (map2); Map
Map3 = new HashMap
(); Map3.put ("type", "web Service"); map3.put ("ip", "192.168.72.253"); map3.put ("url", "http: // 192.168.72.253: 8080 "); webs. add (map3); datasource. put ("malf_web", webs); // simulates the database test data. list
> Databases = new ArrayList
> (); Map
Map4 = new HashMap
(); Map4.put ("type", "Database Service"); map4.put ("ip", "192.168.73.236"); map4.put ("username", "system "); map4.put ("password", "talent"); map4.put ("url", "jdbc: oracle: thin: @ 192.168.73.236: 1521: k3"); map4.put ("SQL ", "select 1 from dual"); map4.put ("driver", "oracle. jdbc. driver. oracleDriver "); databases. add (map4); Map
Map5 = new HashMap
(); Map5.put ("type", "Database Service"); map5.put ("ip", "192.168.67.130"); map5.put ("username", "k3 "); map5.put ("password", "k3"); map5.put ("url", "jdbc: oracle: thin: @ 192.168.67.130: 1521: kongsan"); map5.put ("SQL ", "select 1 from dual"); map5.put ("driver", "oracle. jdbc. driver. oracleDriver "); databases. add (map5); Map
Map6 = new HashMap
(); Map6.put ("type", "Database Service"); map6.put ("ip", "192.168.72.153"); map6.put ("username", "core "); map6.put ("password", "core"); map6.put ("url", "jdbc: oracle: thin: @ 192.168.72.153: 1521: core"); map6.put ("SQL ", "select 1 from dual"); map6.put ("driver", "oracle. jdbc. driver. oracleDriver "); databases. add (map6); datasource. put ("malf_database", databases); // create a Task Scheduler: Detect the task once every 30 seconds. new MalfQuartz (). start ();}}
Only part of the code is posted. See the following project directory: