Authored by unknown

去掉memcache

... ... @@ -5,7 +5,7 @@
<parent>
<groupId>com.yoho</groupId>
<artifactId>parent</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.3.5-SNAPSHOT</version>
</parent>
<groupId>com.yoho.dsf</groupId>
<artifactId>yoho-unions</artifactId>
... ...
package com.yoho.activity.thread;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.netflix.config.DynamicLongProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.yoho.core.cache.CacheClient;
import com.yoho.core.redis.YHValueOperations;
public class DrawServiceThread {
static Logger log = LoggerFactory.getLogger(DrawServiceThread.class);
private static final String DRAW_USER_LIST_KEY = "DRAW_USER_LIST_KEY";
private static final String DRAW_USER_LIST_MEM_KEY = "DRAW_USER_LIST_MEM_KEY";
@Resource
CacheClient cacheClient;
@Resource
YHValueOperations<String, String> valueOperations;
String fileName = "/Data/logs/union/redis.txt";
// String fileName = "D:/redis.txt";
/**
* 启动一个定时任务,来读取元宵节抽签活动的
*/
public DrawServiceThread() {
//tomcat启动后,延迟一分钟,每个一个小时写一次文件
DynamicLongProperty delay = DynamicPropertyFactory.getInstance().getLongProperty("draw.writeFile.delay", 60);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
log.info("enter DrawServiceThread");
//cong redis中获取数据
String arr = valueOperations.get(DRAW_USER_LIST_KEY);
Map<String, JSONObject> maps = null;
if (StringUtils.isEmpty(arr)) {
maps = new HashMap<String, JSONObject>();
} else {
maps = (Map) JSONObject.parse(arr);
}
log.debug("DrawServiceThread query redis data total is {}", maps.size());
//从mem里面获取
Map<String, String> memMap = cacheClient.get(DRAW_USER_LIST_MEM_KEY, Map.class);
if (memMap == null) {
memMap = new HashMap<String, String>();
}
log.debug("DrawServiceThread query mem data total is {}", memMap.size());
List<String> l = new ArrayList<String>();
for (Entry<String, JSONObject> entry : maps.entrySet()) {
if (StringUtils.isNotEmpty(memMap.get(entry.getKey()))) {
//该用户已经保存过文件,则不在进行重复写
continue;
}
l.add(entry.getValue().toJSONString());
try {
FileUtils.writeLines(new File(fileName), "UTF-8", l, true);
log.debug("DrawServiceThread success write file with key={}, value={}", entry.getKey(), entry.getValue());
} catch (IOException e) {
log.error("write file error with key={}, value={}", entry.getKey(), entry.getValue(), e);
}
//往mem里面记录,该用户已经写过文件的标记
memMap.put(entry.getKey(), "1");
}
cacheClient.set(DRAW_USER_LIST_MEM_KEY, 30 * 24 * 60 * 60, memMap);
log.info("success out DrawServiceThread");
}
}, 1, delay.get(), TimeUnit.MINUTES);
}
}
... ... @@ -37,53 +37,53 @@ public class UnionRest {
@Resource
IUserOrdersService userOrdersService;
/**
* 添加顶当
* @param req
* @return
*/
@RequestMapping(value = "/addUnion")
@ResponseBody public JSONObject addUnion(AddUnionRequestBO req) {
log.info("enter unionsRest.addunions with param is {}", req);
JSONObject response = null;
try {
response = dDServiceImpl.addUnion(req);
} catch (Exception e) {
log.error("addunions error with param is {}", req, e);
response = new JSONObject();
response.put("isSuccess", false);
response.put("msg", "error");
}
return response;
}
// /**
// * 添加顶当
// * @param req
// * @return
// */
// @RequestMapping(value = "/addUnion")
// @ResponseBody public JSONObject addUnion(AddUnionRequestBO req) {
// log.info("enter unionsRest.addunions with param is {}", req);
// JSONObject response = null;
// try {
// response = dDServiceImpl.addUnion(req);
// } catch (Exception e) {
// log.error("addunions error with param is {}", req, e);
// response = new JSONObject();
// response.put("isSuccess", false);
// response.put("msg", "error");
// }
// return response;
// }
/**
* 激活顶当
* @param req
* @return
*/
@RequestMapping(value = "/activeUnion")
@ResponseBody public ActiveUnionResponseBO activeUnion(ActiveUnionRequestBO req) {
log.info("enter unionsRest.activeunions with param is {}", req);
ActiveUnionResponseBO response = new ActiveUnionResponseBO();
try {
UnionResponseBO bo = dingdangService.activeUnion(req);
if (!bo.getIsSuccess()) {
response.setCode(400);
response.setMessage(bo.getMsg());
} else {
response.setCode(200);
response.setMessage("success");
response.setData(bo);
}
} catch (Exception e) {
log.error("", e);
response.setCode(500);
response.setMessage("error");
}
return response;
}
// /**
// * 激活顶当
// * @param req
// * @return
// */
// @RequestMapping(value = "/activeUnion")
// @ResponseBody public ActiveUnionResponseBO activeUnion(ActiveUnionRequestBO req) {
// log.info("enter unionsRest.activeunions with param is {}", req);
// ActiveUnionResponseBO response = new ActiveUnionResponseBO();
// try {
// UnionResponseBO bo = dingdangService.activeUnion(req);
// if (!bo.getIsSuccess()) {
// response.setCode(400);
// response.setMessage(bo.getMsg());
// } else {
// response.setCode(200);
// response.setMessage("success");
// response.setData(bo);
// }
// } catch (Exception e) {
// log.error("", e);
// response.setCode(500);
// response.setMessage("error");
// }
// return response;
// }
@RequestMapping(value ="/addBigData")
@ResponseBody
... ...
... ... @@ -19,18 +19,18 @@ public interface DingdangService {
* @return
* @throws Exception
*/
public UnionResponseBO addUnion(AddUnionRequestBO request) throws Exception;
/**
* 联盟激活接口
* @param request
* @return
* @throws Exception
*/
public UnionResponseBO activeUnion(ActiveUnionRequestBO request) throws Exception;
// public UnionResponseBO addUnion(AddUnionRequestBO request) throws Exception;
//
// /**
// * 联盟激活接口
// * @param request
// * @return
// * @throws Exception
// */
//
//
//
// public UnionResponseBO activeUnion(ActiveUnionRequestBO request) throws Exception;
UnionResponseBO addBigData(int beginTime,int endTime);
... ...
... ... @@ -42,7 +42,7 @@ import java.util.concurrent.TimeUnit;
* @author ping.huang
* 2016年3月16日
*/
@Service("dDServiceImpl")
//@Service("dDServiceImpl")
public class DDServiceImpl implements MainUnionService {
static Logger log = LoggerFactory.getLogger(DDServiceImpl.class);
... ...
... ... @@ -41,8 +41,8 @@ public class DingdangServiceImpl implements DingdangService {
static Logger ipMatch = LoggerFactory.getLogger("ipMatch");
@Resource
CacheClient cacheClient;
// @Resource
// CacheClient cacheClient;
@Resource
IUnionsActiveRecordDAO unionsActiveRecordDAO;
... ... @@ -69,181 +69,181 @@ public class DingdangServiceImpl implements DingdangService {
static final String yasUrl = "http://analytics.m.yohobuy.com/yas.gif/web/";
@Override
public UnionResponseBO addUnion(AddUnionRequestBO request) throws Exception {
log.debug("addunions with param is {}", request);
UnionResponseBO bo = new UnionResponseBO();
if (StringUtils.isEmpty(request.getApp())) {
log.warn("addunions error app is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("APP IS NULL");
return bo;
// throw new ServiceException(ServiceError.APP_IS_NULL);
}
if (StringUtils.isEmpty(request.getUdid())) {
log.warn("addunions error udid is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("UDID IS NULL");
return bo;
// throw new ServiceException(ServiceError.UDID_IS_NULL);
}
if (StringUtils.isEmpty(request.getCallbackurl())) {
log.warn("addunions error callbackurl is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("CALLBACKURL IS NULL");
return bo;
// throw new ServiceException(ServiceError.CALLBACKURL_IS_NULL);
}
//检查memcached中是否已经有该udid
String cacheUdid = cacheClient.get(unions_KEY + request.getApp() + "_" + request.getUdid(), String.class);
log.info("addunions get cache key={}, cacheUdid={}", unions_KEY + request.getApp() + "_" + request.getUdid(), cacheUdid);
if (StringUtils.isNotEmpty(cacheUdid)) {
log.warn("addunions error app and udid is added with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("APP UDID IS EXISTS");
return bo;
// throw new ServiceException(ServiceError.APP_UDID_IS_EXISTS);
}
//保存到memcached,时间,一个小时
DynamicIntProperty activeTime = DynamicPropertyFactory.getInstance().getIntProperty("activeTime", 60 * 60);
cacheClient.set(unions_KEY + request.getApp() + "_" + request.getUdid(), activeTime.get(), request.getUdid());
log.debug("addunions set cache success");
//插入数据库
try {
UnionsActiveRecord unions = new UnionsActiveRecord();
unions.setApp(request.getApp());
unions.setUdid(request.getUdid());
unions.setCallbackurl(request.getCallbackurl());
unions.setType(request.getType());
log.debug("add to unionsLog db with param is {}", unions);
unionsActiveRecordDAO.insert(unions);
} catch (Exception e) {
log.error("", e);
}
log.info("addunions success with param is {}", request);
addDingdang.info("addunions success with param is {}", request);
bo.setMsg("成功");
bo.setIsSuccess(true);
return bo;
}
// @Override
// public UnionResponseBO addUnion(AddUnionRequestBO request) throws Exception {
// log.debug("addunions with param is {}", request);
// UnionResponseBO bo = new UnionResponseBO();
// if (StringUtils.isEmpty(request.getApp())) {
// log.warn("addunions error app is null with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("APP IS NULL");
// return bo;
//// throw new ServiceException(ServiceError.APP_IS_NULL);
// }
// if (StringUtils.isEmpty(request.getUdid())) {
// log.warn("addunions error udid is null with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("UDID IS NULL");
// return bo;
//// throw new ServiceException(ServiceError.UDID_IS_NULL);
// }
// if (StringUtils.isEmpty(request.getCallbackurl())) {
// log.warn("addunions error callbackurl is null with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("CALLBACKURL IS NULL");
// return bo;
//// throw new ServiceException(ServiceError.CALLBACKURL_IS_NULL);
// }
//
// //检查memcached中是否已经有该udid
// String cacheUdid = cacheClient.get(unions_KEY + request.getApp() + "_" + request.getUdid(), String.class);
// log.info("addunions get cache key={}, cacheUdid={}", unions_KEY + request.getApp() + "_" + request.getUdid(), cacheUdid);
// if (StringUtils.isNotEmpty(cacheUdid)) {
// log.warn("addunions error app and udid is added with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("APP UDID IS EXISTS");
// return bo;
//// throw new ServiceException(ServiceError.APP_UDID_IS_EXISTS);
// }
//
// //保存到memcached,时间,一个小时
// DynamicIntProperty activeTime = DynamicPropertyFactory.getInstance().getIntProperty("activeTime", 60 * 60);
// cacheClient.set(unions_KEY + request.getApp() + "_" + request.getUdid(), activeTime.get(), request.getUdid());
// log.debug("addunions set cache success");
// //插入数据库
// try {
// UnionsActiveRecord unions = new UnionsActiveRecord();
// unions.setApp(request.getApp());
// unions.setUdid(request.getUdid());
// unions.setCallbackurl(request.getCallbackurl());
// unions.setType(request.getType());
// log.debug("add to unionsLog db with param is {}", unions);
// unionsActiveRecordDAO.insert(unions);
// } catch (Exception e) {
// log.error("", e);
// }
// log.info("addunions success with param is {}", request);
// addDingdang.info("addunions success with param is {}", request);
// bo.setMsg("成功");
// bo.setIsSuccess(true);
// return bo;
// }
@Override
public UnionResponseBO activeUnion(ActiveUnionRequestBO request) throws Exception {
log.debug("activeunions with param is {}", request);
UnionResponseBO bo = new UnionResponseBO();
if (StringUtils.isEmpty(request.getAppid())) {
log.warn("activeunions error app is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("APP IS NULL");
return bo;
//throw new ServiceException(500, "APP_IS_NULL");
}
if (StringUtils.isEmpty(request.getUdid())) {
log.warn("activeunions error udid is null with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("UDID IS NULL");
return bo;
// throw new ServiceException(500, "UDID_IS_NULL");
}
String memKey = unions_KEY + request.getAppid() + "_" + request.getUdid();
//检查memcached中是否已经有该udid
String cacheUdid = cacheClient.get(memKey, String.class);
log.info("activeUnion get cache key={}, cacheUdid={}", memKey, cacheUdid);
if (StringUtils.isEmpty(cacheUdid)) {
log.warn("activeunions error app and udid is not exists with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("APP UDID IS NOT EXISTS IN CACHE");
return bo;
}
//检查该app和udid是否已经激活
UnionsActiveRecord unions = new UnionsActiveRecord();
unions.setApp(request.getAppid());
unions.setUdid(request.getUdid());
unions.setType(request.getApptype());
UnionsActiveRecord u = unionsActiveRecordDAO.selectByUdidAndApp(unions);
if (u == null) {
log.warn("activeunions error app and udid is not exists with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("database has not exists this udid but cache exists");
return bo;
}
//已经激活
if ("1".equals(u.getIsActive())) {
log.warn("activeunions error app is actived with param is {}", request);
bo.setIsSuccess(false);
bo.setMsg("app udid had actived");
return bo;
}
//调用联盟激活接口
int repeatCount = 0;
Pair<Integer, String> pair = null;
JSONObject json = null;
boolean isSuccess = false;
String url = "";
while (true) {
//如果已经成功,或者重试次数大于3次,则不再调用
if (isSuccess || repeatCount >= 3) {
break;
}
try {
url = u.getCallbackurl();
pair = HttpUtils.httpGet(url);
if (pair.getLeft() == HttpStatus.SC_OK) {
log.debug("call " + u.getCallbackurl() + " url success return message is {}", pair.getRight());
json = JSONObject.parseObject(pair.getRight());
isSuccess = json.getBooleanValue("isSuccess");
}
} catch (Exception e) {
log.error("", e);
}
repeatCount++;
}
log.info("call " + u.getCallbackurl() + " url return message is {}", pair.getRight());
//更新数据库
if (isSuccess) {
unions.setIsActive("1");
unions.setId(u.getId());
unionsActiveRecordDAO.updateByPrimaryKey(unions);
//激活成功,从memcached中删除
cacheClient.delete(memKey);
log.debug("activeUnion delete cache success key={}", memKey);
bo.setMsg("激活成功");
bo.setIsSuccess(true);
} else {
unions.setIsActive("0");
unions.setId(u.getId());
unionsActiveRecordDAO.updateByPrimaryKey(unions);
bo.setMsg("激活失败");
bo.setIsSuccess(false);
}
try {
//组装大数据分析的日志
JSONObject j = new JSONObject();
j.put("apptype", request.getApptype());
j.put("appid", request.getAppid());
j.put("udid", request.getUdid());
j.put("dateid", DateUtil.getcurrentDateTime());
SourceEnum e = SourceEnum.getSourceEnumByValue(u.getType());
j.put("source", e == null ? "" : e.getName());
j.put("ip", request.getClientIP());
j.put("collect_ip", "");
j.put("tdid", request.getTdid());
activeDingdang.info(j.toString());
} catch (Exception e) {
log.error("", e);
}
return bo;
}
// @Override
// public UnionResponseBO activeUnion(ActiveUnionRequestBO request) throws Exception {
// log.debug("activeunions with param is {}", request);
// UnionResponseBO bo = new UnionResponseBO();
// if (StringUtils.isEmpty(request.getAppid())) {
// log.warn("activeunions error app is null with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("APP IS NULL");
// return bo;
// //throw new ServiceException(500, "APP_IS_NULL");
// }
// if (StringUtils.isEmpty(request.getUdid())) {
// log.warn("activeunions error udid is null with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("UDID IS NULL");
// return bo;
//// throw new ServiceException(500, "UDID_IS_NULL");
// }
//
// String memKey = unions_KEY + request.getAppid() + "_" + request.getUdid();
//
// //检查memcached中是否已经有该udid
// String cacheUdid = cacheClient.get(memKey, String.class);
// log.info("activeUnion get cache key={}, cacheUdid={}", memKey, cacheUdid);
// if (StringUtils.isEmpty(cacheUdid)) {
// log.warn("activeunions error app and udid is not exists with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("APP UDID IS NOT EXISTS IN CACHE");
// return bo;
// }
//
// //检查该app和udid是否已经激活
// UnionsActiveRecord unions = new UnionsActiveRecord();
// unions.setApp(request.getAppid());
// unions.setUdid(request.getUdid());
// unions.setType(request.getApptype());
// UnionsActiveRecord u = unionsActiveRecordDAO.selectByUdidAndApp(unions);
//
// if (u == null) {
// log.warn("activeunions error app and udid is not exists with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("database has not exists this udid but cache exists");
// return bo;
// }
//
// //已经激活
// if ("1".equals(u.getIsActive())) {
// log.warn("activeunions error app is actived with param is {}", request);
// bo.setIsSuccess(false);
// bo.setMsg("app udid had actived");
// return bo;
// }
//
// //调用联盟激活接口
// int repeatCount = 0;
// Pair<Integer, String> pair = null;
// JSONObject json = null;
// boolean isSuccess = false;
// String url = "";
// while (true) {
// //如果已经成功,或者重试次数大于3次,则不再调用
// if (isSuccess || repeatCount >= 3) {
// break;
// }
// try {
// url = u.getCallbackurl();
// pair = HttpUtils.httpGet(url);
// if (pair.getLeft() == HttpStatus.SC_OK) {
// log.debug("call " + u.getCallbackurl() + " url success return message is {}", pair.getRight());
// json = JSONObject.parseObject(pair.getRight());
// isSuccess = json.getBooleanValue("isSuccess");
// }
// } catch (Exception e) {
// log.error("", e);
// }
// repeatCount++;
// }
// log.info("call " + u.getCallbackurl() + " url return message is {}", pair.getRight());
//
// //更新数据库
// if (isSuccess) {
// unions.setIsActive("1");
// unions.setId(u.getId());
// unionsActiveRecordDAO.updateByPrimaryKey(unions);
// //激活成功,从memcached中删除
// cacheClient.delete(memKey);
// log.debug("activeUnion delete cache success key={}", memKey);
// bo.setMsg("激活成功");
// bo.setIsSuccess(true);
// } else {
// unions.setIsActive("0");
// unions.setId(u.getId());
// unionsActiveRecordDAO.updateByPrimaryKey(unions);
// bo.setMsg("激活失败");
// bo.setIsSuccess(false);
// }
//
// try {
// //组装大数据分析的日志
// JSONObject j = new JSONObject();
// j.put("apptype", request.getApptype());
// j.put("appid", request.getAppid());
// j.put("udid", request.getUdid());
// j.put("dateid", DateUtil.getcurrentDateTime());
// SourceEnum e = SourceEnum.getSourceEnumByValue(u.getType());
// j.put("source", e == null ? "" : e.getName());
// j.put("ip", request.getClientIP());
// j.put("collect_ip", "");
// j.put("tdid", request.getTdid());
// activeDingdang.info(j.toString());
// } catch (Exception e) {
// log.error("", e);
// }
// return bo;
// }
@Override
public UnionResponseBO addBigData(int beginTime,int endTime){
... ...
... ... @@ -21,9 +21,6 @@
<bean id="springContextUtil" class="com.yoho.unions.common.utils.SpringContextUtil"/>
<!-- 元宵节抽签活动写文件 -->
<bean class="com.yoho.activity.thread.DrawServiceThread"/>
<!-- 装在properties文件 -->
<context:property-placeholder location="classpath*:config.properties"/>
... ...