1. Overview
Aspectran framework’s AOP (Aspect-Oriented Programming) proxy mechanism is designed with a focus on performance optimization and flexible context management in asynchronous (@Async) execution environments. At the core of this mechanism are two components: AbstractBeanProxy and ProxyActivity. This document analyzes the roles and interactions of these two components to explain how the new AOP proxy operates more efficiently and stably.
2. Core Component Analysis
2.1. AbstractBeanProxy: Intelligent AOP Execution Control
AbstractBeanProxy is an abstract class that serves as the base for all proxy objects that apply AOP advice. The most important feature of this class is that it eliminates unnecessary overhead through selective advice application logic.
Performance Optimization (
isAdvisableMethod): When a method of a proxied bean is called,AbstractBeanProxyfirst checks if the method has the@Advisableor@Asyncannotation. If neither annotation is present, it skips all complex logic for applying AOP advice (e.g., looking up advice rules, creating contexts) and immediately executes the original method. This fundamentally eliminates unnecessary overhead from numerous internal method calls that do not require advice, significantly improving overall application performance.Synchronous/Asynchronous Execution Branching: If the method has an annotation, it branches the execution path as follows:
- If the
@Asyncannotation is detected, it calls theinvokeAsync()method for asynchronous execution. - If only the
@Advisableannotation is present, it calls theinvokeSync()method for synchronous execution to perform the existing AOP advice logic.
- If the
2.2. ProxyActivity: Lightweight Context for Advice Execution
ProxyActivity is a lightweight Activity designed for the specific purpose of executing AOP advice. Unlike WebActivity or ShellActivity, which handle general request-response cycles, it operates very lightly by providing only the minimum functionality required for advice execution.
- Two Creation Modes:
- Independent Mode (
new ProxyActivity(context)): Created when there is noActivityrunning in the current thread (e.g., when an@Asyncmethod is called for the first time). ThisProxyActivityhas its own independentActivityDataand executes advice in a completely isolated context. - Wrapping Mode (
new ProxyActivity(activity)): Created by wrapping an existingActivitywhen anActivityalready exists in the current thread. The most significant feature of this mode is that it shares theActivityDataof the originalActivity. This allows the asynchronous worker thread to read or write request parameters or attributes from the caller thread.
- Independent Mode (
- Limited Role: Most methods unrelated to advice execution, such as
getTranslet()andgetDeclaredResponse(), throwUnsupportedOperationException, enforcing the clear role of this class. Its core role is to register itself as theCurrentActivityin the current thread via theperform()method, execute the given logic (advice and original method), and then cleanly remove itself from the thread in thefinallyblock.
3. Asynchronous (@Async) Processing and Context Sharing Mechanism
The advantage of this design is clearly evident in how context is shared in an asynchronous environment. When multiple advice-applied methods are called in a chain within a single asynchronous task, the context (Activity) is shared efficiently as follows:
- First
@AsyncMethod Call:- The task starts in a new Worker thread by
AsyncTaskExecutor. - At this point, the Worker thread has no
CurrentActivity, soAbstractBeanProxycreates a newProxyActivityin independent mode (hereinafterPA_1).
- The task starts in a new Worker thread by
- Context Registration:
- The created
PA_1is registered as theCurrentActivityin the current Worker thread’sThreadLocalvia itsperform()method.
- The created
- Chained Internal Method Calls:
- If another
@Advisablemethod (methodA) is called inside the@Asyncmethod,AbstractBeanProxyoperates again. - The proxy checks
context.hasCurrentActivity()and receivestruebecausePA_1is already registered in the thread. - Therefore, it reuses the existing
PA_1instance without creating a newActivityto execute the advice formethodA. - Even if
methodAcalls another advice-applied method later, thePA_1context continues to be shared as long as it runs in that Worker thread.
- If another
- Context Cleanup:
- When the execution of the initial
@Asyncmethod is complete, thefinallyblock ofPA_1.perform()executes, safely removing theCurrentActivityfrom the thread’sThreadLocal.
- When the execution of the initial
Thanks to this mechanism, a single logical asynchronous task unit shares a single ProxyActivity instance, allowing it to maintain a consistent context while preventing unnecessary context object creation.
4. Expected Effects and Conclusion
The new AOP proxy mechanism offers the following clear advantages:
- Performance Improvement: Eliminates the overhead of unnecessary method calls that do not require advice, improving overall system performance.
- Flexible Context Management: With
ProxyActivity’s independent and wrapping modes, flexible and stable context management is possible in both synchronous and asynchronous environments. - Code Clarity: Through
@Advisableand@Asyncannotations, developers can clearly express which methods are subject to AOP.
In conclusion, Aspectran has further evolved into a lighter, faster, and modern AOP framework that perfectly supports complex asynchronous environments through AbstractBeanProxy and ProxyActivity.