package com.sagacloud.broadcast;

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

import com.sagacloud.utils.OssUtils;

/**
 * Created by wo on 9/11/17.
 */
public class MessageHandler {
	private static final Logger log = LoggerFactory.getLogger(MessageHandler.class);

    private static MsgHandler msgHandler;
    private static boolean active = true;

    public static void init(){
    	if("false".equals(OssUtils.getProperty("jms.active"))) {
    		active = false;
    	}else {
    		try {
	    		if(OssUtils.getProperty("jms.choice") == null || !OssUtils.getProperty("jms.choice").equalsIgnoreCase("rabbit")){
	    			msgHandler = new ActiveMsgHandler();
	    		}else {
	    			msgHandler = new RabbitMsgHandler();
	    		}
	    		msgHandler.init();
	    		log.info("消息处理服务启动成功!");
    		}catch (Exception e) {
    			log.error("【MsgError】------- 消息处理服务启动失败!!! -------");
				e.printStackTrace();
			}
    	}
    }

    public static void publish(String msg) {
    	if(active) {
    		try {
    			msgHandler.publish(msg);
    		}catch (Exception e) {
    			log.error("【MsgError】--- 消息发送失败 msg={}", msg);
			}
    	}
    }

}
