一、处理日志读取文件、过滤文件
class="java" name="code">package com.bhu.defender.front.controller;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bhu.basestation.exception.BaseException;
import com.bhu.defender.front.core.service.AlarmProcesserProxy;
import com.bhu.defender.front.core.service.UserServiceProxy;
/**
*
* @ClassName: LogAlarmStatController
* @Description: 处理日志 添加数据到告警统计表st_Alarm_stat
* @author:lijunling 2013-6-17 下午4:22:11
* @version V1.0
*/
public class QuartzJob
{
/**
* 插入数据 开始执行任务的方法
* @throws IOException
*/
public void logInsetAlarmStat() throws IOException
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取上次执行的时间
String lastFileTime="";
Date lastDate = AlarmProcesserProxy.selectMaxCreateTime();
if(null!=lastDate){
lastFileTime = format.format(lastDate);
}
//本次执行的时间
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
Date date = calendar.getTime();
String fileTime = format.format(date);
//判断上次执行日期是否是昨天 ,若是昨天,则只执行今天的操作 ,若非昨天,则从上次执行到今天均执行
//如:此次fileTime=07-04 lastFileTime=07-03 则处理日志07-03.log 和07-04.log
if(!(lastFileTime.equals(fileTime))){
Long LastTime = lastDate.getTime();
Long thisTime = date.getTime();
while(LastTime<thisTime){
Logger logger= LoggerFactory.getLogger("basestation."+format.format(LastTime)+".log");
String filename = logger.getName();
String strPath = "/home/logs/"+filename;
excuteFile(strPath);
//执行完毕后 日期加1
Calendar cd = Calendar.getInstance();
cd.setTime(lastDate);
cd.add(Calendar.DATE, +1);
lastDate = cd.getTime();
LastTime = cd.getTime().getTime();
}
}else{
Logger logger= LoggerFactory.getLogger("basestation."+fileTime+".log");
String filename = logger.getName();
//String filename ="basestation.2013-07-03.log";
//String strPath = "C:/Users/andy/Downloads/"+filename;
String strPath = "/home/logs/"+filename;
excuteFile(strPath);
}
}
/**
* 执行文件
* @param strPath 文件路径
* @throws IOException
*/
private void excuteFile(String strPath) throws IOException{
List <Integer> rowcountList = getRowList(strPath);//获取行的list
List <List> allContentList = new ArrayList<List>();
if(rowcountList.size()!=0){
for(Integer item : rowcountList){
List<String> contentList = readAppointLineNumber(strPath,item);
//若list中的最后一样含有result=0 则表示成功 是需要的数据 若为1则不成功 过滤掉
String lastRowStr = contentList.get(contentList.size()-1);
if(lastRowStr.contains("result=0")){
allContentList.add(contentList);
}
}
}
processDataList(allContentList);
}
/**
* 自动更新密码开始执行
*/
public void autoUpdateRandompsw(){
try {
UserServiceProxy.autoUpdateRandompsw();
} catch (BaseException e) {
e.printStackTrace();
}
}
/**
* 根据行号读取文本内容 返回list
* @param path 文件路径
* @param lineNumber 行号
* @return
* @throws IOException
*/
private List<String> readAppointLineNumber(String path,int lineNumber) throws IOException{
List<String> contentList = new ArrayList<String>();
FileReader file = new FileReader(path);
LineNumberReader reader = new LineNumberReader(file);
String readline = "";
int lines=0;
while(readline != null){
lines++;
readline = reader.readLine();
int sub = lines - lineNumber;
//获取该行号开始的后几行
if(sub>=0 && sub<=7) {
//System.out.println("当前行号为:"+reader.getLineNumber());
//System.out.println("=====s:"+readline);
contentList.add(readline);
}else if(sub>7){
//System.exit(0);
}
}
reader.close();
file.close();
return contentList;
}
/**
* 根据文件名返回存在需要的数值的行号的list
* @param path 文件路径
* @return
* @throws IOException
*/
private List<Integer> getRowList(String path) throws IOException{
FileReader file=new FileReader(path);
List<Integer> rowcountList = new ArrayList<Integer>();//记录行号,第几行有需要的数据
BufferedReader br=new BufferedReader(file);
String temp=null;
int rowCount=0;
while((temp =br.readLine())!=null){
rowCount++;
/*按行读取 行中包含 am=addAlarmRealtime的记录行号*/
Pattern pattern = Pattern.compile("am=addAlarmRealtime");
Matcher matcher = pattern.matcher(temp);
if(matcher.find()){
rowcountList.add(rowCount);
}
}
br.close();
file.close();
return rowcountList;
}
/**
* 根据需要的文本内容解析数据字段
* @param allContentList
*/
private void processDataList(List <List> allContentList){
for(List item:allContentList){
if(item.size()!=0){
String alarmTimeStr = "";
String alarmTypeIdStr = "";
String terminalCodeStr = "";
String terminalPortIdStr = "";
String typeStr = "";
for(int i=0;i<item.size();i++){
String []itemStr = item.get(i).toString().split(":");
if(itemStr.length>1){
if(i == 1){
alarmTimeStr = filterStr(itemStr[1]);
}else if(i == 2){
alarmTypeIdStr = filterStr(itemStr[1]);
}else if(i == 3){
terminalCodeStr = filterStr(itemStr[1]);
}else if(i == 4){
terminalPortIdStr = filterStr(itemStr[1]);
}else if(i == 5){
typeStr = itemStr[1];
}
}
}
alarmTimeStr = alarmTimeStr.substring(0, 10);
AlarmProcesserProxy.logInsertAalrmStat(terminalCodeStr,Integer.parseInt(alarmTypeIdStr),Integer.parseInt(terminalPortIdStr),alarmTimeStr);
}
}
}
/**
* 过滤空格、双引号、单引号
* @param str
* @return
*/
private String filterStr(String str){
str = str.replace("\"", "").replace(",", "").trim();
return str;
}
}