파일 설명
LogAnalyzerMain.java : 메인
LogAnalyzerThread.java : 로그분석 및 write
LogCreater.java : 로그생성기
LogQueue.java : 데이타구조
LogReaderThread.java : 로그리딩
※ LogAnalyzerMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class LogAnalyzerMain { private static final int THREAD_SLOT = 100; public static void main(String[] args) { LogReaderThread logReader = new LogReaderThread(); ExecutorService logReaderThread = Executors.newSingleThreadExecutor(); logReaderThread.submit(logReader); ExecutorService logAnalyzerThreadPool = Executors.newFixedThreadPool(THREAD_SLOT); for(int i = 0 ; i < THREAD_SLOT ; i++) { logAnalyzerThreadPool.submit(new LogAnalyzerThread()); } logReaderThread.shutdown(); System.out.println("reader shutdown"); //logAnalyzerThreadPool.shutdownNow(); logAnalyzerThreadPool.shutdown(); while(logAnalyzerThreadPool.isTerminated() != true) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("writer shutdown"); File report = new File("./report.txt"); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(report)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } int total_cnt = 0; for(int i = 0 ; i < LogAnalyzerThread.logFileDatas.length ; i++ ) { if(LogAnalyzerThread.logFileDatas[i].logType == null) { continue; } try { bw.write(LogAnalyzerThread.logFileDatas[i].logType + "#" + LogAnalyzerThread.logFileDatas[i].count); bw.newLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.print(LogAnalyzerThread.logFileDatas[i].logType + " : "); System.out.println(LogAnalyzerThread.logFileDatas[i].count); total_cnt += LogAnalyzerThread.logFileDatas[i].count; } System.out.println("Total cnt : " + total_cnt); try { bw.write("Total : " + total_cnt); bw.newLine(); bw.flush(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } | cs |
※ LogAnalyzerThread.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.concurrent.Callable; public class LogAnalyzerThread implements Callable<Object> { public class LogFileData { Object obj = new Object(); File logFile; String logType; BufferedWriter bw; int count; } public static LogFileData[] logFileDatas = new LogFileData[10]; public LogAnalyzerThread() { if(logFileDatas[0] == null) { for(int i = 0 ; i < logFileDatas.length ; i++ ) { logFileDatas[i] = new LogFileData(); } } } @Override public Object call() throws Exception { // TODO Auto-generated method stub while(true) { String message = LogQueue.poll(); if(message != null) { if(message.equals("END")) return null; //System.out.println(message); writeLog(message); } else { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // return null; } public boolean writeLog(String message) { String[] slit = message.split("#"); LogFileData logData = getLogFileData(slit[1]); //System.out.println("LogData : " + logData); if(logData != null) { synchronized (logData.obj) { try { //System.out.println("WRITE : " + message); logData.bw.write(message); logData.bw.newLine(); logData.bw.flush(); logData.count++; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return false; } public LogFileData getLogFileData(String logType) { //System.out.print("LogType : " + logType); for(LogFileData logFileData : logFileDatas) { //System.out.println(" count : " + i++); /* 이곳을 크리티컬 섹션으로 잡지 않으면 logFile이 처음엔 모두 null이므로 Thread가 중복되어 instance를 생성하게 된다. */ if(logFileData.logFile == null) { synchronized(logFileData.obj) { /* block이 걸려있다가 풀렸을 때 이미 클래스를 생성했을 경우 */ if(logFileData.logFile != null) { for(LogFileData logFileData2 : logFileDatas) { if(logType.equals(logFileData2.logType)) { System.out.println("중간에 다른 쓰레드가 instance 생성"); return logFileData; } } } //System.out.println("FileOpen"); logFileData.logFile = new File("./" + logType + ".log"); logFileData.logType = logType; try { logFileData.bw = new BufferedWriter(new FileWriter(logFileData.logFile)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return logFileData; } } else { if(logType.equals(logFileData.logType)) { return logFileData; } } } return null; } @Override protected void finalize() throws Throwable { // TODO Auto-generated method stub super.finalize(); for(LogFileData logFileData : logFileDatas) { if(logFileData.bw != null) { logFileData.bw.flush(); logFileData.bw.close(); logFileData.bw = null; } } } } | cs |
※ LogQueue.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import java.util.LinkedList; import java.util.Queue; public class LogQueue { private static Queue<String> queue = new LinkedList<String>(); class LogCount { String logType; int count; } private static LogCount[] logCount = new LogCount[10]; synchronized public static String poll() { return queue.poll(); } synchronized public static boolean add(String log) { // String[] slplitStr = log.split("#"); // // for(int i = 0 ; i < logCount.length ; i++) { // if(logCount[i].logType != null) { // if(logCount[i].logType.equals(slplitStr[1])) { // logCount[i].count++; // break; // } // }else { // /* 최초 발견시 */ // logCount[i].logType = slplitStr[1]; // logCount[1].count++; // break; // } // } // System.out.println(logCount[0].count); return queue.add(log); } public static int size() { return queue.size(); } } | cs |
※ LogReaderThread.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.concurrent.Callable; public class LogReaderThread implements Callable<Object> { private static File logFile = null; private static BufferedReader br = null; public LogReaderThread() { if(logFile == null) { logFile = new File("./logFile.log"); try { br = new BufferedReader(new FileReader(logFile)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public Object call() throws Exception { String readLine; synchronized (br) { while((readLine = br.readLine()) != null) { LogQueue.add(readLine); } } for(int i = 0 ; i < 100 ; i++) { LogQueue.add("END"); } return null; } } | cs |
※LogCreater.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; public class LogCreater { /* 로그형태 시간#타입#message */ private static Random ran = new Random(); private static final char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); private static final String[] type = {"AA", "BB", "CC", "DD","EE", "FF", "HH"}; public static void main(String[] args) { File logFile = new File("./logFile.log"); try { BufferedWriter bw = new BufferedWriter(new FileWriter(logFile, true)); for(int i = 0 ; i < 1000000 ; i++) { bw.write(String.format("%s#%s#%s", getLogDateTime(), getLogType(), getLogMessage())); bw.newLine(); } bw.flush(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static String getLogType() { return type[ran.nextInt(type.length)]; } private static String getLogMessage() { StringBuilder sb = new StringBuilder(); for(int i = 0 ; i < 10 ; i++) { sb.append(alpha[ran.nextInt(alpha.length - 1)]); } return sb.toString(); } private static String getLogDateTime(){ Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.format(date); } } | cs |