package com.eactive.eai.rms.onl.server.handler;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.eactive.eai.rms.onl.server.repository.Repository;
import com.eactive.eai.rms.onl.vo.JmsMonitorVo;

@Component
public class JmsQueueHandler implements IMessageHandler {
    public static final int TIME_TO_LIVE = 20;

    private static final Logger logger = Logger.getLogger(JmsQueueHandler.class);
    
    @Autowired
    @Qualifier("repository")
    private Repository repository;

    public Class<JmsMonitorVo> targetClass() {
        return JmsMonitorVo.class;
    }

    public void messageReceived( Object message)
            throws Exception {

        JmsMonitorVo vo = (JmsMonitorVo) message;

        String qname = vo.getUdpQueueName();
        String info[] = vo.getUdpInfo();

        Object obj = repository.getMonitorData(vo.getHostName(),
            vo.getInstanceName(), vo.getCategory());
        if (obj == null) {
            vo.setArrayData(qname, info);
            repository.addMonitorData(vo.getHostName(), vo.getInstanceName(),
                vo.getCategory(), vo, TIME_TO_LIVE);
        } else {
            JmsMonitorVo repoVo = (JmsMonitorVo) obj;
            repoVo.setArrayData(qname, info);
            repository.addMonitorData(vo.getHostName(), vo.getInstanceName(),
                repoVo, TIME_TO_LIVE);
        }
        
        if( logger.isDebugEnabled() ) {
            logger.debug("RECV  JmsQueueHandler] qname=" + qname + ", info[5]=" + info[5] );
        }
    }

}
