package it.mice.voila.runtime.taskexecutor;


import org.apache.log4j.Logger;
import org.springframework.jms.listener.DefaultMessageListenerContainer;


public class DefaultMessageListenerContainerPoolMonitorService implements IDefaultMessageListenerContainerMonitorService {
	
	private static Logger log = Logger.getLogger(DefaultMessageListenerContainerPoolMonitorService.class);
	
	

	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());
		strBuff.append(" \n- Number of currently active consumers: ").append(container.getActiveConsumerCount()+"\n");
		strBuff.append(" - Currently configured maxConcurrentConsumers value: ").append(container.getMaxConcurrentConsumers()+"\n");
		strBuff.append(" - Currently Configured ConcurrentConsumers: ").append(container.getConcurrentConsumers()+"\n");
		strBuff.append(" - Scheduled Consumer Count: ").append(container.getScheduledConsumerCount()+"\n");
		strBuff.append(" - Limit for the number of idle consumers: ").append(container.getIdleConsumerLimit()+"\n");
		strBuff.append(" - Limit for idle executions of a consumer task.: ").append(container.getIdleTaskExecutionLimit()+"\n");
		strBuff.append(" - Maximum number of messages to process in one task: ").append(container.getMaxMessagesPerTask()+"\n");
		
		
		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;

	DefaultMessageListenerContainer container;
	public DefaultMessageListenerContainer getDefaultMessageListenerContainer() {
		// TODO Auto-generated method stub
		return container;
	}

	public void setDefaultMessageListenerContainer(
			DefaultMessageListenerContainer theDefaultMessageListenerContainer) {
		container=theDefaultMessageListenerContainer;
		
	}
}
