Commit e9e9ac98 authored by yanzg's avatar yanzg

Merge remote-tracking branch 'origin/master'

parents 6c15a836 26ad40c3
......@@ -584,6 +584,48 @@ public class DateHelper {
return cal.getTime();
}
/**
* 增加小时处理
*
* @param date 当前日期
* @param hour 需要增加的小时
* @return 增加之后的值
*/
public static Date addHour(Date date, int hour) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, hour);
return cal.getTime();
}
/**
* 增加分钟处理
*
* @param date 当前日期
* @param minute 需要增加的分钟
* @return 增加之后的值
*/
public static Date addMinute(Date date, int minute) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, minute);
return cal.getTime();
}
/**
* 增加秒处理
*
* @param date 当前日期
* @param second 需要增加的分钟
* @return 增加之后的值
*/
public static Date addSecond(Date date, int second) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, second);
return cal.getTime();
}
/**
* 获取当前时间,精准到秒,格式为: yyyy-MM-dd HH:mm:ss
*
......
......@@ -68,12 +68,10 @@ public class WebAspect extends BaseRequestAspect {
// 用户数据库记录
long start = requestLog(TAG, joinPoint);
Exception ex = null;
Ref<Boolean> flag = new Ref<>(false);
boolean isInit = false;
try {
isInit = tokenServiceCall.tokenInit();
result = executeMethod(joinPoint);
tokenFinish(flag);
return result;
} catch (Exception e) {
ex = e;
......@@ -84,8 +82,8 @@ public class WebAspect extends BaseRequestAspect {
throw e;
}
} finally {
tokenServiceCall.tokenFinish();
if (isInit) {
tokenFinish(flag);
TokenHelper.remove();
}
responseLog(logWeb, clear, TAG, HttpAspectUtil.getHttpRequestUrl(), joinPoint, start, result, ex);
......@@ -101,19 +99,6 @@ public class WebAspect extends BaseRequestAspect {
return this.applicationName.matches(this.gateWay);
}
/**
* 执行结束函数
*
* @param flag 结束函数
* @return 结束函数
*/
private void tokenFinish(Ref<Boolean> flag) {
if (flag.value == true) {
return;
}
flag.value = true;
tokenServiceCall.tokenFinish();
}
/**
* 获取返回的至类型
......
......@@ -6,7 +6,6 @@ import com.yanzuoguang.token.TokenLoad;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ThreadHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
......@@ -26,7 +25,7 @@ public class TokenServiceCall implements TokenLoad {
private int flagCount = 3;
private MemoryCache<Boolean> countCache = new MemoryCache<>();
private MemoryCache<Integer> countCache = new MemoryCache<>();
public TokenServiceCall() {
TokenHelper.setTokenLoad(this);
......@@ -56,13 +55,12 @@ public class TokenServiceCall implements TokenLoad {
if (tokenService == null) {
return false;
}
if (isHave()) {
return false;
}
if (addHave() == 1) {
tokenService.tokenInit();
setHave();
return true;
}
return false;
}
/**
* 请求时结束token
......@@ -72,8 +70,9 @@ public class TokenServiceCall implements TokenLoad {
if (tokenService == null) {
return;
}
if (subHave() == 0) {
tokenService.tokenFinish();
removeHave();
}
}
/**
......@@ -91,18 +90,22 @@ public class TokenServiceCall implements TokenLoad {
return tokenService.load(token);
}
private boolean isHave() {
private int addHave(int flag) {
String id = StringHelper.toString(Thread.currentThread().getId());
return StringHelper.toBoolean(countCache.get(id));
int from = StringHelper.toInt(countCache.get(id));
int to = from + flag;
countCache.put(id, to);
if (to == 0) {
countCache.remove(id);
}
return to;
}
private void setHave() {
String id = StringHelper.toString(Thread.currentThread().getId());
countCache.put(id, true);
private int addHave() {
return addHave(1);
}
private void removeHave() {
String id = StringHelper.toString(Thread.currentThread().getId());
countCache.remove(id);
private int subHave() {
return addHave(-1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment