对数据库中的订单状态和订单金额,进行实时监控的设计和实现(Java实现)

最近需要做一个定时监控数据库同步结果,用一天时间简单做了一个web监控系统,具体一下,分析给大家。

一、数据库设计

 CREATE TABLE `table_monitor` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `msql` varchar(255) DEFAULT NULL,
  `mcompare` varchar(50) DEFAULT NULL,
  `mnum` decimal(10,2) DEFAULT NULL,
  `mdes` varchar(50) DEFAULT NULL,
  `type` int(20) DEFAULT '1' COMMENT '1=sql监控,2=网址监控',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=54 DEFAULT CHARSET=utf8;
 
CREATE TABLE `table_monitor_result` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `createTime` datetime DEFAULT NULL COMMENT '处理时间',
  `mSQL` varchar(255) DEFAULT NULL COMMENT '监控SQL或地址',
  `mResult` text DEFAULT NULL COMMENT '监控结果',
  `doResult` varchar(50) DEFAULT NULL COMMENT '处理结果',
  `doMsg` text DEFAULT NULL COMMENT '处理备注',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


二、监控数据比对实现

     //检测ERP数据库中的订单状态
    public void monitorOrderStatus() {
        long startTime=System.currentTimeMillis();
        try {
            String COM_EQUAL = "=";
            String COM_GREATER_THAN = ">";
            String COM_LESS_THAN = "<";
            List<PageData> list = this.query(Common.MONITOR_TYPE_ORDERSTATUS);
            int size = StringUtil.getListSize(list);
            for (int i = 0; i < size; i++) {
                //获取监控SQL
                PageData pd = list.get(i);
                String id = (String) pd.get("mid");
                String msql = pd.getString("msql");
                String mcompare = pd.getString("mcompare");
                BigDecimal mnum = (BigDecimal) pd.get("mnum");
                String mdes = pd.getString("mdes");
                mdes=id+mdes;
                //检查
                PageData queryResultPD = this.executeERPSelectSQL(msql);
                Double dbnum = Double.parseDouble(queryResultPD.get("count") + "");
                Double expectnum = mnum.doubleValue();
                //有问题的发送短信
                if (COM_EQUAL.equals(mcompare) ? Math.abs(dbnum - expectnum) > 0.01 : false) {
                    this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
                } else if (COM_GREATER_THAN.equals(mcompare) ? getScale(dbnum - expectnum) < 0.00 : false) {
                    this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
                } else if (COM_LESS_THAN.equals(mcompare) ? getScale(dbnum - expectnum) > 0.00 : false) {
                    this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
                }
            }
        }catch (Exception e){
            this.monitorFailure("", Tools.getExceptionAllinformation(e),false);
            this.sendMsg("订单状态数据检测失败,失败原因"+e.getMessage());
        }
        if(Common.NOTE_MSG) {
            this.sendMsg("订单状态数据检测完毕,总用时"+(System.currentTimeMillis()-startTime)+"毫秒");
        }
    }

    //检测ERP数据库中的订单金额
    public void monitorOrderMoney() {
        long startTime=System.currentTimeMillis();
        try {
            String COM_EQUAL = "=";
            String COM_GREATER_THAN = ">";
            String COM_LESS_THAN = "<";
            List<PageData> list = this.query(Common.MONITOR_TYPE_ORDERMONEY);
            int size = StringUtil.getListSize(list);
            for (int i = 0; i < size; i++) {
                //获取监控SQL
                PageData pd = list.get(i);
                String id = (String) pd.get("mid");
                String msql = pd.getString("msql");
                String mcompare = pd.getString("mcompare");
                BigDecimal mnum = (BigDecimal) pd.get("mnum");
                String mdes = pd.getString("mdes");
                mdes=id+mdes;
                //检查
                PageData queryResultPD = this.executeERPSelectSQL(msql);
                Double dbnum = Double.parseDouble(queryResultPD.get("count") + "");
                Double expectnum = mnum.doubleValue();
                //有问题的发送短信
                if (COM_EQUAL.equals(mcompare) ? Math.abs(dbnum - expectnum) > 0.01 : false) {
                    this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
                } else if (COM_GREATER_THAN.equals(mcompare) ? getScale(dbnum - expectnum) < 0.00 : false) {
                    this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
                } else if (COM_LESS_THAN.equals(mcompare) ? getScale(dbnum - expectnum) > 0.00 : false) {
                    this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
                }
            }
        }catch (Exception e){
            this.monitorFailure("", Tools.getExceptionAllinformation(e),false);
            this.sendMsg("订单金额数据检测失败,失败原因"+e.getMessage());
        }
    }


三、定时监控实现

<?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="monitorOrderStatusAndMoneyJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="tableQuartz"></property>
            <property name="targetMethod" value="monitorOrderStatusAndMoney"></property><!-- 配置调度指定类中的指定的方法 -->
            <property name="concurrent" value="false"></property>
        </bean>

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

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

源码下载

猜你喜欢

转载自blog.csdn.net/jlq_diligence/article/details/90255911
今日推荐