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

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;


import com.eactive.eai.batch.scheduler.ScheduleHandlerProcess;
import com.eactive.eai.batch.scheduler.ScheduleRecvProcess;

import com.eactive.eai.common.util.Logger;


public class TimerEventListenerQueueConsumer  extends AbstractQueueConsumer {
	
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	
	static final String PROC_JOB = "요구송수신작업수행";
	static final String REG_JOB = "요구수신작업등록";
	static final String PROC_JOB_CLASS = "com.eactive.eai.batch.scheduler.ScheduleHandlerProcess";
	static final String REG_JOB_CLASS = "com.eactive.eai.batch.scheduler.ScheduleRecvProcess";

	public TimerEventListenerQueueConsumer(String name, Connection con, String queue) {
		super(name, con, queue);
	}
	

	
	@Override
	public void excute() {
		
		String messageStr = null;
		try {
			Message message = consumer.receive(2000);

			ObjectMessage omsg = null;

			if (message == null) {
				// logger.warn(name + " -> EMPTY MESSAGE...");
				return;
			}

			if (message instanceof ObjectMessage) {
				omsg = (ObjectMessage) message;
			} else {
				throw new RuntimeException(name + " Invlid JMS Message Type. - " + message);
			}

			try {
				messageStr = (String) omsg.getObject();
			} catch (Exception e) {
				throw new RuntimeException(name + " Invlid value object in ObjectMessage. - " + e.getMessage());
			}

			try {
				if (messageStr.equals(PROC_JOB)) {
					ScheduleHandlerProcess process = new ScheduleHandlerProcess();
					process.clientRequest();
				} else if (messageStr.equals(REG_JOB)) {
					ScheduleRecvProcess process = new ScheduleRecvProcess();
					process.clientRequest();
				} else {
					logger.error(this.getClass().getName() + "[Message]:" + message);
				}
			} catch (Exception e) {
				logger.error("프로세스 수행 중 에러 발생. [" + message + "]- " + e.getMessage());
			}

		} catch (IllegalStateException ise) {
			logger.error(name + " IllegalStateException = " + ise.getMessage(), ise);

		} catch (JMSException e) {
			logger.error(name + " JMSException = " + e.getMessage(), e);
			run = false;
		} catch (RuntimeException e) {
			logger.error(name + " RuntimeException = " + e.getMessage(), e);
		}
	}
	

}
