Compare commits
	
		
			14 Commits
		
	
	
		
			51a2829433
			...
			完善日志功能
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1231fe3132 | |||
| fbe9f8e532 | |||
| 53849e4f74 | |||
| d1bcb3b956 | |||
| c0443d3a8b | |||
| eb7f53ad4b | |||
| 91c99ba8f9 | |||
| 0fb8b90509 | |||
| cd743c645f | |||
| 2d1d4dea66 | |||
| 24cdc66b7d | |||
| 5da53f02ea | |||
| 0587001de0 | |||
| 64170d2e67 | 
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -2,4 +2,5 @@ code/java/天气灾害预警/target/
 | 
			
		||||
code/java/天气灾害预警/out/
 | 
			
		||||
*.jar
 | 
			
		||||
*.iws
 | 
			
		||||
*.log
 | 
			
		||||
*.log
 | 
			
		||||
*.lck
 | 
			
		||||
							
								
								
									
										5
									
								
								code/java/DisasterWarning/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								code/java/DisasterWarning/.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -133,4 +133,7 @@ $RECYCLE.BIN/
 | 
			
		||||
# Windows shortcuts
 | 
			
		||||
*.lnk
 | 
			
		||||
 | 
			
		||||
target/*
 | 
			
		||||
target/*
 | 
			
		||||
 | 
			
		||||
logs/*
 | 
			
		||||
logs
 | 
			
		||||
@@ -1,13 +1,14 @@
 | 
			
		||||
{
 | 
			
		||||
    "tns_name": "xmcx1",
 | 
			
		||||
    "ip_addr": "10.39.0.86",
 | 
			
		||||
    "jdbc_url": "",
 | 
			
		||||
    "user_name": "xim_offical_account",
 | 
			
		||||
    "password": "Cpic#1234",
 | 
			
		||||
    "jdbc_url": "jdbc:oracle:thin:@10.39.0.86:1521:xmcx1",
 | 
			
		||||
    "table_space": "wechat",
 | 
			
		||||
    "user_name": "wechat",
 | 
			
		||||
    "password": "@rn7Q+t5zeyKIZ~s",
 | 
			
		||||
    "tables": [
 | 
			
		||||
        {
 | 
			
		||||
            "table_name": "",
 | 
			
		||||
            "table_description": ""
 | 
			
		||||
            "table_name": "weather_disaster_notify",
 | 
			
		||||
            "table_description": "天气预警消息表"
 | 
			
		||||
        }
 | 
			
		||||
    ]
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								code/java/DisasterWarning/logging.properties
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								code/java/DisasterWarning/logging.properties
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
 | 
			
		||||
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
 | 
			
		||||
 | 
			
		||||
.level = INFO
 | 
			
		||||
 | 
			
		||||
java.util.logging.FileHandler.pattern = ./logs/log_%u.log
 | 
			
		||||
java.util.logging.FileHandler.limit = 50000
 | 
			
		||||
java.util.logging.FileHandler.count = 1
 | 
			
		||||
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
 | 
			
		||||
java.util.logging.FileHandler.append = true;
 | 
			
		||||
@@ -5,6 +5,13 @@
 | 
			
		||||
    <artifactId>disaster_warning</artifactId>
 | 
			
		||||
    <version>1.0-SNAPSHOT</version>
 | 
			
		||||
    <dependencies>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.oracle</groupId>
 | 
			
		||||
            <artifactId>ojdbc8</artifactId>
 | 
			
		||||
            <version>1.0</version>
 | 
			
		||||
            <scope>system</scope>
 | 
			
		||||
            <systemPath>${project.basedir}/lib/ojdbc8.jar</systemPath>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.apache.httpcomponents</groupId>
 | 
			
		||||
            <artifactId>httpclient</artifactId>
 | 
			
		||||
@@ -16,7 +23,7 @@
 | 
			
		||||
            <version>4.13.2</version>
 | 
			
		||||
            <scope>test</scope>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
        <!-- <dependency>
 | 
			
		||||
            <groupId>commons-beanutils</groupId>
 | 
			
		||||
            <artifactId>commons-beanutils</artifactId>
 | 
			
		||||
            <version>1.9.4</version>
 | 
			
		||||
@@ -46,7 +53,7 @@
 | 
			
		||||
            <artifactId>json-lib</artifactId>
 | 
			
		||||
            <version>2.4</version>
 | 
			
		||||
            <classifier>jdk15</classifier>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        </dependency> -->
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.fasterxml.jackson.core</groupId>
 | 
			
		||||
            <artifactId>jackson-core</artifactId>
 | 
			
		||||
@@ -72,6 +79,40 @@
 | 
			
		||||
    </properties>
 | 
			
		||||
    <build>
 | 
			
		||||
        <defaultGoal>compile</defaultGoal>
 | 
			
		||||
 | 
			
		||||
        <plugins>
 | 
			
		||||
            <plugin>
 | 
			
		||||
                <groupId>org.apache.maven.plugins</groupId>
 | 
			
		||||
                <artifactId>maven-surefire-plugin</artifactId>
 | 
			
		||||
                <version>3.0.0-M6</version>
 | 
			
		||||
                <configuration>
 | 
			
		||||
                    <skipTests>true</skipTests>
 | 
			
		||||
                </configuration>
 | 
			
		||||
            </plugin>
 | 
			
		||||
            <plugin>
 | 
			
		||||
                <groupId>org.apache.maven.plugins</groupId>
 | 
			
		||||
                <artifactId>maven-assembly-plugin</artifactId>
 | 
			
		||||
                <configuration>
 | 
			
		||||
                    <descriptorRefs>
 | 
			
		||||
                        <descriptorRef>jar-with-dependencies</descriptorRef>
 | 
			
		||||
                    </descriptorRefs>
 | 
			
		||||
                    <archive>
 | 
			
		||||
                        <manifest>
 | 
			
		||||
                            <mainClass>AppMain</mainClass>
 | 
			
		||||
                        </manifest>
 | 
			
		||||
                    </archive>
 | 
			
		||||
                </configuration>
 | 
			
		||||
                <executions>
 | 
			
		||||
                    <execution>
 | 
			
		||||
                        <id>make-assmenbly</id>
 | 
			
		||||
                        <phase>package</phase>
 | 
			
		||||
                        <goals>
 | 
			
		||||
                            <goal>single</goal>
 | 
			
		||||
                        </goals>
 | 
			
		||||
                    </execution>
 | 
			
		||||
                </executions>
 | 
			
		||||
            </plugin>
 | 
			
		||||
        </plugins>
 | 
			
		||||
    </build>
 | 
			
		||||
    <profiles>
 | 
			
		||||
        <profile>
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 * @Author: Kane
 | 
			
		||||
 * @Date: 2022-04-22 10:53:49
 | 
			
		||||
 * @LastEditors: Kane
 | 
			
		||||
 * @LastEditTime: 2022-04-26 22:33:14
 | 
			
		||||
 * @LastEditTime: 2022-05-10 16:01:13
 | 
			
		||||
 * @FilePath: \DisasterWarning\src\main\java\AppMain.java
 | 
			
		||||
 * @Description: 和风天气预警推送厦门太保公众号主程序!
 | 
			
		||||
 * 
 | 
			
		||||
@@ -11,16 +11,18 @@
 | 
			
		||||
import com.cpic.xim.config.City;
 | 
			
		||||
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
 | 
			
		||||
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
 | 
			
		||||
import com.cpic.xim.wechat.officalAccount.sendMessage;
 | 
			
		||||
import com.cpic.xim.config.WeatherDisasterNotifyConfig;
 | 
			
		||||
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.sql.SQLException;
 | 
			
		||||
import java.util.Vector;
 | 
			
		||||
import java.util.logging.*;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class AppMain
 | 
			
		||||
{
 | 
			
		||||
    private final static String LOG_FILE_PATH = "./app.log";
 | 
			
		||||
    // private final static String LOG_FILE_PATH = "./logs/app%u.log";
 | 
			
		||||
 | 
			
		||||
    public static void main( String[] args )
 | 
			
		||||
    {
 | 
			
		||||
@@ -43,7 +45,6 @@ public class AppMain
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        // 读取配置
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
@@ -68,8 +69,6 @@ public class AppMain
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL,
 | 
			
		||||
                        userKey, city.getCityCode() );
 | 
			
		||||
                warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
 | 
			
		||||
@@ -80,14 +79,21 @@ public class AppMain
 | 
			
		||||
                // 判断是否有警报
 | 
			
		||||
                if ( warning.getWarning().isEmpty() == true)
 | 
			
		||||
                {
 | 
			
		||||
                    logger.log( Level.INFO, "查询{0}天气预警,无警报!。", new Object[]
 | 
			
		||||
                    { city.getCityName()} );
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                logger.log( Level.INFO, "查询{0}天气预警,发送日志。", new Object[]
 | 
			
		||||
                { city.getCityName()} );
 | 
			
		||||
 | 
			
		||||
                // sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(),
 | 
			
		||||
                // warning );
 | 
			
		||||
                sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(),
 | 
			
		||||
                        warning );
 | 
			
		||||
 | 
			
		||||
                logger.log( Level.INFO, "{0}天气预警,日志发送成功。", new Object[]
 | 
			
		||||
                { city.getCityName()} );
 | 
			
		||||
 | 
			
		||||
                sendMessage.saveWeatherDisasterWarning( city.getCityName(), warning );
 | 
			
		||||
            }
 | 
			
		||||
            catch ( IOException error )
 | 
			
		||||
            {
 | 
			
		||||
@@ -96,8 +102,22 @@ public class AppMain
 | 
			
		||||
 | 
			
		||||
                logger.log( Level.SEVERE, "查询 {0} 出现异常:{1}。", new Object[]
 | 
			
		||||
                { city.getCityName(), error.getMessage()} );
 | 
			
		||||
            }
 | 
			
		||||
            catch ( SQLException error )
 | 
			
		||||
            {
 | 
			
		||||
                logger.log( Level.SEVERE, "查询 {0} 写入数据库失败:{1}。", new Object[]
 | 
			
		||||
                { city.getCityName(), error.getMessage()} );
 | 
			
		||||
            }
 | 
			
		||||
            catch ( ClassNotFoundException error )
 | 
			
		||||
            {
 | 
			
		||||
                logger.log( Level.SEVERE, "查询 {0} 加载oracle驱动失败:{1}。", new Object[]
 | 
			
		||||
                { city.getCityName(), error.getMessage()} );
 | 
			
		||||
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            catch ( Exception error )
 | 
			
		||||
            {
 | 
			
		||||
                logger.log( Level.SEVERE, "查询 {0} 出现未知错误:{1}。", new Object[]
 | 
			
		||||
                { city.getCityName(), error.getMessage()} );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -107,17 +127,29 @@ public class AppMain
 | 
			
		||||
     */
 | 
			
		||||
    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();
 | 
			
		||||
        // 使用外部的配置文件。
 | 
			
		||||
        FileInputStream configFile = new FileInputStream( "./logging.properties" );
 | 
			
		||||
        // 使用jar中的配置文件。
 | 
			
		||||
        // InputStream configFile =
 | 
			
		||||
        // AppMain.class.getClassLoader().getResourceAsStream( "logging.properties" );
 | 
			
		||||
 | 
			
		||||
        logManager.readConfiguration( configFile );
 | 
			
		||||
 | 
			
		||||
        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 );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 * @Author: Kane
 | 
			
		||||
 * @Date: 2022-04-22 17:33:30
 | 
			
		||||
 * @LastEditors: Kane
 | 
			
		||||
 * @LastEditTime: 2022-04-23 23:50:20
 | 
			
		||||
 * @LastEditTime: 2022-04-27 16:53:13
 | 
			
		||||
 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\db\DBTable.java
 | 
			
		||||
 * @Description: 
 | 
			
		||||
 * 
 | 
			
		||||
