// Path: rabbitmqservice/src/main/java/sync/listener/ESMessageListener.java
// @Component
// public class ESMessageListener implements MessageListener {
//     private static final Logger lightLogger = Logger.getLogger(ESMessageListener.class);
// 
//     private ESHandlerManager handlerManager = null;
//     private ConfigurableApplicationContext context;
// 
//     private final CounterService counterService;
// 
//     private final GaugeService gaugeService;
// 
//     public ESMessageListener(ConfigurableApplicationContext context) {
//         this.counterService = context.getBean("counterService", CounterService.class);
//         this.gaugeService = context.getBean("gaugeService", GaugeService.class);
//         this.context = context;
//         //handlerManager.initHandler(context);
//     }
// 
//     @Override
//     public void onMessage(Message message) {
//         if (handlerManager == null) {
//             handlerManager = new ESHandlerManager();
//             handlerManager.initHandler(context);
//         }
// 
//         doWork(message);
//     }
// 
//     protected void postHandle(Message message) {
//         String messageId = String.valueOf(message.getMessageProperties()
//                 .getHeaders().get(MQMessageConst.H_MESSAGEID));
//         String jobName = String.valueOf(message.getMessageProperties()
//                 .getHeaders().get(MQMessageConst.H_JOBNAME));
//     }
// 
//     public void doWork(Message message) {
//         String routingKey = message.getMessageProperties().getReceivedRoutingKey();
//         Matcher m = ESConstants.ROUTINGKEY_PATTERN.matcher(routingKey);
//         try {
//             if (m.find() && m.groupCount() == 3) {
//                 ESHandleMessage esHandleMessage = new ESHandleMessage(Long.parseLong(m.group(3)),
//                         m.group(1), m.group(2), message.getBody());
//                 esHandleMessage.setEventGenerated(isGeneratedMessage(message));
//                 handleMsg(esHandleMessage);
//             } else {
//                 lightLogger.warn("Message is invalid: " + routingKey);
//             }
//         } finally {
//             lightLogger.info("Done");
//         }
// 
//     }
// 
//     private boolean isGeneratedMessage(Message message) {
//         return ObjectUtils.equals("EventGenerator", message.getMessageProperties().getHeaders().get("eventSource"));
//     }
// 
//     private void handleMsg(ESHandleMessage message) {
//         StopWatch stopWatch = new StopWatch();
//         stopWatch.start();
//         boolean successful = false;
// 
//         lightLogger.info("ES sync start on ESHandleMessage " + message.toString());
//         ESHandler handler = null;
//         try {
//             handler = handlerManager.getHandler(message);
//             if (handler == null) {
//                 lightLogger.info("Handler is null for message" + message.toString());
//                 return;
//             }
// 
//             handler.onMessage(message);
// 
//             successful = true;
//         } catch (Throwable t) {
//             successful = false;
//             throw t;
//         } finally {
//             stopWatch.stop();
//             String metricName = message.getClass().getSimpleName() + "." + handler.getClass().getSimpleName();
//             //counterService.increment(metricName + "." + (successful ? ".success" : "failed"));
//             //gaugeService.submit(metricName, stopWatch.getTime());
//             lightLogger.info("ES sync end on execTime[" + stopWatch.getTime() + "]");
//         }
//     }
// 
// //    @Override
//     public String[] getRoutingKeys() {
//         List<String> routingList = handlerManager.getRoutingKeyList();
//         return routingList.toArray(new String[routingList.size()]);
//     }
// 
//     public void setHandlerManager(ESHandlerManager handlerManager) {
//         this.handlerManager = handlerManager;
//     }
// }


import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jca.cci.connection.SingleConnectionFactory;
import sync.listener.ESMessageListener;

package rabbitmq;


/**
 * Created by I311352 on 12/28/2016.
 */

