|
|
1
|
+package com.yoho.activity.thread;
|
|
|
2
|
+
|
|
|
3
|
+import java.io.File;
|
|
|
4
|
+import java.io.IOException;
|
|
|
5
|
+import java.util.ArrayList;
|
|
|
6
|
+import java.util.HashMap;
|
|
|
7
|
+import java.util.List;
|
|
|
8
|
+import java.util.Map;
|
|
|
9
|
+import java.util.Map.Entry;
|
|
|
10
|
+import java.util.concurrent.Executors;
|
|
|
11
|
+import java.util.concurrent.TimeUnit;
|
|
|
12
|
+
|
|
|
13
|
+import javax.annotation.Resource;
|
|
|
14
|
+
|
|
|
15
|
+import org.apache.commons.io.FileUtils;
|
|
|
16
|
+import org.apache.commons.lang.StringUtils;
|
|
|
17
|
+import org.slf4j.Logger;
|
|
|
18
|
+import org.slf4j.LoggerFactory;
|
|
|
19
|
+
|
|
|
20
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
21
|
+import com.netflix.config.DynamicLongProperty;
|
|
|
22
|
+import com.netflix.config.DynamicPropertyFactory;
|
|
|
23
|
+import com.netflix.config.DynamicStringProperty;
|
|
|
24
|
+import com.yoho.core.cache.CacheClient;
|
|
|
25
|
+import com.yoho.core.redis.YHValueOperations;
|
|
|
26
|
+
|
|
|
27
|
+public class DrawServiceThread {
|
|
|
28
|
+
|
|
|
29
|
+ static Logger log = LoggerFactory.getLogger(DrawServiceThread.class);
|
|
|
30
|
+ private static final String DRAW_USER_LIST_KEY = "DRAW_USER_LIST_KEY";
|
|
|
31
|
+ private static final String DRAW_USER_LIST_MEM_KEY = "DRAW_USER_LIST_MEM_KEY";
|
|
|
32
|
+
|
|
|
33
|
+ @Resource
|
|
|
34
|
+ CacheClient cacheClient;
|
|
|
35
|
+
|
|
|
36
|
+ @Resource
|
|
|
37
|
+ YHValueOperations<String, String> valueOperations;
|
|
|
38
|
+
|
|
|
39
|
+ String fileName = "/Data/logs/union/redis.txt";
|
|
|
40
|
+// String fileName = "D:/redis.txt";
|
|
|
41
|
+
|
|
|
42
|
+ /**
|
|
|
43
|
+ * 启动一个定时任务,来读取元宵节抽签活动的
|
|
|
44
|
+ */
|
|
|
45
|
+ public DrawServiceThread() {
|
|
|
46
|
+ //tomcat启动后,延迟一分钟,每个一个小时写一次文件
|
|
|
47
|
+ DynamicLongProperty delay = DynamicPropertyFactory.getInstance().getLongProperty("draw.writeFile.delay", 60);
|
|
|
48
|
+ Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
|
|
|
49
|
+
|
|
|
50
|
+ @Override
|
|
|
51
|
+ public void run() {
|
|
|
52
|
+ log.info("enter DrawServiceThread");
|
|
|
53
|
+ //cong redis中获取数据
|
|
|
54
|
+ String arr = valueOperations.get(DRAW_USER_LIST_KEY);
|
|
|
55
|
+ Map<String, JSONObject> maps = null;
|
|
|
56
|
+ if (StringUtils.isEmpty(arr)) {
|
|
|
57
|
+ maps = new HashMap<String, JSONObject>();
|
|
|
58
|
+ } else {
|
|
|
59
|
+ maps = (Map) JSONObject.parse(arr);
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ log.debug("DrawServiceThread query redis data total is {}", maps.size());
|
|
|
63
|
+
|
|
|
64
|
+ //从mem里面获取
|
|
|
65
|
+ Map<String, String> memMap = cacheClient.get(DRAW_USER_LIST_MEM_KEY, Map.class);
|
|
|
66
|
+ if (memMap == null) {
|
|
|
67
|
+ memMap = new HashMap<String, String>();
|
|
|
68
|
+ }
|
|
|
69
|
+ log.debug("DrawServiceThread query mem data total is {}", memMap.size());
|
|
|
70
|
+ List<String> l = new ArrayList<String>();
|
|
|
71
|
+ for (Entry<String, JSONObject> entry : maps.entrySet()) {
|
|
|
72
|
+ if (StringUtils.isNotEmpty(memMap.get(entry.getKey()))) {
|
|
|
73
|
+ //该用户已经保存过文件,则不在进行重复写
|
|
|
74
|
+ continue;
|
|
|
75
|
+ }
|
|
|
76
|
+ l.add(entry.getValue().toJSONString());
|
|
|
77
|
+ try {
|
|
|
78
|
+ FileUtils.writeLines(new File(fileName), "UTF-8", l, true);
|
|
|
79
|
+ log.debug("DrawServiceThread success write file with key={}, value={}", entry.getKey(), entry.getValue());
|
|
|
80
|
+ } catch (IOException e) {
|
|
|
81
|
+ log.error("write file error with key={}, value={}", entry.getKey(), entry.getValue(), e);
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ //往mem里面记录,该用户已经写过文件的标记
|
|
|
85
|
+ memMap.put(entry.getKey(), "1");
|
|
|
86
|
+ }
|
|
|
87
|
+ cacheClient.set(DRAW_USER_LIST_MEM_KEY, 30 * 24 * 60 * 60, memMap);
|
|
|
88
|
+ log.info("success out DrawServiceThread");
|
|
|
89
|
+ }
|
|
|
90
|
+ }, 1, delay.get(), TimeUnit.MINUTES);
|
|
|
91
|
+ }
|
|
|
92
|
+} |