Skip to content

Commit 40b745c

Browse files
authored
Merge pull request #13 from Frodez/0.3-alpha
0.3 alpha
2 parents 46fcb35 + 83e45d4 commit 40b745c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+390
-238
lines changed

src/main/java/frodez/config/aop/log/annotation/DurationLog.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.lang.annotation.Retention;
66
import java.lang.annotation.RetentionPolicy;
77
import java.lang.annotation.Target;
8+
import javax.validation.constraints.Positive;
89

910
/**
1011
* 方法耗时监控注解
@@ -21,6 +22,7 @@
2122
* @author Frodez
2223
* @date 2019-04-13
2324
*/
25+
@Positive
2426
long threshold() default 3000;
2527

2628
}

src/main/java/frodez/config/aop/request/TimeoutAOP.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ public class TimeoutAOP {
4949
public Object process(ProceedingJoinPoint point) throws Throwable {
5050
HttpServletRequest request = MVCUtil.request();
5151
TimeoutLock timeoutLock = AspectUtil.annotation(point, TimeoutLock.class);
52-
if (timeoutLock.value() < 0) {
53-
throw new IllegalArgumentException("the timeout can't be negative!");
54-
}
5552
String key = KeyGenerator.servletKey(AspectUtil.fullMethodName(point), request);
5653
if (checker.check(key)) {
5754
log.info("重复请求:IP地址{}", ServletUtil.getAddr(request));

src/main/java/frodez/config/aop/request/annotation/Limit.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.lang.annotation.Retention;
66
import java.lang.annotation.RetentionPolicy;
77
import java.lang.annotation.Target;
8+
import javax.validation.constraints.Positive;
89

910
/**
1011
* 请求限流注解,只用于controller中的端点
@@ -21,13 +22,15 @@
2122
* @author Frodez
2223
* @date 2019-04-13
2324
*/
25+
@Positive
2426
double value() default 100.0;
2527

2628
/**
2729
* 超时时间,默认值3000毫秒
2830
* @author Frodez
2931
* @date 2019-04-13
3032
*/
33+
@Positive
3134
long timeout() default 3000;
3235

3336
}

src/main/java/frodez/config/aop/request/annotation/TimeoutLock.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.lang.annotation.Retention;
66
import java.lang.annotation.RetentionPolicy;
77
import java.lang.annotation.Target;
8+
import javax.validation.constraints.Positive;
89

910
/**
1011
* 控制重复请求注解(带过期时间),只用于controller中的端点
@@ -21,6 +22,7 @@
2122
* @author Frodez
2223
* @date 2019-04-13
2324
*/
25+
@Positive
2426
long value() default 500;
2527

2628
}

src/main/java/frodez/config/aop/validation/ValidationAOP.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import frodez.util.aop.AspectUtil;
44
import frodez.util.beans.result.Result;
5-
import frodez.util.common.EmptyUtil;
65
import frodez.util.common.ValidationUtil;
76
import org.aspectj.lang.ProceedingJoinPoint;
87
import org.aspectj.lang.annotation.Around;
@@ -28,7 +27,7 @@ public class ValidationAOP {
2827
@Around("@annotation(frodez.config.aop.validation.annotation.Check)")
2928
public Object validate(ProceedingJoinPoint p) throws Throwable {
3029
String msg = ValidationUtil.validateParam(p.getTarget(), AspectUtil.method(p), p.getArgs());
31-
return EmptyUtil.yes(msg) ? p.proceed() : Result.errorRequest(msg);
30+
return msg == null ? p.proceed() : Result.errorRequest(msg);
3231
}
3332

3433
}

src/main/java/frodez/config/aop/validation/annotation/common/LegalStr.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
*/
3737
String regex();
3838

39+
/**
40+
* 正则表达式的模式
41+
* @see java.util.regex.Pattern
42+
* @see javax.validation.constraints.Pattern.Flag
43+
* @author Frodez
44+
* @date 2019-04-18
45+
*/
3946
Flag[] flags() default {};
4047

4148
/**
@@ -57,6 +64,9 @@ class Validator implements ConstraintValidator<LegalStr, String> {
5764
*/
5865
private String regex;
5966

67+
/**
68+
* 正则表达式的模式
69+
*/
6070
private int flag;
6171

6272
/**

src/main/java/frodez/config/aop/validation/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* 本包用于配置方法参数自动验证。<br>
33
* 使用hibernate-validation框架进行验证。<br>
4+
* <strong>本包中的自定义注解使用后,相当于默认添加了@NotNull注解。如果需要关闭此功能,请在自定义注解的属性中设定nullable为true。</strong>
45
* @author Frodez
56
* @date 2019-03-11
67
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package frodez.config.async;
2+
3+
import frodez.util.spring.ContextUtil;
4+
import java.util.concurrent.Executor;
5+
import java.util.concurrent.ThreadPoolExecutor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.scheduling.annotation.EnableAsync;
10+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
11+
12+
/**
13+
* 异步配置
14+
* @author Frodez
15+
* @date 2019-04-15
16+
*/
17+
@Slf4j
18+
@EnableAsync
19+
@Configuration
20+
public class AsyncConfig {
21+
22+
@Bean
23+
public Executor getAsyncExecutor() {
24+
AsyncProperties properties = ContextUtil.get(AsyncProperties.class);
25+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
26+
int availableProcessors = Runtime.getRuntime().availableProcessors();
27+
int corePoolSize = Math.round(availableProcessors * properties.getCoreThreadTimes());
28+
int maxPoolSize = Math.round(availableProcessors * properties.getMaxThreadTimes());
29+
int queueCapacity = Math.round(maxPoolSize * properties.getQueueFactors());
30+
executor.setCorePoolSize(corePoolSize);
31+
executor.setMaxPoolSize(maxPoolSize);
32+
executor.setQueueCapacity(queueCapacity);
33+
executor.setKeepAliveSeconds(properties.getKeepAliveSeconds());
34+
executor.setThreadNamePrefix(properties.getThreadNamePrefix());
35+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
36+
executor.initialize();
37+
log.info("async executor is running now!");
38+
log.info("async config:{}corePoolSize, {}maxPoolSize, {}queueCapacity, {}keepAliveSeconds, {}threadNamePrefix",
39+
corePoolSize, maxPoolSize, queueCapacity, properties.getKeepAliveSeconds(), properties
40+
.getThreadNamePrefix());
41+
return executor;
42+
}
43+
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package frodez.config.async;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.context.annotation.PropertySource;
6+
import org.springframework.stereotype.Component;
7+
8+
/**
9+
* 异步任务配置
10+
* @author Frodez
11+
* @date 2019-04-15
12+
*/
13+
@Data
14+
@Component
15+
@PropertySource(value = { "classpath:settings/${spring.profiles.active}/async.properties" })
16+
@ConfigurationProperties(prefix = "async")
17+
public class AsyncProperties {
18+
19+
/**
20+
* 核心线程基数,实际数量等于系统环境可用核心数乘以该基数并四舍五入。
21+
*/
22+
private float coreThreadTimes = 0.5F;
23+
24+
/**
25+
* 最大线程基数,实际数量等于系统环境可用核心数乘以该基数并四舍五入。
26+
*/
27+
private float maxThreadTimes = 1.0F;
28+
29+
/**
30+
* 队列规模因子,队列最大长度等于计算出的最大线程数乘以规模因子并四舍五入。
31+
*/
32+
private float queueFactors = 16.0F;
33+
34+
/**
35+
* 线程最长活跃时间,单位为秒
36+
*/
37+
private int keepAliveSeconds = 60;
38+
39+
/**
40+
* 线程名前缀
41+
*/
42+
private String threadNamePrefix = "async";
43+
44+
}

src/main/java/frodez/config/cache/CacheProperties.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,82 @@
1616
@ConfigurationProperties(prefix = "cache")
1717
public class CacheProperties {
1818

19+
/**
20+
* 标准配置
21+
*/
1922
private StandardProperties standard = new StandardProperties();
2023

24+
/**
25+
* AutoGuavaChecker配置
26+
*/
2127
private AutoGuavaCheckerProperties autoGuavaChecker = new AutoGuavaCheckerProperties();
2228

29+
/**
30+
* ManualGuavaChecker配置
31+
*/
2332
private ManualGuavaCheckerProperties manualGuavaChecker = new ManualGuavaCheckerProperties();
2433

34+
/**
35+
* LimitUserGuavaChecker配置
36+
*/
2537
private LimitUserGuavaCheckerProperties limitUserGuavaChecker = new LimitUserGuavaCheckerProperties();
2638

39+
/**
40+
* URLMatcher配置
41+
*/
2742
private URLMatcherProperties urlMatcher = new URLMatcherProperties();
2843

2944
@Data
3045
public static class StandardProperties {
3146

47+
/**
48+
* 超时时间,单位毫秒
49+
*/
3250
private Integer timeout = 60000;
3351

3452
}
3553

3654
@Data
3755
public static class AutoGuavaCheckerProperties {
3856

57+
/**
58+
* 超时时间,单位毫秒
59+
*/
3960
private Integer timeout = 60000;
4061

4162
}
4263

4364
@Data
4465
public static class ManualGuavaCheckerProperties {
4566

67+
/**
68+
* 超时时间,单位毫秒
69+
*/
4670
private Integer timeout = 60000;
4771

4872
}
4973

5074
@Data
5175
public static class LimitUserGuavaCheckerProperties {
5276

77+
/**
78+
* 超时时间,单位毫秒
79+
*/
5380
private Integer timeout = 60000;
5481

5582
}
5683

5784
@Data
5885
public static class URLMatcherProperties {
5986

87+
/**
88+
* 超时时间,单位毫秒
89+
*/
6090
private Integer timeout = 3600000;
6191

92+
/**
93+
* 缓存最大容量
94+
*/
6295
private Integer maxSize = 65536;
6396

6497
}

0 commit comments

Comments
 (0)