gpt4 book ai didi

java - 从回调内部重新抛出 Throwable

转载 作者:行者123 更新时间:2023-12-02 07:47:14 27 4
gpt4 key购买 nike

我正在实现一个 AOP 拦截器,它处理 RetryTemplate 中的逻辑。问题是 ProceedingJoinPoint#execute 被声明为抛出 Throwable,但 RetryTemplate#doWithRetry 允许重新抛出 Exception仅。所以我必须包装 Throwable 然后再将其拆开。

我最好的尝试如下:

@Aspect
@Component
public class RetryableOperationFailureInterceptor {

@Around("@annotation(retryable)")
public Object performOperation(final ProceedingJoinPoint pjp, Retryable retryable)
throws Throwable {

RetryTemplate retryTemplate = getRetryTemplate(retryable);
try {
return retryTemplate.execute(new RetryCallback<Object>() {
@Override
public Object doWithRetry(RetryContext context)
throws Exception {

try {
return pjp.proceed(); // throws Throwable
} catch (Exception e) {
throw e;
} catch (Error e) {
throw e;
} catch (Throwable e) {
throw new RetryWrappedException(e);
}
}
});
} catch (RetryWrappedException e) {
throw e.getCause();
}
}
}

有什么方法可以避免包装或/和捕获Throwable?最新的对我来说更重要,因为捕获 Throwable 会触发代码样式违规。

最佳答案

M。 Deinum 是对的,但您不需要使用自己的 RetryWrappedException因为 AspectJ 已经提供了 SoftException (扩展 RuntimeException )用于软化/包装检查的异常,但它也适用于其他 Throwable s。

因为我发现你的问题很有趣,但我不是 Spring 用户,所以我将你的一些示例类复制为虚拟类或最小实现,以便创建一个完全工作的 POJO + AspectJ 示例,而无需任何 Spring 依赖。我还扩展了 around 建议,以便自行执行重试循环,阅读 maxRetries值来自Retryable注解。我知道这不是 100% 你的用例,但它说明了问题和其他一些事情:

虚拟类/接口(interface):

package de.scrum_master.app;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Retryable {
int maxRetries();
}

--

package de.scrum_master.app;

public class RetryContext {}

--

package de.scrum_master.app;

public interface RetryCallback<T> {
Object doWithRetry(RetryContext context) throws Exception;
}

--

package de.scrum_master.app;

public class RetryTemplate {
public Object execute(RetryCallback<Object> retryCallback) throws Exception {
return retryCallback.doWithRetry(new RetryContext());
}
}

驱动程序应用程序:

package de.scrum_master.app;

import java.util.Random;

public class Application {
private static final Random RANDOM = new Random();

public static void main(String[] args) throws Exception {
new Application().performOperation();
}

@Retryable(maxRetries = 2)
public void performOperation() throws Exception {
System.out.println("Performing some operation");
if (RANDOM.nextBoolean())
throw new Exception("Operation failed");
System.out.println("Operation succeeded");
}
}

没有aspect,就不会重试并且performOperation()会随机成功

Performing some operation
Operation succeeded

或者失败

Performing some operationException in thread "main" 
java.lang.Exception: Operation failed
at de.scrum_master.app.Application.performOperation(Application.java:16)
at de.scrum_master.app.Application.main(Application.java:9)

拦截/重试方面:

现在我们可以通过以下方面

  • 拦截由Retryable注释的方法,
  • 获取最大重试次数
  • 循环并尝试通过 proceed() 执行连接点指定的次数( maxRetries + 1 因为即使请求零次重试,我们也至少调用该方法一次)。

正如您所看到的,我们根据错误类型使用不同的错误处理:

  • Exception如果尚未达到最大重试次数,则仅打印,否则将重新抛出,即如果最终重试也失败,则将抛出异常。
  • 错误总是会被重新抛出,因为错误通常是由 JVM 抛出且不可恢复的,即在这种情况下将不会再重试。
  • 一个Throwable (永远不应该发生)将被包装成 SoftException后者被重新抛出。
package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.SoftException;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import de.scrum_master.app.RetryCallback;
import de.scrum_master.app.RetryContext;
import de.scrum_master.app.RetryTemplate;
import de.scrum_master.app.Retryable;

@Aspect
public class RetryableOperationFailureInterceptor {
@Around("@annotation(retryable) && execution(* *(..))")
public Object performOperation(final ProceedingJoinPoint pjp, final Retryable retryable) throws Throwable {
final int maxRetries = retryable.maxRetries();
System.out.println(pjp + " - maxRetries = " + maxRetries);
RetryTemplate retryTemplate = getRetryTemplate(retryable);
return retryTemplate.execute(
new RetryCallback<Object>() {
@Override
public Object doWithRetry(RetryContext context) throws Exception {
for (int i = 0; i <= maxRetries; i++) {
System.out.println("Retry #" + i);
try {
return pjp.proceed();
} catch (Exception e) {
if (i == maxRetries)
throw e;
System.out.println(e);
} catch (Error e) {
throw e;
} catch (Throwable t) {
throw new SoftException(t);
}
}
return null;
}
}
);
}

private RetryTemplate getRetryTemplate(Retryable retryable) {
return new RetryTemplate();
}
}

具有 Activity 方面的结果输出:

立即成功(无需重试):

execution(void de.scrum_master.app.Application.performOperation()) - maxRetries = 2
Retry #0
Performing some operation
Operation succeeded

重试后成功:

execution(void de.scrum_master.app.Application.performOperation()) - maxRetries = 2
Retry #0
Performing some operation
java.lang.Exception: Operation failed
Retry #1
Performing some operation
java.lang.Exception: Operation failed
Retry #2
Performing some operation
Operation succeeded

最终重试后失败:

execution(void de.scrum_master.app.Application.performOperation()) - maxRetries = 2
Retry #0
Performing some operation
java.lang.Exception: Operation failed
Retry #1
Performing some operation
java.lang.Exception: Operation failed
Retry #2
Performing some operation
Exception in thread "main" java.lang.Exception: Operation failed
at de.scrum_master.app.Application.performOperation_aroundBody0(Application.java:16)
at de.scrum_master.app.Application$AjcClosure1.run(Application.java:1)
at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149)
at de.scrum_master.aspect.RetryableOperationFailureInterceptor$1.doWithRetry(RetryableOperationFailureInterceptor.java:27)
at de.scrum_master.app.RetryTemplate.execute(RetryTemplate.java:5)
at de.scrum_master.aspect.RetryableOperationFailureInterceptor.performOperation(RetryableOperationFailureInterceptor.java:20)
at de.scrum_master.app.Application.performOperation(Application.java:13)
at de.scrum_master.app.Application.main(Application.java:9)

关于java - 从回调内部重新抛出 Throwable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24408945/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com