Java 在@Async注释的情况下,方面中的验证异常会被忽略
我有一个方法定义为:Java 在@Async注释的情况下,方面中的验证异常会被忽略,java,spring,asynchronous,rate-limiting,aspect,Java,Spring,Asynchronous,Rate Limiting,Aspect,我有一个方法定义为: @Async("queryTaskExecutor") @RateLimitation(rateSize = 10) public Future<RunQueryCommandResult> execute(final Path xmlPath, final Boolean calculateAlreadyAllowedTraffic,
@Async("queryTaskExecutor")
@RateLimitation(rateSize = 10)
public Future<RunQueryCommandResult> execute(final Path xmlPath,
final Boolean calculateAlreadyAllowedTraffic,
final String sessionId,
final boolean isQueryFromAFA,
final String mode) {}
@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。
在调试测试时,由于验证错误,方面代码中确实抛出了IllegalStateException
这个例外被吞没了。
如果我从方法定义中删除@Async注释,测试将正常工作。
请提供帮助。问题在于使用@Async注释调用方法不会触发Springbean代理。 我通过将测试更改为调用控制器(调用测试服务的控制器)解决了这个问题。
然后按照预期抛出并捕获异常。Btw,您使用的是基于代理的AOP还是AspectJ AOP?