93 lines
3.2 KiB
Java
93 lines
3.2 KiB
Java
package cn.ac.iie.service;
|
|
|
|
import cn.ac.iie.config.ApplicationConfig;
|
|
import cn.ac.iie.dao.BaseMariaDB;
|
|
import cn.ac.iie.dao.FqdnFile;
|
|
import cn.ac.iie.utils.BrightCloudUtils;
|
|
import cn.ac.iie.utils.FileUtils;
|
|
import cn.ac.iie.utils.LogUtils;
|
|
import cn.ac.iie.utils.MariaDBConnect;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import org.apache.log4j.Logger;
|
|
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStreamWriter;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.sql.Connection;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.TimerTask;
|
|
|
|
/**
|
|
* @author yjy
|
|
* @version 1.0
|
|
* @date 2021/2/25 11:29 上午
|
|
*/
|
|
public class UpdateTask extends TimerTask {
|
|
private static final Logger LOG = Logger.getLogger(UpdateTask.class);
|
|
private static final LogUtils logutils = new LogUtils();
|
|
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
runTask();
|
|
} catch (SQLException | IOException exception) {
|
|
exception.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void runTask() throws SQLException, IOException {
|
|
LOG.info("Start update task ...");
|
|
List<String> updateFqdns = new ArrayList<>();
|
|
|
|
Connection mariaConn = MariaDBConnect.getConnection();
|
|
Statement mariaStat = mariaConn.createStatement();
|
|
BaseMariaDB mariaDB = new BaseMariaDB(mariaConn, mariaStat);
|
|
BrightCloudUtils brightCloudUtils = new BrightCloudUtils();
|
|
List<String> queryTypes = BrightCloudUtils.getQueryTypes();
|
|
|
|
// expired records
|
|
ResultSet expiredSet = mariaDB.getExpiredRecord();
|
|
while (expiredSet.next()) {
|
|
updateFqdns.add(expiredSet.getString("fqdn"));
|
|
}
|
|
long expiredNum = updateFqdns.size();
|
|
|
|
// unlabelled records
|
|
ResultSet unlabeledSet = mariaDB.getUnlabelRecord();
|
|
while (unlabeledSet.next()) {
|
|
updateFqdns.add(unlabeledSet.getString("fqdn"));
|
|
}
|
|
long unlabeledNum = updateFqdns.size() - expiredNum;
|
|
|
|
if (updateFqdns.size()>0){
|
|
JSONObject jsonObj = brightCloudUtils.getQueryResults(updateFqdns);
|
|
List<FqdnFile> updateFiles = brightCloudUtils.responseSparse(jsonObj);
|
|
mariaDB.updateRecords(updateFiles);
|
|
MariaDBConnect.close(mariaStat, mariaConn);
|
|
|
|
// 打印处理结果至日志
|
|
LOG.info("[UpdateTask]-update records: " + expiredNum +" expired records, " + unlabeledNum + " unlabeled or failed-query records");
|
|
|
|
// 打印查询操作记录日志
|
|
OutputStream bcQueryLogStream = new FileOutputStream(ApplicationConfig.LOG_BC_QUERY_REPORT_FILE, true);
|
|
OutputStreamWriter bcQueryLogWriter = new OutputStreamWriter(bcQueryLogStream, StandardCharsets.UTF_8);
|
|
for (String type : queryTypes) {
|
|
java.sql.Date d = new java.sql.Date(System.currentTimeMillis());
|
|
bcQueryLogWriter.write(d + "," + "UpdateTask," + null + "," + type + "," + updateFqdns.size() + "\n");
|
|
}
|
|
|
|
FileUtils.writerClose(bcQueryLogWriter, bcQueryLogStream);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|