ログ管理設計クラス

2749 ワード

public class LogDataQueue {
	private static Queue<Object> logDataQueue = null;
	
	public static Queue<Object> getLogDataQueue(){
		if(logDataQueue == null){
			logDataQueue = new    ConcurrentLinkedQueue<Object>();// 
		}
		return logDataQueue;
	}
	
}




 
public interface ILogPersistance {
	public boolean saveLog(Object obj);
}



public class LogPersistanceService implements ILogPersistance {
	private static final Logger log = Logger.getLogger(LogPersistanceService.class);
	@Override
	public boolean saveLog(Object obj) {
    
		LogToDBProcessor logToDBProcessor = DaoServiceProvider.getLogToDBProcessor();// BEAN, bean
		log.info(" ,obj:"+obj);
		logToDBProcessor.saveLog(obj);
		return true;
	}

}




//  
public class LogToDBProcessor implements Runnable{
	private static final Logger log = Logger.getLogger(LogToDBProcessor.class);
	private Queue<Object> queue = LogDataQueue.getLogDataQueue();
	
	public synchronized void saveLog(Object log)
	{              // 
			LogDataQueue.getLogDataQueue().add(log);
	}
	
	public void run() {
		while(true){
			try {
				Thread.sleep(10000);
				this.process();
			} catch (Exception e) {
				log.info(" :"+e.getMessage());
			}
		}		
	}
	
	private void process(){
		int currentSize = queue.size();
		if(currentSize!=0)
		{
			List<Object> list = new ArrayList<Object>();
			if(currentSize > 100){			
				for(int i=0; i<100; i++){
// 	 		
					list.add(queue.poll());
				}
			}else {
				for (int i = 0; i < queue.size(); i++) {
  
					list.add(queue.poll());
				}
			}
			this.toDb(list);
		}
	}
// 	
	private void toDb(List<Object> list){
// 
		GenericPersistenceService genericPersistenceService = (GenericPersistenceService)ApplicationContextProvider.getBean("genericPersistenceService");
		log.info(" :list.size="+list.size());
		for(Object o : list){
// 
			if(o instanceof Logininfo && "1".equals(((Logininfo)o).getFlag())){
				genericPersistenceService.update(o);
			}else{
				genericPersistenceService.save(o);
			}
		}
		//genericPersistenceService.saveBatch(list);
	}	
		
}