|
|
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);
|
|
|
}
|
|
|
} |
...
|
...
|
|