【发布时间】:2019-12-06 09:32:20
【问题描述】:
我有一个方法定义为:
@Async("queryTaskExecutor")
@RateLimitation(rateSize = 10)
public Future<RunQueryCommandResult> execute(final Path xmlPath,
final Boolean calculateAlreadyAllowedTraffic,
final String sessionId,
final boolean isQueryFromAFA,
final String mode) {}
RateLimitation 是针对每个方法执行的 RateLimiter 验证的自定义注释。 触发Aspect的annoration定义为:
@Aspect
public class RateLimitationAspect {
private static final Logger log = LoggerFactory.getLogger(RateLimitationAspect.class);
@Autowired
RateLimitationValidator rateLimitationValidator;
public RateLimitationAspect() {
}
@Before("@annotation(rateLimitation)")
public void aroundRateLimitationAction(JoinPoint joinPoint, RateLimitation rateLimitation) throws Throwable {
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
Method method = methodSignature.getMethod();
log.info("Validating rate limitation on method " + method.getName());
if (!this.rateLimitationValidator.isValid(method, (ConstraintValidatorContext)null)) {
throw new IllegalStateException(MessageFormat.format("Calling method [{0}] reached the maximum permitted calls per second, request is denied.", method.toString()));
}
}
}
我创建了一个用于检查速率验证的测试:
@Test(expected = IllegalStateException.class)
public void test_execute_command_service_in_forbidden_amount_parallel() {
IntStream.range(1, 40).parallel().forEach(i ->
runQueryCommandService.execute(XML_PATH, false, VALID_ALL_FIREWALLS_SESSION, false, QUERY)
);
}
测试失败,因为没有抛出 IllegalStateException。 当我调试测试时,由于验证错误,确实在 Aspect 代码中抛出了 IllegalStateException。
llegalStateException 被吞下。 如果我从方法定义中删除 @Async 注释,则测试正在运行。 请帮忙。
【问题讨论】:
-
顺便说一句,您使用的是基于代理的 AOP 还是 AspectJ AOP?
标签: java spring asynchronous rate-limiting aspect