一个AOP示例

现在您已经看到了所有组成部分是如何工作的,我们可以将它们组合起来做一些有用的事情。

由于业务服务的执行有时会由于并发问题(例如死锁失败者)而失败。如果操作重试,很可能在下一次尝试时成功。对于适合在这种情况下重试的业务服务(幂等操作,不需要返回用户进行冲突解决),我们希望透明地重试操作,以避免客户端看到 PessimisticLockingFailureException。这是一个明显跨越服务层中多个服务的要求,因此,通过切面来实现是理想的。

因为我们想要重试操作,所以我们需要使用环绕通知,这样我们可以多次调用 proceed。以下清单显示了基本的切面实现:

  • Java

  • Kotlin

@Aspect
public class ConcurrentOperationExecutor implements Ordered {

	private static final int DEFAULT_MAX_RETRIES = 2;

	private int maxRetries = DEFAULT_MAX_RETRIES;
	private int order = 1;

	public void setMaxRetries(int maxRetries) {
		this.maxRetries = maxRetries;
	}

	public int getOrder() {
		return this.order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	@Around("com.xyz.CommonPointcuts.businessService()") (1)
	public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
		int numAttempts = 0;
		PessimisticLockingFailureException lockFailureException;
		do {
			numAttempts++;
			try {
				return pjp.proceed();
			}
			catch(PessimisticLockingFailureException ex) {
				lockFailureException = ex;
			}
		} while(numAttempts <= this.maxRetries);
		throw lockFailureException;
	}
}
1 引用了在共享命名切入点定义中定义的 businessService 命名切入点。
@Aspect
class ConcurrentOperationExecutor : Ordered {

	private val DEFAULT_MAX_RETRIES = 2
	private var maxRetries = DEFAULT_MAX_RETRIES
	private var order = 1

	fun setMaxRetries(maxRetries: Int) {
		this.maxRetries = maxRetries
	}

	override fun getOrder(): Int {
		return this.order
	}

	fun setOrder(order: Int) {
		this.order = order
	}

	@Around("com.xyz.CommonPointcuts.businessService()") (1)
	fun doConcurrentOperation(pjp: ProceedingJoinPoint): Any? {
		var numAttempts = 0
		var lockFailureException: PessimisticLockingFailureException
		do {
			numAttempts++
			try {
				return pjp.proceed()
			} catch (ex: PessimisticLockingFailureException) {
				lockFailureException = ex
			}

		} while (numAttempts <= this.maxRetries)
		throw lockFailureException
	}
}
1 引用了在共享命名切入点定义中定义的 businessService 命名切入点。

请注意,该切面实现了 Ordered 接口,以便我们可以将该切面的优先级设置为高于事务通知(我们希望每次重试时都有一个新事务)。maxRetriesorder 属性都由 Spring 配置。主要操作发生在 doConcurrentOperation 环绕通知中。请注意,目前我们将重试逻辑应用于每个 businessService。我们尝试继续进行,如果遇到 PessimisticLockingFailureException 失败,则再次尝试,除非我们已经耗尽了所有的重试尝试。

相应的 Spring 配置如下:

<aop:aspectj-autoproxy/>

<bean id="concurrentOperationExecutor"
		class="com.xyz.service.impl.ConcurrentOperationExecutor">
	<property name="maxRetries" value="3"/>
	<property name="order" value="100"/>
</bean>

为了使切面仅重试幂等操作,我们可以定义以下 Idempotent 注解:

  • Java

  • Kotlin

@Retention(RetentionPolicy.RUNTIME)
// 标记注解
public @interface Idempotent {
}
@Retention(AnnotationRetention.RUNTIME)
// 标记注解
annotation class Idempotent

然后,我们可以使用该注解来注释服务操作的实现。将切面修改为仅重试幂等操作涉及细化切入点表达式,以便仅匹配 @Idempotent 操作,如下所示:

  • Java

  • Kotlin

@Around("execution(* com.xyz..service.*.*(..)) && " +
		"@annotation(com.xyz.service.Idempotent)")
public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
	// ...
}
@Around("execution(* com.xyz..service.*.*(..)) && " +
		"@annotation(com.xyz.service.Idempotent)")
fun doConcurrentOperation(pjp: ProceedingJoinPoint): Any? {
	// ...
}