...
|
...
|
@@ -8,9 +8,13 @@ import java.util.Date; |
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Random;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -19,6 +23,7 @@ import org.springframework.dao.QueryTimeoutException; |
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.yoho.unions.common.redis.RedisValueCache;
|
|
|
import com.yoho.unions.dal.IUnionClickLogsDAO;
|
|
|
import com.yoho.unions.dal.UnionClickCountDayMapper;
|
|
|
import com.yoho.unions.dal.model.UnionClickCount;
|
...
|
...
|
@@ -41,34 +46,42 @@ public class UnionClickCountDayTask { |
|
|
// 防止交错执行
|
|
|
private Lock lock = new ReentrantLock();
|
|
|
|
|
|
@Resource
|
|
|
private RedisValueCache redisValueCache;
|
|
|
|
|
|
// 每天01:30分执行
|
|
|
@Scheduled(cron = "* 30 1 * * ?")
|
|
|
//@Scheduled(cron = "* 0/2 * * * ?")
|
|
|
public void run() {
|
|
|
|
|
|
try {
|
|
|
InetAddress addr = InetAddress.getLocalHost();
|
|
|
String ip = addr.getHostAddress();
|
|
|
logger.info("当前ip是:{}", ip);
|
|
|
if (!StringUtils.equals("172.31.70.253", ip)) {
|
|
|
logger.info("当前环境不是目标环境,不执行操作!");
|
|
|
return;
|
|
|
|
|
|
String key = "yh:union:uniontype:UnionClickCountDayTask:"
|
|
|
+ formatDay.format(new Date(System.currentTimeMillis()));
|
|
|
String value = Long.toString(System.nanoTime()) + Long.toString(new Random().nextInt(Integer.MAX_VALUE));
|
|
|
|
|
|
logger.info("UnionClickCountDayTask run time={} , exKey={}, exValue={}", format.format(new Date()), key, value);
|
|
|
redisValueCache.setIfAbsent(key, value);
|
|
|
String getValue = redisValueCache.get(key, String.class);
|
|
|
logger.info("UnionClickCountDayTask run time={} , getKeyValue={}", format.format(new Date()), getValue);
|
|
|
if (getValue != null && getValue.equals(value)) {
|
|
|
logger.info("UnionClickCountDayTask run time={} , redis lock success", format.format(new Date()));
|
|
|
try {
|
|
|
Calendar time = Calendar.getInstance();
|
|
|
time.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
time.set(Calendar.MINUTE, 0);
|
|
|
time.set(Calendar.SECOND, 0);
|
|
|
time.set(Calendar.MILLISECOND, 0);
|
|
|
int dayStart = (int) (time.getTimeInMillis() / 1000);
|
|
|
int begin = dayStart - 24 * 60 * 60;
|
|
|
int end = dayStart;
|
|
|
exeTask(begin, end);
|
|
|
} finally {
|
|
|
logger.info("UnionClickCountDayTask run time={} , exe over set lock key timeout",
|
|
|
format.format(new Date()));
|
|
|
redisValueCache.set(key, value, 1, TimeUnit.HOURS);
|
|
|
}
|
|
|
} catch (UnknownHostException e1) {
|
|
|
logger.error("获取ip地址出错!");
|
|
|
return;
|
|
|
} else {
|
|
|
logger.info("UnionClickCountDayTask run time={} , redis can not get lock", format.format(new Date()));
|
|
|
}
|
|
|
|
|
|
Calendar time = Calendar.getInstance();
|
|
|
time.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
time.set(Calendar.MINUTE, 0);
|
|
|
time.set(Calendar.SECOND, 0);
|
|
|
time.set(Calendar.MILLISECOND, 0);
|
|
|
int dayStart = (int) (time.getTimeInMillis() / 1000);
|
|
|
int begin = dayStart - 24 * 60 * 60;
|
|
|
int end = dayStart;
|
|
|
|
|
|
exeTask(begin, end);
|
|
|
}
|
|
|
|
|
|
public String exeTask(int begin, int end) {
|
...
|
...
|
|