package it.mice.voila.runtime.taskexecutor;


import org.apache.log4j.Logger;


public class ThreadPoolMonitorService implements IThreadPoolMonitorService {
	
	private static Logger log = Logger.getLogger(ThreadPoolMonitorService.class);
	ThreadPoolTaskExecutor executor;
	public ThreadPoolTaskExecutor getExecutor() {
		return executor;
	}

	public void setExecutor(ThreadPoolTaskExecutor executor) {
		this.executor = executor;
	}

	private long monitoringPeriod; 
	
	public void run() {
		try {
			while (!isRequestedStop() && true){
				monitorThreadPool();
				Thread.sleep(monitoringPeriod*1000);
			} 
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}
	
	public void monitorThreadPool() {
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("CurrentPoolSize : ").append(executor.getThreadPoolExecutor().getPoolSize());
		strBuff.append(" - CorePoolSize : ").append(executor.getThreadPoolExecutor().getCorePoolSize());
		strBuff.append(" - MaximumPoolSize : ").append(executor.getThreadPoolExecutor().getMaximumPoolSize());
		strBuff.append(" - ActiveTaskCount : ").append(executor.getThreadPoolExecutor().getActiveCount());
		strBuff.append(" - CompletedTaskCount : ").append(executor.getThreadPoolExecutor().getCompletedTaskCount());
		strBuff.append(" - TotalTaskCount : ").append(executor.getThreadPoolExecutor().getTaskCount());
		strBuff.append(" - isTerminated : ").append(executor.getThreadPoolExecutor().isTerminated());

		log.info(strBuff.toString());
	}
	
	
	
	public long getMonitoringPeriod() {
		return monitoringPeriod;
	}

	public void setMonitoringPeriod(long monitoringPeriod) {
		this.monitoringPeriod = monitoringPeriod;
	}

	/**
	 * @param requestedStop the requestedStop to set
	 */
	public void setRequestedStop(boolean requestedStop) {
		this.requestedStop = requestedStop;
	}

	/**
	 * @return the requestedStop
	 */
	public boolean isRequestedStop() {
		return requestedStop;
	}

	private  boolean requestedStop=false;
}
