package ca.sebastiendionne.grizzly.queue;

import java.util.concurrent.BlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.sebastiendionne.grizzly.manager.IEchoManager;

public class QueueConsumer implements Runnable {

	private static final Logger s_logger = LoggerFactory.getLogger(QueueConsumer.class);
	
	protected BlockingQueue<QueueItem> quoteBlockingQueue;
	protected IEchoManager manager;
	
	public QueueConsumer(IEchoManager server, BlockingQueue<QueueItem> quoteBlockingQueue){
		this.manager = server;
		this.quoteBlockingQueue = quoteBlockingQueue;
	}
	
	public void process(QueueItem item){
		if(item!=null){
			manager.process(item);
		}
	}
	
	public void run() {
		
		if(s_logger.isDebugEnabled()){
			s_logger.debug("QuoteConsumer started");
		}

		while(!Thread.currentThread().isInterrupted()){
			try {
				QueueItem command = quoteBlockingQueue.take();
				
				if(s_logger.isDebugEnabled()){
					s_logger.debug("     took item=" + command.getQuery());
				}
				
				process(command);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		}
		
		s_logger.info("QuoteConsumer stopped");
	}

}
