改变配置文件的获取方式!改成singleton!

This commit is contained in:
Kane Wang 2022-04-25 23:27:38 +08:00
parent a78fad18ea
commit 9d57987f0c
6 changed files with 121 additions and 13 deletions

View File

@ -1,12 +1,13 @@
{ {
"tns_name": "xmcx1", "tns_name": "xmcx1",
"ip_addr": "10.39.0.86", "ip_addr": "10.39.0.86",
"user_name": "", "jdbc_url": "",
"password": "", "user_name": "xim_offical_account",
"password": "Cpic#1234",
"tables": [ "tables": [
{ {
"table_name": "", "table_name": "",
"table_description": "" "table_description": ""
} }
] ]
} }

View File

@ -19,8 +19,6 @@ import java.util.Vector;
public class AppMain public class AppMain
{ {
private static final String CONFIG_FILE_PATH = "./config.json";
public static void main( String[] args ) public static void main( String[] args )
{ {
String json; String json;
@ -30,7 +28,7 @@ public class AppMain
// 读取配置 // 读取配置
try try
{ {
config = WeatherDisasterNotifyConfig.load( CONFIG_FILE_PATH ); config = WeatherDisasterNotifyConfig.getConfig();
} }
catch (IOException error) catch (IOException error)
{ {

View File

@ -11,11 +11,17 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategy;
public class WeatherDisasterNotifyConfig public class WeatherDisasterNotifyConfig
{ {
private static final int BUFFER_SIZE = 1024; private static final int BUFFER_SIZE = 1024;
private static final String CONFIG_FILE_PATH = "./config.json";
private static WeatherDisasterNotifyConfig appConfig = null;
public static WeatherDisasterNotifyConfig load( String filePath ) throws IOException public static WeatherDisasterNotifyConfig getConfig() throws IOException
{ {
WeatherDisasterNotifyConfig config = null; if ( appConfig != null)
{
return appConfig;
}
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
FileReader configFile = null; FileReader configFile = null;
StringBuffer json = null; StringBuffer json = null;
@ -26,7 +32,7 @@ public class WeatherDisasterNotifyConfig
try try
{ {
configFile = new FileReader( filePath ); configFile = new FileReader( CONFIG_FILE_PATH );
json = new StringBuffer(); json = new StringBuffer();
int length = configFile.read( buffer ); int length = configFile.read( buffer );
@ -38,7 +44,7 @@ public class WeatherDisasterNotifyConfig
length = configFile.read( buffer ); length = configFile.read( buffer );
} }
config = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class ); appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class );
} }
finally finally
{ {
@ -55,7 +61,7 @@ public class WeatherDisasterNotifyConfig
} }
} }
return config; return appConfig;
} }
public WeatherDisasterNotifyConfig() public WeatherDisasterNotifyConfig()

View File

@ -2,7 +2,7 @@
* @Author: Kane * @Author: Kane
* @Date: 2022-04-22 17:33:30 * @Date: 2022-04-22 17:33:30
* @LastEditors: Kane * @LastEditors: Kane
* @LastEditTime: 2022-04-23 23:51:46 * @LastEditTime: 2022-04-25 21:33:07
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\db\OracleConfig.java * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\db\OracleConfig.java
* @Description: * @Description:
* *
@ -11,7 +11,11 @@
package com.cpic.xim.config.db; package com.cpic.xim.config.db;
import java.util.Vector; import java.util.Vector;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @Description oracle数据库相关参数对象
*/
public class OracleConfig public class OracleConfig
{ {
@ -75,10 +79,21 @@ public class OracleConfig
this.tables = tables; this.tables = tables;
} }
@JsonProperty( "tns_name")
private String tnsName; private String tnsName;
@JsonProperty( "ip_addr")
private String ipAddr; private String ipAddr;
@JsonProperty( "jdbc_url")
private String jdbcURL; private String jdbcURL;
@JsonProperty( "user_name")
private String userName; private String userName;
@JsonProperty( "password")
private String password; private String password;
@JsonProperty( "tables")
private Vector<DBTable> tables; private Vector<DBTable> tables;
} }

View File

@ -0,0 +1,74 @@
/*
* @Author: Kane
* @Date: 2022-04-25 21:45:12
* @LastEditors: Kane
* @LastEditTime: 2022-04-25 22:58:47
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\db\OracleConfigLoader.java
* @Description: oracle数据库配置文件加载类
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
*/
package com.cpic.xim.config.db;
import java.io.*;
import com.fasterxml.jackson.databind.*;
public class OracleConfigLoader
{
private static final String CONFIG_FILE_PATH = "./db.json";
private static final int BUFFER_SIZE = 1024;
private static OracleConfig dbConfig = null;
private OracleConfigLoader()
{}
public OracleConfig getOracleConfig() throws IOException
{
FileReader file = null;
StringBuffer json = null;
char[] buffer = new char[BUFFER_SIZE];
if ( dbConfig != null)
{
return dbConfig;
}
try
{
file = new FileReader( CONFIG_FILE_PATH );
json = new StringBuffer();
int count = file.read( buffer );
while (count != -1)
{
json.append( buffer );
count = file.read( buffer );
}
ObjectMapper mapper = new ObjectMapper();
dbConfig = mapper.readValue( json.toString(), OracleConfig.class );
}
catch (IOException error)
{
throw error;
}
finally
{
try
{
file.close();
}
catch (Exception error)
{
error.printStackTrace();
}
}
return dbConfig;
}
}

View File

@ -2,7 +2,7 @@
* @Author: Kane * @Author: Kane
* @Date: 2022-04-22 10:53:49 * @Date: 2022-04-22 10:53:49
* @LastEditors: Kane * @LastEditors: Kane
* @LastEditTime: 2022-04-25 17:40:31 * @LastEditTime: 2022-04-25 19:20:32
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java
* @Description: 用来推送公众号消息的程序库 * @Description: 用来推送公众号消息的程序库
* *
@ -75,6 +75,20 @@ public class sendMessage
} }
} }
/**
*
* @param warningID
* @return
*/
private boolean checkWarningHasSended( String warningID )
{
boolean result = false;
return result;
}
static static
{ {
// 预警级别色彩 // 预警级别色彩