Authored by DengXinFei

Merge branch 'dev' of git.dev.yoho.cn:yoho30/yohobuy-union into dev

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.netflix.config.DynamicStringProperty;
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);
}
}
... ...
... ... @@ -19,6 +19,9 @@
<constructor-arg name="memcachedServers" value="${cache.servers.memcached.address}"/>
</bean>
<!-- 元宵节抽签活动写文件 -->
<bean class="com.yoho.activity.thread.DrawServiceThread" />
<mvc:annotation-driven />
<!-- 装在properties文件 -->
<context:property-placeholder location="classpath*:config.properties" />
... ...
... ... @@ -23,4 +23,7 @@ redis.proxy.auth=
zkAddress=127.0.0.1:11111
# web context
web.context=union
\ No newline at end of file
web.context=union
#元宵节抽签活动,定时写文件的周期(分)
draw.writeFile.delay=1
\ No newline at end of file
... ...
... ... @@ -20,6 +20,8 @@
<property name="activeTime" defaultValue="3600" description="" />
<property name="draw.writeFile.delay" defaultValue="60" description="元宵节抽签活动,定时写文件的周期(分)" />
</group>
<script>
<generate template="META-INF/autoconf/config.properties" destfile="WEB-INF/classes/config.properties" />
... ...
... ... @@ -25,4 +25,7 @@ redis.readonly.proxy.auth=${redis.readonly.proxy.auth}
#zookeeper地址
zkAddress=${zkAddress}
# web context
web.context=union
\ No newline at end of file
web.context=union
#元宵节抽签活动,定时写文件的周期(分)
draw.writeFile.delay=${draw.writeFile.delay}
\ No newline at end of file
... ...