ActivateUnionRest.java 2.75 KB
/**
 * 
 */
package com.yoho.unions.server.restapi;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yoho.service.model.union.request.ActivateUnionRequestBO;
import com.yoho.service.model.union.request.ActivateUnionRequestVO;
import com.yoho.service.model.union.response.ActiveUnionResponseBO;
import com.yoho.unions.common.constant.UnionConstant;
import com.yoho.unions.common.enums.ClientTypeEnum;
import com.yoho.unions.common.utils.SpringContextUtil;
import com.yoho.unions.server.service.MainUnionService;

/**
 * 描述:
 * 激活联盟入口
 * @author ping.huang
 * 2016年3月16日
 */
@Controller
@RequestMapping("/ActivateUnionRest")
public class ActivateUnionRest {

	static Logger log = LoggerFactory.getLogger(ActivateUnionRest.class);
	
	static ExecutorService exe = Executors.newFixedThreadPool(50);
	
	/**
	 * 激活联盟
	 * @param request
	 * @return
	 */
	@RequestMapping("/activateUnion")
	@ResponseBody public ActiveUnionResponseBO activateUnion(ActivateUnionRequestVO vo) {
		log.info("addUnion with param is {}", vo);
		ActivateUnionRequestBO bo = new ActivateUnionRequestBO();
		BeanUtils.copyProperties(vo, bo);
		String clientType = vo.getClient_type();
		if (!ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(clientType) && !ClientTypeEnum.IOS.getName().equalsIgnoreCase(clientType)) {
			log.warn("activateUnion error with param is {}", vo);
			return new ActiveUnionResponseBO(600, "error");
		}
		//多线程处理
		exe.execute(new Runnable() {
			
			@Override
			public void run() {
				MainUnionService service = null;
				if (ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(clientType)) {
					//处理安卓的服务
					for (String str : UnionConstant.andriodServiceList) {
						//捕获异常,不影响后面的联盟
						try {
							service = SpringContextUtil.getBean(str, MainUnionService.class);
							service.activeUnion(bo);
						} catch (Exception e) {
							log.warn("addUnion error with param is {}", vo);
						}
					}
				} else if (ClientTypeEnum.IOS.getName().equalsIgnoreCase(clientType)) {
					//处理iOS的服务
					for (String str : UnionConstant.iOSServiceList) {
						//捕获异常,不影响后面的联盟
						try {
							service = SpringContextUtil.getBean(str, MainUnionService.class);
							service.activeUnion(bo);
						} catch (Exception e) {
							log.warn("addUnion error with param is {}", vo);
						}
					}
				}
			}
		});
		
		
		return new ActiveUnionResponseBO(200, "success");
	}
}