Access to the application is normal, real-time monitoring of the design and implementation (Java implementation)

Recently a need to make the application access is normal, one day to do a simple web monitoring system, what specific analysis for everyone.

First, the database design

TABLE `table_monitor` the CREATE (
  ` id` BIGINT (20 is) the AUTO_INCREMENT the NOT NULL,
  `msql` VARCHAR (255) the DEFAULT NULL,
  ` mcompare` VARCHAR (50) the DEFAULT NULL,
  `mnum` decimal (10,2) the DEFAULT NULL,
  ` VARCHAR mdes` (50) the DEFAULT NULL,
  `type` int (20 is) the DEFAULT '. 1' the COMMENT '= SQL monitor. 1, 2 = monitoring URLs',
  a PRIMARY KEY (` id`)
) ENGINE = MyISAM the AUTO_INCREMENT = 54 is the DEFAULT the CHARSET = UTF8;
 
the CREATE TABLE table_monitor_result` `(
  ` id` BIGINT (20 is) the AUTO_INCREMENT the NOT NULL,
  `createTime` the COMMENT datetime the DEFAULT NULL 'processing time',
  ` mSQL` VARCHAR (255) the COMMENT the DEFAULT NULL 'monitoring SQL or address',
  `mResult `text DEFAULT NULL COMMENT 'monitoring results',
  ` doResult` VARCHAR (50) NULL the DEFAULT the COMMENT "processing result",
  `doMsg` text DEFAULT NULL COMMENT 'treatment Notes',
  a PRIMARY KEY (` id`)
) ENGINE = MyISAM the DEFAULT the CHARSET = UTF8;
 


Second, the application access monitoring and control system

       /*访问地址监控*/
    public void  monitorURL() throws  Exception{
        List<PageData> list=this.query(Common.MONITOR_TYPE_URL);
        int size=StringUtil.getListSize(list);
        for (int i = 0; i < size; i++) {
            PageData pd=list.get(i);
            String url=pd.getString("msql");
            String cont=pd.getString("mdes");
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httpPost);
            try {
                if (response.getStatusLine().getStatusCode() == 200) {
                    //如果状态正常,什么操作都不做
                the else {}
                    // if not normal to send text messages
                    this.sendMsg (CONT);
                }
            } the catch (Exception E) {
                this.sendMsg (CONT);
                e.printStackTrace ();
            }
        }
    }


Third, to achieve regular monitoring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:amq="http://activemq.apache.org/schema/core" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
         http://activemq.apache.org/schema/core 
         http://activemq.apache.org/schema/core/activemq-core.xsd">  
        <!-- 添加调度的任务bean 配置对应的class-->
        <bean id="tableQuartz" class="com.zrsc.table.common.TableQuartz"></bean>

        <bean id="monitorURLJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="tableQuartz"></property>
            <property name="targetMethod" value="monitorURL"></property><!-- 配置调度指定类中的指定的方法 -->
            <property name="concurrent" value="false"></property>
        </bean>

        <bean id="monitorURLTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="monitorURLJob" />
        <property name="cronExpression" value="0 0 */1 * * ?" />
        </bean>

             <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                <property name="triggers">
                    <list>
                        <ref bean="monitorURLTrigger"/>
                    </list>
                </property>
                <property name="autoStartup" value="true"/>
             </bean>
    </beans>

Source download

Guess you like

Origin blog.csdn.net/jlq_diligence/article/details/90255929