项目健康检查

在项目中,我们需要知道项目数据源连接状态和war的信息详细,所以有一个对于项目健康检查是必不可少的
定义的输出结果的实体类

  @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    final class Result {


        private TridionDependency tridionDependency;
        private AppInfo appInfo;
        private War war;
        private int health;
        private String description;
        private DatabaseDependency databaseDependency;
        //setter 和 getter
    }

TridionDependency

class TridionDependency {
        private String name;
        private int health;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getHealth() {
            return health;
        }

        public void setHealth(int health) {
            this.health = health;
        }

    }

AppInfo

class AppInfo {
    private String name;
    private int health;
    private AppinfoDetails details;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public AppinfoDetails getDetails() {
        return details;
    }

    public void setDetails(AppinfoDetails details) {
        this.details = details;
    }


}

war

class War {
    private String name;
    private int health;
    private WarDetails details;
    //setter 和 getter
}

DatabaseDependency

DatabaseDependency {
   
    private String name;
    private int health;
    private DatabaseDetails details;
}
class AppinfoDetails {

    private String logPath;
    private String imageUploadPath;
}

具体实现类

         //初始化输出结果类
         Result result = new Result();
        // 初始化信息类
        DatabaseDependency database = new DatabaseDependency();
        //数据库详细信息类
        DatabaseDetails databaseDetails = new DatabaseDetails();
        //设置成全局的为了close()
        Connection conn = null;
        ResultSet tables = null;
        try {
            //配置你自己的数据库参数
            Class.forName("com.mysql.jdbc.Driver");
            String username = Constants.spring_datasource_username;
            String passworld = Constants.spring_datasource_password;
            String url = Constants.spring_datasource_url;
            logger.info("username=====" + username);
            logger.info("passworld=====" + passworld);
            logger.info("url=====" + url);
            try {
               //获取连接
                conn = DriverManager.getConnection(url, username, passworld);
                //得到连接数据库对象
                DatabaseMetaData dbmd = conn.getMetaData();
                //获取数据库版本
                String version = dbmd.getDatabaseProductVersion();
                //获取mysq 驱动的版本
                String driverversion = dbmd.getDriverVersion();
                //获取数据库表的信息,并且封装套list结合里面  resultSetToList后面有介绍
                String[] types = new String[]{"TABLE"};
                tables = dbmd.getTables(null, null, "", types);
                List list = resultSetToList(tables);
                //分别把输出的信息,封装到实体类上
                databaseDetails.setDbStatus("connected");
                databaseDetails.setDbDriverNameVersion(driverversion);
                databaseDetails.setDbConnetionURL(url);
                databaseDetails.setDbProductNameVersion(version);
                databaseDetails.setDbCatalog(list);
                database.setName("databaseDependency");
                //0就代表正常啦
                database.setHealth(0);
                database.setDetails(databaseDetails);
                //war包信息的读取
                War war = new War();
                war.setName("war");
                war.setHealth(0);
                WarDetails warDetails = new WarDetails();
                Manifest manifest = new Manifest();
                MainAttributes mainAttributes = new MainAttributes();
                //定义的war的相关信息,比如路径
                String warpath = System.getProperty("user.dir");
                String Build_version = "0.0.5-SNAPSHOT";
                String ArtifactId = "alibaba";
                String groupId = "com.alibaba.test.service";
                mainAttributes.setBuild_version(Build_version);
                mainAttributes.setArtifactId(ArtifactId);
                mainAttributes.setGroupId(groupId);
                //设置到相关对象
                manifest.setMainAttributes(mainAttributes);
                warDetails.setManifest(manifest);
                war.setDetails(warDetails);

                AppInfo appInfo = new AppInfo();
                AppinfoDetails appinfoDetails = new AppinfoDetails();
                //设置你日志保存的路径
                appinfoDetails.setLogPath(warpath + "\\log");\]
                //你图片上传的路径
                appinfoDetails.setImageUploadPath(Constants.ROOTPATH + "WEB-INF/classes/static/img/thumb");
                logger.info("logpath" + appinfoDetails.getImageUploadPath());
                appInfo.setName("appInfo");
                appInfo.setHealth(0);
                appInfo.setDetails(appinfoDetails);
               //把相关信息放入输出结果类上
                TridionDependency tridionDependency = new TridionDependency();
                tridionDependency.setName("tridionDependency");
                tridionDependency.setHealth(0);
                result.setTridionDependency(tridionDependency);
                result.setDatabaseDependency(database);
                result.setAppInfo(appInfo);
                result.setDescription("All system tests pass.");
                result.setWar(war);
                result.setHealth(0);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                databaseDetails.setDbStatus("数据库连接失败");
                database.setHealth(1);
                result.setHealth(1);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            databaseDetails.setDbStatus("驱动加载错误");
            database.setHealth(1);
            result.setHealth(1);
        } finally {
            if (conn != null) {
                conn.close();
            } else if (tables != null){
                tables.close();
            }
        }
        return result;

工具类

resultSetToList 把数据库信息封装到list集合当中

public static List resultSetToList(ResultSet rs) throws java.sql.SQLException {
        if (rs == null)
            return Collections.EMPTY_LIST;
        ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等
        int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数
        List list = new ArrayList();
        Map rowData = new HashMap();
        while (rs.next()) {
            rowData = new HashMap(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }

你可能感兴趣的:(java)