Commit e9e9ac98 authored by yanzg's avatar yanzg

Merge remote-tracking branch 'origin/master'

parents 6c15a836 26ad40c3
...@@ -584,6 +584,48 @@ public class DateHelper { ...@@ -584,6 +584,48 @@ public class DateHelper {
return cal.getTime(); 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 * 获取当前时间,精准到秒,格式为: yyyy-MM-dd HH:mm:ss
* *
......
...@@ -68,12 +68,10 @@ public class WebAspect extends BaseRequestAspect { ...@@ -68,12 +68,10 @@ public class WebAspect extends BaseRequestAspect {
// 用户数据库记录 // 用户数据库记录
long start = requestLog(TAG, joinPoint); long start = requestLog(TAG, joinPoint);
Exception ex = null; Exception ex = null;
Ref<Boolean> flag = new Ref<>(false);
boolean isInit = false; boolean isInit = false;
try { try {
isInit = tokenServiceCall.tokenInit(); isInit = tokenServiceCall.tokenInit();
result = executeMethod(joinPoint); result = executeMethod(joinPoint);
tokenFinish(flag);
return result; return result;
} catch (Exception e) { } catch (Exception e) {
ex = e; ex = e;
...@@ -84,8 +82,8 @@ public class WebAspect extends BaseRequestAspect { ...@@ -84,8 +82,8 @@ public class WebAspect extends BaseRequestAspect {
throw e; throw e;
} }
} finally { } finally {
tokenServiceCall.tokenFinish();
if (isInit) { if (isInit) {
tokenFinish(flag);
TokenHelper.remove(); TokenHelper.remove();
} }
responseLog(logWeb, clear, TAG, HttpAspectUtil.getHttpRequestUrl(), joinPoint, start, result, ex); responseLog(logWeb, clear, TAG, HttpAspectUtil.getHttpRequestUrl(), joinPoint, start, result, ex);
...@@ -101,19 +99,6 @@ public class WebAspect extends BaseRequestAspect { ...@@ -101,19 +99,6 @@ public class WebAspect extends BaseRequestAspect {
return this.applicationName.matches(this.gateWay); 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; ...@@ -6,7 +6,6 @@ import com.yanzuoguang.token.TokenLoad;
import com.yanzuoguang.util.cache.MemoryCache; import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ThreadHelper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -26,7 +25,7 @@ public class TokenServiceCall implements TokenLoad { ...@@ -26,7 +25,7 @@ public class TokenServiceCall implements TokenLoad {
private int flagCount = 3; private int flagCount = 3;
private MemoryCache<Boolean> countCache = new MemoryCache<>(); private MemoryCache<Integer> countCache = new MemoryCache<>();
public TokenServiceCall() { public TokenServiceCall() {
TokenHelper.setTokenLoad(this); TokenHelper.setTokenLoad(this);
...@@ -56,12 +55,11 @@ public class TokenServiceCall implements TokenLoad { ...@@ -56,12 +55,11 @@ public class TokenServiceCall implements TokenLoad {
if (tokenService == null) { if (tokenService == null) {
return false; return false;
} }
if (isHave()) { if (addHave() == 1) {
return false; tokenService.tokenInit();
return true;
} }
tokenService.tokenInit(); return false;
setHave();
return true;
} }
/** /**
...@@ -72,8 +70,9 @@ public class TokenServiceCall implements TokenLoad { ...@@ -72,8 +70,9 @@ public class TokenServiceCall implements TokenLoad {
if (tokenService == null) { if (tokenService == null) {
return; return;
} }
tokenService.tokenFinish(); if (subHave() == 0) {
removeHave(); tokenService.tokenFinish();
}
} }
/** /**
...@@ -91,18 +90,22 @@ public class TokenServiceCall implements TokenLoad { ...@@ -91,18 +90,22 @@ public class TokenServiceCall implements TokenLoad {
return tokenService.load(token); return tokenService.load(token);
} }
private boolean isHave() { private int addHave(int flag) {
String id = StringHelper.toString(Thread.currentThread().getId()); 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() { private int addHave() {
String id = StringHelper.toString(Thread.currentThread().getId()); return addHave(1);
countCache.put(id, true);
} }
private void removeHave() { private int subHave() {
String id = StringHelper.toString(Thread.currentThread().getId()); return addHave(-1);
countCache.remove(id);
} }
} }
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