Authored by DengXinFei

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

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