package com.eactive.eai.common.routing.jms;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jms.Connection;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import com.eactive.eai.common.dao.Keys;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.common.util.ServiceLocator;

public class TimerEventListenerQueueConsumerService  implements InitializingBean, DisposableBean {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	// uri = "vm://localhost?broker.persistent=false&broker.useJmx=false"
	// uri = "tcp://localhost:61617""
	private String uri;
	private String queue;
	private int maxThread; 
	
	String serverName = System.getProperty(Keys.SERVER_KEY);
	
	Connection connection = null;
	Session[] sessions = null;
	AbstractQueueConsumer[] consumers = null;
	
	ExecutorService es = null; 
			
	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		if("bapSvr11".equals(serverName)) {
			uri = uri.replace("21", "11");
		}else {
			uri = uri.replace("11", "21");
		}
		this.uri = uri;
	}

	public String getQueue() {
		return queue;
	}

	public void setQueue(String queue) {
		if("bapSvr11".equals(serverName)) {
			queue = queue.replace("21", "11");
		}else {
			queue = queue.replace("11", "21");
		}
		this.queue = queue;
	}

	public int getMaxThread() {
		return maxThread;
	}

	public void setMaxThread(int maxThread) {
		this.maxThread = maxThread;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		startConsumer();	
	}
	
	@Override
	public void destroy() throws Exception {
		stopConsumer();
	}
	
	public void startConsumer() {
		try {
			es = Executors.newFixedThreadPool(maxThread);
			
            ServiceLocator locator = ServiceLocator.getInstance();  
            
            QueueConnectionFactory connectionFactory = locator.getQueueConnectionFactory(uri);
            connection = connectionFactory.createQueueConnection();
			
            consumers = new AbstractQueueConsumer[maxThread];
            
	        for(int i=0; i<maxThread; i++) {
	        	
	        	
	        	AbstractQueueConsumer consumer = new TimerEventListenerQueueConsumer("JMS-" +i, connection, queue);
	        	es.submit(consumer);
	        	consumers[i] = consumer;
	        }
	        logger.debug("Start JMS QueueListener  [ uri=" + uri + 
	        		", queue="+queue + ", maxthread=" + maxThread+"] and wait for listening");
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void stopConsumer() {
		
		for(int i=0; i<maxThread; i++) {
			try {
				consumers[i].stop();
			} catch (Exception e) {
				logger.warn(e.getMessage());
			}
		}
		
		if(es != null) {
			es.shutdown();
		}
		es = null;
		
	}
	
	public static void main(String[] argv) throws Exception {
		TimerEventListenerQueueConsumerService service = new TimerEventListenerQueueConsumerService();
		service.setUri("vm://localhost?broker.persistent=false&broker.useJmx=false");
		service.setQueue("TESTQ1");
		service.setMaxThread(10);
		service.startConsumer();
		
		Thread.sleep(2 * 1000);
		
		service.stopConsumer();
		
		Thread.sleep(2 * 1000);
	}
}
