拷貝日誌副本,刪除原有日誌,避免重複請求接口導致數據重複寫入deleteErrorFileAndCopy

package com.alibaba.algo.controller.cpwController;


import com.alibaba.algo.model.CpwLogErrorDO;
import com.alibaba.newcpw.FileOperate.FileCopy;
import com.alibaba.newcpw.FileOperate.FileRead;
import com.alibaba.newcpw.service.CpwLogErrorService;
import com.alibaba.newcpw.FileOperate.FileWrite;
import com.alibaba.newcpw.component.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;

import static com.alibaba.newcpw.FileOperate.FileWrite.pathAll;

@Controller
public class CpwLogErrorController {
    public static String pathError = FileRead.pathError;

    @Autowired
    CpwLogErrorService cpwLogErrorServer;
    @Autowired
    CpwLogErrorController cpwLogErrorController;

    // 注意這裏是RequestMapping  後面的內容爲接口路徑。此接口爲logError,每個參數錯誤日誌斷言
    @RequestMapping("/logerror")
    @ResponseBody
    public CpwResultModel runProcessTask() {
        return parseData(pathError, true);
    }

    // 讀取path路徑的txt文件
    public CpwResultModel parseData(String path, Boolean isError) {

        // 文件讀取必須得用try catch,文件按行讀取
        // 按行處理數據,一行行插入數據庫
        int count = 0;

        try {
            File file = new File(path);
            BufferedReader br = new BufferedReader(new FileReader(file));//構造一個BufferedReader類來讀取文件
            String s;

            while ((s = br.readLine()) != null) {//使用readLine方法,一次讀一行
                String result = s.trim();//去空白,末尾或者開頭。去空格
                if (result.equals("")) {
                    continue;
                }
                String[] array = result.split(FileWrite.rex);
                count++;
                if (isError) {
                    errorLogInsert(array);
                }

            }
            br.close();

            // 拷貝error_log.txt爲error_log_copy.txt副本,刪除控制檯運行出原始日誌error_log.txt,防止重複請求接口導致數據重複寫入
            deleteErrorFileAndCopy();

            CpwResultModel cpwResultModel = new CpwResultModel();

            cpwResultModel.setCode(Constants.CODE_SUCCESS);
            cpwResultModel.setResult("success");
            cpwResultModel.setCount(count);

            if (count == 0) {
                cpwResultModel.setError("暫無錯誤日誌~~");
            }

            return cpwResultModel;

        } catch (Exception e) {
            e.printStackTrace();
            CpwResultModel cpwResultModel = new CpwResultModel();

            cpwResultModel.setCode(Constants.CODE_ERROR);
            cpwResultModel.setResult("failed");
            cpwResultModel.setCount(count);
            cpwResultModel.setError(e.toString());
            return cpwResultModel;
        }
    }

    // 原始日誌error_log.txt解析後字段入庫
    private void errorLogInsert(String[] array) {
        String nowTime = array[0];
        String logTime = array[1];
        String logId = array[2];
        String errorLog = array[3];
        String errorLevel = array[4];

        // 把字段解析到cpw_log_param數據表

        CpwLogErrorDO cpwLogErrorDO = new CpwLogErrorDO();

        // 自增ID不需要插入,無需以下代碼,cpwLogErrorDO.setId((long)1);

        cpwLogErrorDO.setGmtCreate(new Date());
        cpwLogErrorDO.setGmtModified(new Date());
        cpwLogErrorDO.setNowTime(nowTime);
        cpwLogErrorDO.setLogTime(logTime);
        cpwLogErrorDO.setLogId(Integer.parseInt(logId));
        cpwLogErrorDO.setErrorLog(errorLog);
        cpwLogErrorDO.setErrorLevel(Integer.parseInt(errorLevel));

        cpwLogErrorServer.insertData(cpwLogErrorDO);

    }

    private void deleteErrorFileAndCopy(){
        // 請求接口日誌入庫後,拷貝本地error_log_txt到error_log_copy.txt 每次日誌運行做備份
        File error_log = new File(pathAll + File.separator + "error_log.txt");
        File error_log_copy = new File(pathAll + File.separator + "error_log_copy.txt");

        try {
            if (error_log_copy.exists()) {
                error_log_copy.delete();
            }
            System.out.println("可能沒有這個文件,兼容代碼===錯誤日誌副本已刪除。delete error_log_copy.txt file ");

        } catch (Exception e) {
            e.printStackTrace();
        }

        // 拷貝副本,同步數據
        FileCopy.copyFileUsingJava7Files(error_log, error_log_copy);
        System.out.println("error_log_copy同步完成");

        // 刪除正本數據,保留副本數據
        FileWrite.deleteErrorLogFirstRunFile();

    }
}

 

待續。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章