?? datacollector.java
字號(hào):
package dms.business.collectProcess;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.util.Vector;
import dms.business.sendProcess.CollectionListener;
import dms.business.sendProcess.LogDealer;
import dms.data.LogRecord;
import dms.data.MatchedRecord;
import dms.exception.HistoryFileException;
import dms.exception.HistoryLocationFileException;
import dms.exception.NetworkException;
import dms.exception.SourceFileException;
import dms.exception.StoreFileException;
import dms.util.DataReader;
import dms.util.FileUtil;
/**
* 數(shù)據(jù)采集器
*/
public class DataCollector {
/**
* 采集主方法
* @throws SourceFileException
* @throws HistoryLocationFileException
* @throws HistoryFileException
* @throws StoreFileException
* @throws NetworkException
*/
public void collect() throws SourceFileException,
HistoryLocationFileException,
HistoryFileException,
StoreFileException,
NetworkException {
Vector<LogRecord> logins = new Vector<LogRecord>();
Vector<LogRecord> logouts = new Vector<LogRecord>();
// step1 讀源文件,并映射到本地緩存
String sourceFile = FileUtil.getSourceFile();
DataReader dataReader = new DataReader();
MappedByteBuffer buffer;
try {
buffer = dataReader.mappingLogBuffer(sourceFile);
} catch(FileNotFoundException e){
throw new SourceFileException();
}catch (IOException e) {
throw new SourceFileException();
}
// step1.1 判斷historyFile是否存在
String historyFileName = FileUtil.getHistoryFile();//上次匹配未成功的記錄
if(new File(historyFileName).exists()){
Object obj;
try {
obj = FileUtil.active(historyFileName);
} catch (IOException e) {
throw new HistoryFileException();
} catch (ClassNotFoundException e) {
throw new HistoryFileException();
}
Vector<LogRecord> historyLogins = null;
if(obj instanceof Vector){
historyLogins = (Vector<LogRecord>) obj;
System.out.println("上次匹配失敗的記錄"+ historyLogins.size()+"條");
copyHistoryLogins( historyLogins,logins);//把上次沒匹配上的記錄拷貝到本次Vector,一起匹配
}
}
// step2 解析文件,過濾記錄
DataParser dataParser = new DataParser();
try {
dataParser.parseLogBuffer(buffer, logouts, logins);
} catch (IOException e) {
throw new HistoryLocationFileException();
} catch (ClassNotFoundException e) {
throw new HistoryLocationFileException();
}
// step3 匹配記錄
DataMatcher dataMatcher = new DataMatcher();
Vector<MatchedRecord> matchedRecords;
try {
matchedRecords = dataMatcher.match(logouts, logins);
} catch (IOException e) {
throw new HistoryFileException();
}
// step4 發(fā)送匹配好的記錄
CollectionListener logdealer = new LogDealer();
try {
logdealer.deal(matchedRecords);
} catch (IOException e) {
throw new NetworkException();
} catch (ClassNotFoundException e) {
throw new StoreFileException();
}
}
/**
* 把上次未匹配上的historyLogins,裝入這次logins匹配
* @param historyLogins 上次未匹配上的登錄數(shù)據(jù)
* @param logins 這次登錄數(shù)據(jù)
*/
private void copyHistoryLogins(Vector<LogRecord> historyLogins, Vector<LogRecord> logins) {
if(!historyLogins.isEmpty()){
for(LogRecord login:historyLogins){
logins.addElement(login);
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -