完成日志功能!
This commit is contained in:
parent
1231fe3132
commit
2847747f53
@ -3,6 +3,7 @@
|
|||||||
"key": "fe9fa8eeeb6f4301a92541eed565dd15",
|
"key": "fe9fa8eeeb6f4301a92541eed565dd15",
|
||||||
"query_url": "https://devapi.qweather.com/v7/warning/now?",
|
"query_url": "https://devapi.qweather.com/v7/warning/now?",
|
||||||
"wechat_officalaccount_url": "https://cxxmwx.cpic.com.cn/app/index.php?i=2&c=entry&do=send_group_tpl_api&m=ok_tplmessage",
|
"wechat_officalaccount_url": "https://cxxmwx.cpic.com.cn/app/index.php?i=2&c=entry&do=send_group_tpl_api&m=ok_tplmessage",
|
||||||
|
"query_interval": 10,
|
||||||
"cities": [
|
"cities": [
|
||||||
{
|
{
|
||||||
"city_name": "厦门",
|
"city_name": "厦门",
|
||||||
|
@ -2,12 +2,13 @@
|
|||||||
* @Author: Kane
|
* @Author: Kane
|
||||||
* @Date: 2022-04-22 10:53:49
|
* @Date: 2022-04-22 10:53:49
|
||||||
* @LastEditors: Kane
|
* @LastEditors: Kane
|
||||||
* @LastEditTime: 2022-05-10 16:01:13
|
* @LastEditTime: 2022-05-10 16:28:07
|
||||||
* @FilePath: \DisasterWarning\src\main\java\AppMain.java
|
* @FilePath: \DisasterWarning\src\main\java\AppMain.java
|
||||||
* @Description: 和风天气预警推送厦门太保公众号主程序!
|
* @Description: 和风天气预警推送厦门太保公众号主程序!
|
||||||
*
|
*
|
||||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||||
*/
|
*/
|
||||||
|
import com.cpic.xim.config.AppConfigManager;
|
||||||
import com.cpic.xim.config.City;
|
import com.cpic.xim.config.City;
|
||||||
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
|
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
|
||||||
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
|
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
|
||||||
@ -48,7 +49,7 @@ public class AppMain
|
|||||||
// 读取配置
|
// 读取配置
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
config = WeatherDisasterNotifyConfig.getConfig();
|
config = AppConfigManager.getConfig();
|
||||||
}
|
}
|
||||||
catch ( IOException error )
|
catch ( IOException error )
|
||||||
{
|
{
|
||||||
@ -65,6 +66,8 @@ public class AppMain
|
|||||||
String userKey = config.getKey();
|
String userKey = config.getKey();
|
||||||
|
|
||||||
// 遍历所有城市,查询是否有警报,有则推送。
|
// 遍历所有城市,查询是否有警报,有则推送。
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
for ( City city : cities )
|
for ( City city : cities )
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -120,6 +123,18 @@ public class AppMain
|
|||||||
{ city.getCityName(), error.getMessage()} );
|
{ city.getCityName(), error.getMessage()} );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
logger.log( Level.INFO, "查询结束,休眠{0}分钟。", config.getQueryInterval() );
|
||||||
|
Thread.sleep( config.getQueryInterval() * 1000 * 60 );
|
||||||
|
}
|
||||||
|
catch ( InterruptedException error )
|
||||||
|
{
|
||||||
|
logger.log( Level.SEVERE, "线程休眠异常,错误信息:{0}", new Object[]
|
||||||
|
{ error.getMessage()} );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -127,20 +142,6 @@ public class AppMain
|
|||||||
*/
|
*/
|
||||||
private static void setRootLogger() throws IOException
|
private static void setRootLogger() throws IOException
|
||||||
{
|
{
|
||||||
// Logger rootLogger = Logger.getLogger( "com.cpicxim" );
|
|
||||||
// ConsoleHandler consoleHandler = new ConsoleHandler();
|
|
||||||
// FileHandler fileHandler = new FileHandler( LOG_FILE_PATH );
|
|
||||||
// SimpleFormatter formatter = new SimpleFormatter();
|
|
||||||
|
|
||||||
// consoleHandler.setFormatter( formatter );
|
|
||||||
// fileHandler.setFormatter( formatter );
|
|
||||||
|
|
||||||
// rootLogger.addHandler( consoleHandler );
|
|
||||||
// rootLogger.addHandler( fileHandler );
|
|
||||||
|
|
||||||
// rootLogger.setUseParentHandlers( false );
|
|
||||||
// rootLogger.setLevel( Level.ALL );
|
|
||||||
|
|
||||||
LogManager logManager = LogManager.getLogManager();
|
LogManager logManager = LogManager.getLogManager();
|
||||||
// 使用外部的配置文件。
|
// 使用外部的配置文件。
|
||||||
FileInputStream configFile = new FileInputStream( "./logging.properties" );
|
FileInputStream configFile = new FileInputStream( "./logging.properties" );
|
||||||
@ -150,6 +151,6 @@ public class AppMain
|
|||||||
|
|
||||||
logManager.readConfiguration( configFile );
|
logManager.readConfiguration( configFile );
|
||||||
|
|
||||||
Logger rootLogger = Logger.getLogger( "com.cpicxim" );
|
Logger.getLogger( "com.cpicxim" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* @Author: Kane
|
||||||
|
* @Date: 2022-05-10 16:06:14
|
||||||
|
* @LastEditors: Kane
|
||||||
|
* @LastEditTime: 2022-05-10 16:09:07
|
||||||
|
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\AppConfigManager.java
|
||||||
|
* @Description:
|
||||||
|
*
|
||||||
|
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.cpic.xim.config;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||||
|
|
||||||
|
public class AppConfigManager
|
||||||
|
{
|
||||||
|
private static final int BUFFER_SIZE = 1024;
|
||||||
|
private static final String CONFIG_FILE_CHARSET = "UTF-8";
|
||||||
|
private static final String CONFIG_FILE_PATH = "./config.json";
|
||||||
|
private static WeatherDisasterNotifyConfig appConfig = null;
|
||||||
|
|
||||||
|
public static WeatherDisasterNotifyConfig getConfig() throws IOException
|
||||||
|
|
||||||
|
{
|
||||||
|
if ( appConfig != null)
|
||||||
|
{
|
||||||
|
return appConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
FileInputStream configFile = null;
|
||||||
|
InputStreamReader in = null;
|
||||||
|
StringBuffer json = null;
|
||||||
|
char[] buffer = new char[BUFFER_SIZE];
|
||||||
|
|
||||||
|
// 设置json属性
|
||||||
|
mapper.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
configFile = new FileInputStream( CONFIG_FILE_PATH );
|
||||||
|
in = new InputStreamReader( configFile, CONFIG_FILE_CHARSET );
|
||||||
|
json = new StringBuffer();
|
||||||
|
|
||||||
|
int length = in.read( buffer );
|
||||||
|
|
||||||
|
while (length != -1)
|
||||||
|
{
|
||||||
|
json.append( buffer );
|
||||||
|
|
||||||
|
length = in.read( buffer );
|
||||||
|
}
|
||||||
|
|
||||||
|
appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class );
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if ( configFile != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
configFile.close();
|
||||||
|
}
|
||||||
|
catch ( IOException e )
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return appConfig;
|
||||||
|
}
|
||||||
|
}
|
@ -1,72 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* @Author: Kane
|
||||||
|
* @Date: 2022-04-24 10:21:46
|
||||||
|
* @LastEditors: Kane
|
||||||
|
* @LastEditTime: 2022-05-10 16:16:28
|
||||||
|
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\WeatherDisasterNotifyConfig.java
|
||||||
|
* @Description:
|
||||||
|
*
|
||||||
|
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||||
|
*/
|
||||||
package com.cpic.xim.config;
|
package com.cpic.xim.config;
|
||||||
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
|
||||||
|
|
||||||
public class WeatherDisasterNotifyConfig
|
public class WeatherDisasterNotifyConfig
|
||||||
{
|
{
|
||||||
private static final int BUFFER_SIZE = 1024;
|
|
||||||
private static final String CONFIG_FILE_CHARSET = "UTF-8";
|
|
||||||
private static final String CONFIG_FILE_PATH = "./config.json";
|
|
||||||
private static WeatherDisasterNotifyConfig appConfig = null;
|
|
||||||
|
|
||||||
public static WeatherDisasterNotifyConfig getConfig() throws IOException
|
|
||||||
|
|
||||||
{
|
|
||||||
if ( appConfig != null)
|
|
||||||
{
|
|
||||||
return appConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
|
||||||
FileInputStream configFile = null;
|
|
||||||
InputStreamReader in = null;
|
|
||||||
StringBuffer json = null;
|
|
||||||
char[] buffer = new char[BUFFER_SIZE];
|
|
||||||
|
|
||||||
// 设置json属性
|
|
||||||
mapper.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE );
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
configFile = new FileInputStream( CONFIG_FILE_PATH );
|
|
||||||
in = new InputStreamReader( configFile, CONFIG_FILE_CHARSET );
|
|
||||||
json = new StringBuffer();
|
|
||||||
|
|
||||||
int length = in.read( buffer );
|
|
||||||
|
|
||||||
while (length != -1)
|
|
||||||
{
|
|
||||||
json.append( buffer );
|
|
||||||
|
|
||||||
length = in.read( buffer );
|
|
||||||
}
|
|
||||||
|
|
||||||
appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class );
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if ( configFile != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
configFile.close();
|
|
||||||
}
|
|
||||||
catch ( IOException e )
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return appConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
public WeatherDisasterNotifyConfig()
|
public WeatherDisasterNotifyConfig()
|
||||||
{};
|
{};
|
||||||
@ -106,6 +56,18 @@ public class WeatherDisasterNotifyConfig
|
|||||||
return cities;
|
return cities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getQueryInterval()
|
||||||
|
{
|
||||||
|
return queryInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQueryInterval( int queryInterval )
|
||||||
|
{
|
||||||
|
this.queryInterval = queryInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setCities( Vector<City> cities )
|
public void setCities( Vector<City> cities )
|
||||||
{
|
{
|
||||||
this.cities = cities;
|
this.cities = cities;
|
||||||
@ -131,12 +93,24 @@ public class WeatherDisasterNotifyConfig
|
|||||||
this.wechatOfficalAccountURL = wechatOfficalAccountURL;
|
this.wechatOfficalAccountURL = wechatOfficalAccountURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonProperty( "title")
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
|
@JsonProperty( "key")
|
||||||
private String key;
|
private String key;
|
||||||
|
|
||||||
|
@JsonProperty( "query_url")
|
||||||
private String queryUrl;
|
private String queryUrl;
|
||||||
|
|
||||||
|
@JsonProperty( "query_interval")
|
||||||
|
private int queryInterval;
|
||||||
|
|
||||||
@JsonProperty( "wechat_officalaccount_url")
|
@JsonProperty( "wechat_officalaccount_url")
|
||||||
private String wechatOfficalAccountURL;
|
private String wechatOfficalAccountURL;
|
||||||
|
|
||||||
|
@JsonProperty( "cities")
|
||||||
private Vector<City> cities;
|
private Vector<City> cities;
|
||||||
|
|
||||||
|
@JsonProperty( "notify_stuffs")
|
||||||
private Vector<CpicxmStuff> notifyStuffs;
|
private Vector<CpicxmStuff> notifyStuffs;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user