CheckConnectionDao.java 3.13 KB
package com.sitech.ismp.check.downtime;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.sitech.util.SqlMapFactory;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author frank  zmm@honggroup.com.cn
 * @Description:  检查主机,数据库,中间件的连接状态dao.
 * @Package com.sitech.ismp.check.downtime
 * @ClassName: com.sitech.ismp.check.downtime.CheckConnectionDao
 * @date 2017年04月22日 15:05
 */
public class CheckConnectionDao {

    protected static SqlMapClient sqlmapClient;
    protected static SqlMapClient oracleSqlMapClient ;

    /**
     * 因为继承 basedao 后,报basedao 找不到,因此写到方法里面。
     */
    static {
        sqlmapClient = SqlMapFactory.getDefaultSqlMapClient();
        oracleSqlMapClient= SqlMapFactory.getOracleSqlMapClient("oracle_sqlmap.xml");
    }

    private static Logger log=Logger.getLogger(CheckConnectionDao.class);



    /**
     * 插入ping结果。在插入数据之前,删除掉当前时间前30分钟的数据,即只保留30分钟的数据。
     * @param bean
     */
    public void addPingConnectionState(ConnectionStateBean bean){
        try {
            deleteBeforeData();
            sqlmapClient.insert("addPingConnectionState", bean);
        } catch (SQLException e) {
            log.error("Exception while addPingConnectionState.",e);
        }

    }


    /**
     * 清除TB_CONNECTION_STATE表里30分钟前的数据。
     */
    public void deleteBeforeData(){
        long temp=new Date().getTime()-1000*60*30;
        String sql="DELETE from TB_CONNECTION_STATE where CREATE_DATE < "+temp;
        try {
            sqlmapClient.delete("deleteBeforeData", sql);
        } catch (Exception e) {
            log.error("Exception while deleteBeforeData()", e);
        }
    }
    /**
     * 根据IP获取该IP对应ping不通的次数。
     * @param map
     * @return
     */
    public List<ConnectionStateBean> getStatByParam(HashMap<String, Object> map){
        List<ConnectionStateBean> beanList=new ArrayList<ConnectionStateBean>();
        try {
            beanList=sqlmapClient.queryForList("getStatByParam", map);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return beanList;
    }


    /**
     * 根据ip查询该ip是否能ping通,时间按照倒序排序。
     * @param sql
     * @return
     */
    public String getPingResultByIp(String sql){
        List<ConnectionStateBean> beanList=new ArrayList<ConnectionStateBean>();
        try {
            beanList=sqlmapClient.queryForList("getPingResultByIp", sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String result="";
        log.info("&&&&&&&& beanList.size()="+beanList.size()+" &&&&&&&&&&&&&&&&&&&&&&");
        if(null !=beanList && beanList.size()>0){
            ConnectionStateBean bean=beanList.get(0);
            log.info("&&&&&&&& ConnectionStateBean ="+bean.toString()+" &&&&&&&&&&&&&&&&&&&&&&");
            result=bean.getDEVICE_STATE();
        }
        return result;
    }

}