@@ -12,6 +12,8 @@ package com.cpic.xim.config.db;
 | 
			
		||||
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonProperty;
 | 
			
		||||
 | 
			
		||||
public class DBTable
 | 
			
		||||
{
 | 
			
		||||
    public DBTable()
 | 
			
		||||
@@ -59,6 +61,9 @@ public class DBTable
 | 
			
		||||
        return Objects.hash( tableName, tableDescription );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @JsonProperty( "table_name")
 | 
			
		||||
    private String tableName;
 | 
			
		||||
 | 
			
		||||
    @JsonProperty( "table_description")
 | 
			
		||||
    private String tableDescription;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -49,6 +49,17 @@ public class OracleConfig
 | 
			
		||||
        this.jdbcURL = jdbcURL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public String getTableSpace()
 | 
			
		||||
    {
 | 
			
		||||
        return tableSpace;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setTableSpace( String tableSpace )
 | 
			
		||||
    {
 | 
			
		||||
        this.tableSpace = tableSpace;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getUserName()
 | 
			
		||||
    {
 | 
			
		||||
        return userName;
 | 
			
		||||
@@ -88,6 +99,9 @@ public class OracleConfig
 | 
			
		||||
    @JsonProperty( "jdbc_url")
 | 
			
		||||
    private String jdbcURL;
 | 
			
		||||
 | 
			
		||||
    @JsonProperty( "table_space")
 | 
			
		||||
    private String tableSpace;
 | 
			
		||||
 | 
			
		||||
    @JsonProperty( "user_name")
 | 
			
		||||
    private String userName;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,8 +2,8 @@
 | 
			
		||||
 * @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
 | 
			
		||||
 * @LastEditTime: 2022-05-03 22:34:37
 | 
			
		||||
 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\config\db\OracleConfigManager.java
 | 
			
		||||
 * @Description: oracle数据库配置文件加载类
 | 
			
		||||
 * 
 | 
			
		||||
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 | 
			
		||||
@@ -14,17 +14,17 @@ import java.io.*;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.*;
 | 
			
		||||
 | 
			
		||||
public class OracleConfigLoader
 | 
			
		||||
public class OracleConfigManager
 | 
			
		||||
{
 | 
			
		||||
    private static final String CONFIG_FILE_PATH = "./db.json";
 | 
			
		||||
    private static final String CONFIG_FILE_CHARSET = "UTF-8";
 | 
			
		||||
    private static final int BUFFER_SIZE = 1024;
 | 
			
		||||
    private static OracleConfig dbConfig = null;
 | 
			
		||||
 | 
			
		||||
    private OracleConfigLoader()
 | 
			
		||||
    private OracleConfigManager()
 | 
			
		||||
    {}
 | 
			
		||||
 | 
			
		||||
    public OracleConfig getOracleConfig() throws IOException
 | 
			
		||||
    public static OracleConfig getOracleConfig() throws IOException
 | 
			
		||||
    {
 | 
			
		||||
        FileInputStream file = null;
 | 
			
		||||
        InputStreamReader in = null;
 | 
			
		||||
@@ -39,10 +39,9 @@ public class OracleConfigLoader
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            file = new FileInputStream( CONFIG_FILE_PATH );
 | 
			
		||||
            in = new InputStreamReader( file, CONFIG_FILE_CHARSET);
 | 
			
		||||
            in = new InputStreamReader( file, CONFIG_FILE_CHARSET );
 | 
			
		||||
            json = new StringBuffer();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            int count = in.read( buffer );
 | 
			
		||||
 | 
			
		||||
            while (count != -1)
 | 
			
		||||
@@ -56,7 +55,7 @@ public class OracleConfigLoader
 | 
			
		||||
 | 
			
		||||
            dbConfig = mapper.readValue( json.toString(), OracleConfig.class );
 | 
			
		||||
        }
 | 
			
		||||
        catch (IOException error)
 | 
			
		||||
        catch ( IOException error )
 | 
			
		||||
        {
 | 
			
		||||
            throw error;
 | 
			
		||||
        }
 | 
			
		||||
@@ -66,7 +65,7 @@ public class OracleConfigLoader
 | 
			
		||||
            {
 | 
			
		||||
                file.close();
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception error)
 | 
			
		||||
            catch ( Exception error )
 | 
			
		||||
            {
 | 
			
		||||
                error.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 * @Author: Kane
 | 
			
		||||
 * @Date: 2022-04-22 10:53:49
 | 
			
		||||
 * @LastEditors: Kane
 | 
			
		||||
 * @LastEditTime: 2022-04-25 19:20:32
 | 
			
		||||
 * @LastEditTime: 2022-05-06 11:14:32
 | 
			
		||||
 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java
 | 
			
		||||
 * @Description: 用来推送公众号消息的程序库。
 | 
			
		||||
 *
 | 
			
		||||
@@ -10,13 +10,20 @@
 | 
			
		||||
 */
 | 
			
		||||
package com.cpic.xim.wechat.officalAccount;
 | 
			
		||||
 | 
			
		||||
import com.cpic.xim.config.db.OracleConfigManager;
 | 
			
		||||
import com.cpic.xim.config.db.OracleConfig;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonProperty;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.MalformedURLException;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Vector;
 | 
			
		||||
 | 
			
		||||
import java.sql.Connection;
 | 
			
		||||
import java.sql.DriverManager;
 | 
			
		||||
import java.sql.PreparedStatement;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
import java.sql.SQLException;
 | 
			
		||||
import com.cpic.xim.notify.disaster.*;
 | 
			
		||||
import com.cpic.xim.httpUtil.HttpUtils;
 | 
			
		||||
 | 
			
		||||
@@ -31,7 +38,7 @@ public class sendMessage
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 推送天气灾害预警!接口文档参考 彭奕洁 编写《消息发送接口调用文档》
 | 
			
		||||
     * 
 | 
			
		||||
     * 使用 post 方式,请求体内容以最基础的 post 格式。
 | 
			
		||||
     * @param officalAccountURL 产险厦门分公司公众号接口网址
 | 
			
		||||
     * @param warning           灾害预警对象
 | 
			
		||||
     */
 | 
			
		||||
@@ -49,7 +56,7 @@ public class sendMessage
 | 
			
		||||
        headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" );
 | 
			
		||||
 | 
			
		||||
        // 遍历消息,将消息推送出去。
 | 
			
		||||
        for ( QWeatherDisasterWarningItem item : warningItems)
 | 
			
		||||
        for ( QWeatherDisasterWarningItem item : warningItems )
 | 
			
		||||
        {
 | 
			
		||||
            // 拼接消息内容
 | 
			
		||||
            String title = item.getTitle();
 | 
			
		||||
@@ -68,7 +75,7 @@ public class sendMessage
 | 
			
		||||
            {
 | 
			
		||||
                HttpUtils.postHttpRequest( officalAccountURL, headers, requestBody.toString() );
 | 
			
		||||
            }
 | 
			
		||||
            catch (MalformedURLException error)
 | 
			
		||||
            catch ( MalformedURLException error )
 | 
			
		||||
            {
 | 
			
		||||
                error.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
@@ -76,19 +83,180 @@ public class sendMessage
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @param warningID
 | 
			
		||||
     * @return
 | 
			
		||||
     * 检查预警是否已经推送过,通过 QWeatherDisasterWarningItem 对象的 id 属性。
 | 
			
		||||
     * 查询 oracle xmcx1 数据库 wechat 表空间下 weather_disaster_notify 表,
 | 
			
		||||
     * 统计 warningID 的数量,如果不为0,说明该预警已经保存过,视为已经推送过。
 | 
			
		||||
     * @param warningID 预警的id
 | 
			
		||||
     * @return 如果推送过,返回false,否则返回 true。
 | 
			
		||||
     */
 | 
			
		||||
    private boolean checkWarningHasSended( String warningID )
 | 
			
		||||
    public static boolean checkWarningHasSended( String warningID )
 | 
			
		||||
            throws ClassNotFoundException, SQLException, IOException
 | 
			
		||||
    {
 | 
			
		||||
        boolean result = false;
 | 
			
		||||
 | 
			
		||||
        OracleConfig dbConfig = OracleConfigManager.getOracleConfig();;
 | 
			
		||||
 | 
			
		||||
        Connection conn = null;
 | 
			
		||||
        PreparedStatement stmt = null;
 | 
			
		||||
        ResultSet rs = null;
 | 
			
		||||
 | 
			
		||||
        String jdbcURL = dbConfig.getJdbcURL();
 | 
			
		||||
        String userName = dbConfig.getUserName();
 | 
			
		||||
        String password = dbConfig.getPassword();
 | 
			
		||||
        String sql = "select count(*) from weather_disaster_notify where notify_id = ?";
 | 
			
		||||
 | 
			
		||||
        // Logger logger = Logger.getLogger( "com.cpicxim.wechat.officalAccount.sendMessage" );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            Class.forName( "oracle.jdbc.driver.OracleDriver" );
 | 
			
		||||
 | 
			
		||||
            conn = DriverManager.getConnection( jdbcURL, userName, password );
 | 
			
		||||
            stmt = conn.prepareStatement( sql );
 | 
			
		||||
            stmt.setString( 1, warningID );
 | 
			
		||||
 | 
			
		||||
            rs = stmt.executeQuery();
 | 
			
		||||
 | 
			
		||||
            if ( rs.next() && rs.getInt( 1 ) == 0)
 | 
			
		||||
            {
 | 
			
		||||
                result = true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                if ( rs != null)
 | 
			
		||||
                {
 | 
			
		||||
                    rs.close();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch ( Exception error )
 | 
			
		||||
            {
 | 
			
		||||
                error.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                if ( stmt != null)
 | 
			
		||||
                {
 | 
			
		||||
                    stmt.close();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch ( Exception error )
 | 
			
		||||
            {
 | 
			
		||||
                error.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                if ( conn != null)
 | 
			
		||||
                {
 | 
			
		||||
                    conn.close();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch ( Exception error )
 | 
			
		||||
            {
 | 
			
		||||
                error.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 保存天气预警警报数据。
 | 
			
		||||
     * 保存至 oracle xmcx1 数据库 wechat 表空间下 weather_disaster_notify 表。
 | 
			
		||||
     * 保存的数据用来判断预警是否已经推送过,以防止反复推送数据。
 | 
			
		||||
     * @param city 城市名称
 | 
			
		||||
     * @param warning 预警数据,为 QWeatherDisasterWarningItem 对象。
 | 
			
		||||
     * @throws SQLException 执行sql时可能抛出的异常。出现sql异常,就回滚事务。
 | 
			
		||||
     * @throws IOException 读取数据库配置文件时可能抛出的异常。
 | 
			
		||||
     * @throws ClassNotFoundException 加载oracle jdbc驱动时可能抛出的异常。
 | 
			
		||||
     */
 | 
			
		||||
    public static void saveWeatherDisasterWarning( String city, QWeatherDisasterWarning warnings )
 | 
			
		||||
            throws SQLException, IOException, ClassNotFoundException
 | 
			
		||||
    {
 | 
			
		||||
        Connection conn = null;
 | 
			
		||||
        PreparedStatement stmt = null;
 | 
			
		||||
 | 
			
		||||
        String sql = "insert into weather_disaster_notify(notify_id, sender, city,"
 | 
			
		||||
                + "warning_level, warning_type_code, warning_type_name, title,"
 | 
			
		||||
                + "text, pub_time) values( ?,?,?,?,?,?,?,?,?)";
 | 
			
		||||
 | 
			
		||||
        OracleConfig dbConfig = OracleConfigManager.getOracleConfig();
 | 
			
		||||
 | 
			
		||||
        String jdbcURL = dbConfig.getJdbcURL();
 | 
			
		||||
        String userName = dbConfig.getUserName();
 | 
			
		||||
        String password = dbConfig.getPassword();
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            Class.forName( "oracle.jdbc.driver.OracleDriver" );
 | 
			
		||||
 | 
			
		||||
            conn = DriverManager.getConnection( jdbcURL, userName, password );
 | 
			
		||||
            stmt = conn.prepareStatement( sql );
 | 
			
		||||
 | 
			
		||||
            for ( QWeatherDisasterWarningItem warning : warnings.getWarning() )
 | 
			
		||||
            {
 | 
			
		||||
                java.sql.Timestamp pubTime =
 | 
			
		||||
                        new java.sql.Timestamp( warning.getPubTime().getTime() );
 | 
			
		||||
 | 
			
		||||
                stmt.setString( 1, warning.getId() );
 | 
			
		||||
                stmt.setString( 2, warning.getSender() );
 | 
			
		||||
                stmt.setString( 3, city );
 | 
			
		||||
                stmt.setString( 4, warning.getLevel() );
 | 
			
		||||
                stmt.setString( 5, warning.getType() );
 | 
			
		||||
                stmt.setString( 6, warning.getTypeName() );
 | 
			
		||||
                stmt.setString( 7, warning.getTitle() );
 | 
			
		||||
                stmt.setString( 8, warning.getText() );
 | 
			
		||||
                stmt.setTimestamp( 9, pubTime );
 | 
			
		||||
 | 
			
		||||
                stmt.execute();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // 没有问题就提交
 | 
			
		||||
            conn.commit();
 | 
			
		||||
        }
 | 
			
		||||
        catch ( SQLException error )
 | 
			
		||||
        {
 | 
			
		||||
            // 出现sql错误,就回滚!
 | 
			
		||||
            if ( conn != null)
 | 
			
		||||
            {
 | 
			
		||||
                conn.rollback();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            throw error;
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                if ( stmt != null)
 | 
			
		||||
                {
 | 
			
		||||
                    stmt.close();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch ( Exception error )
 | 
			
		||||
            {
 | 
			
		||||
                error.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                if ( conn != null)
 | 
			
		||||
                {
 | 
			
		||||
                    conn.close();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch ( Exception error )
 | 
			
		||||
            {
 | 
			
		||||
                error.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    static
 | 
			
		||||
    {
 | 
			
		||||
        // 预警级别色彩
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,10 @@
 | 
			
		||||
 | 
			
		||||
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
 | 
			
		||||
 | 
			
		||||
.level = INFO
 | 
			
		||||
 | 
			
		||||
java.util.logging.FileHandler.pattern = ./logs/log_%u.log
 | 
			
		||||
java.util.logging.FileHandler.limit = 50000
 | 
			
		||||
java.util.logging.FileHandler.count = 10
 | 
			
		||||
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
 | 
			
		||||
java.util.logging.FileHandler.append = true;
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 * @Author: Kane
 | 
			
		||||
 * @Date: 2022-04-22 10:53:49
 | 
			
		||||
 * @LastEditors: Kane
 | 
			
		||||
 * @LastEditTime: 2022-04-26 23:16:46
 | 
			
		||||
 * @LastEditTime: 2022-05-10 11:33:41
 | 
			
		||||
 * @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java
 | 
			
		||||
 * @Description:
 | 
			
		||||
 *
 | 
			
		||||
@@ -12,11 +12,10 @@ package com.cpic.xim.wechat.officalAccount;
 | 
			
		||||
 | 
			
		||||
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
import com.cpic.xim.httpUtil.*;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
//import java.text.SimpleDateFormat;
 | 
			
		||||
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
@@ -61,7 +60,7 @@ public class sendMessageTest
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    // @Test
 | 
			
		||||
    public void testSendWeatherDisasterWarning() throws IOException
 | 
			
		||||
    {
 | 
			
		||||
        String warningJSON =
 | 
			
		||||
@@ -73,14 +72,29 @@ public class sendMessageTest
 | 
			
		||||
            QWeatherDisasterWarning warning =
 | 
			
		||||
                    mapper.readValue( warningJSON, QWeatherDisasterWarning.class );
 | 
			
		||||
 | 
			
		||||
            // SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
 | 
			
		||||
            // String putTime = format.format( warning.getUpdateTime() );
 | 
			
		||||
            String city = "东山县";
 | 
			
		||||
 | 
			
		||||
            sendMessage.sendWeatherDisasterWarning( url, warning );
 | 
			
		||||
 | 
			
		||||
            sendMessage.saveWeatherDisasterWarning( city, warning );
 | 
			
		||||
        }
 | 
			
		||||
        catch ( Exception error )
 | 
			
		||||
        {
 | 
			
		||||
            fail( error.getMessage() );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // @Test
 | 
			
		||||
    public void testCheckWarningHasSended()
 | 
			
		||||
    {
 | 
			
		||||
        String warningID = "10123060820220422061200476313081";
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            sendMessage.checkWarningHasSended( warningID );
 | 
			
		||||
        }
 | 
			
		||||
        catch ( Exception error )
 | 
			
		||||
        {
 | 
			
		||||
            error.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user