public class ListenerJobBuilder {
    // just everything under default
    public static MQListenerAdmin buildMQListenerAdmin(ConfigurableApplicationContext context) {

// Path: rabbitmqservice/src/main/java/sync/listener/ESMessageListener.java
// @Component
// public class ESMessageListener implements MessageListener {
//     private static final Logger lightLogger = Logger.getLogger(ESMessageListener.class);
// 
//     private ESHandlerManager handlerManager = null;
//     private ConfigurableApplicationContext context;
// 
//     private final CounterService counterService;
// 
//     private final GaugeService gaugeService;
// 
//     public ESMessageListener(ConfigurableApplicationContext context) {
//         this.counterService = context.getBean("counterService", CounterService.class);
//         this.gaugeService = context.getBean("gaugeService", GaugeService.class);
//         this.context = context;
//         //handlerManager.initHandler(context);
//     }
// 
//     @Override
//     public void onMessage(Message message) {
//         if (handlerManager == null) {
//             handlerManager = new ESHandlerManager();
//             handlerManager.initHandler(context);
//         }
// 
//         doWork(message);
//     }
// 
//     protected void postHandle(Message message) {
//         String messageId = String.valueOf(message.getMessageProperties()
//                 .getHeaders().get(MQMessageConst.H_MESSAGEID));
//         String jobName = String.valueOf(message.getMessageProperties()
//                 .getHeaders().get(MQMessageConst.H_JOBNAME));
//     }
// 
//     public void doWork(Message message) {
//         String routingKey = message.getMessageProperties().getReceivedRoutingKey();
//         Matcher m = ESConstants.ROUTINGKEY_PATTERN.matcher(routingKey);
//         try {
//             if (m.find() && m.groupCount() == 3) {
//                 ESHandleMessage esHandleMessage = new ESHandleMessage(Long.parseLong(m.group(3)),
//                         m.group(1), m.group(2), message.getBody());
//                 esHandleMessage.setEventGenerated(isGeneratedMessage(message));
//                 handleMsg(esHandleMessage);
//             } else {
//                 lightLogger.warn("Message is invalid: " + routingKey);
//             }
//         } finally {
//             lightLogger.info("Done");
//         }
// 
//     }
// 
//     private boolean isGeneratedMessage(Message message) {
//         return ObjectUtils.equals("EventGenerator", message.getMessageProperties().getHeaders().get("eventSource"));
//     }
// 
//     private void handleMsg(ESHandleMessage message) {
//         StopWatch stopWatch = new StopWatch();
//         stopWatch.start();
//         boolean successful = false;
// 
//         lightLogger.info("ES sync start on ESHandleMessage " + message.toString());
//         ESHandler handler = null;
//         try {
//             handler = handlerManager.getHandler(message);
//             if (handler == null) {
//                 lightLogger.info("Handler is null for message" + message.toString());
//                 return;
//             }
// 
//             handler.onMessage(message);
// 
//             successful = true;
//         } catch (Throwable t) {
//             successful = false;
//             throw t;
//         } finally {
//             stopWatch.stop();
//             String metricName = message.getClass().getSimpleName() + "." + handler.getClass().getSimpleName();
//             //counterService.increment(metricName + "." + (successful ? ".success" : "failed"));
//             //gaugeService.submit(metricName, stopWatch.getTime());
//             lightLogger.info("ES sync end on execTime[" + stopWatch.getTime() + "]");
//         }
//     }
// 
// //    @Override
//     public String[] getRoutingKeys() {
//         List<String> routingList = handlerManager.getRoutingKeyList();
//         return routingList.toArray(new String[routingList.size()]);
//     }
// 
//     public void setHandlerManager(ESHandlerManager handlerManager) {
//         this.handlerManager = handlerManager;
//     }
// }


// Path: rabbitmqservice/src/main/java/rabbitmq/ListenerJobBuilder.java
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jca.cci.connection.SingleConnectionFactory;
import sync.listener.ESMessageListener;

package rabbitmq;


/**
 * Created by I311352 on 12/28/2016.
 */

public class ListenerJobBuilder {
    // just everything under default
    public static MQListenerAdmin buildMQListenerAdmin(ConfigurableApplicationContext context) {

        MessageListenerProxy listenerProxy = new MessageListenerProxy(new ESMessageListener(context),
