1. Core Concept: The Central Hub of the Application
ActivityContext is the core container (IoC Container) and the most central interface of an Aspectran application. It acts as a Central Hub that oversees and manages all of the application’s configuration information, components (Beans), and runtime execution state. This is a concept similar to the ApplicationContext in the Spring Framework, and it is responsible for the entire lifecycle from the start to the end of the application.
ActivityContext is an interface, and within the framework, the com.aspectran.core.context.DefaultActivityContext class is mainly used as the core implementation of this interface.
2. Key Responsibilities and Roles
The roles of ActivityContext can be broadly divided as follows:
a. Component Lifecycle Management
- Manages the creation, initialization, and destruction of all core components registered in the application.
- The
doInitialize()anddoDestroy()methods ofDefaultActivityContextserially call the lifecycle methods of all registered registries and renderers internally to maintain a consistent state.
b. Core Component Registry
- Provides an access point to the registries that store and manage all the rules and components of the application. This allows all elements of the application to interact with each other through the
ActivityContext. BeanRegistry: Creates and manages instances of all bean objects defined by@Beanor XML configuration. It performs the practical role of a Dependency Injection (DI) container.AspectRuleRegistry: StoresAspectRule, the core of aspects (AOP), and provides information on which advice should be applied to a specific joinpoint.TransletRuleRegistry: Manages all the rules (TransletRule) of the core processing unit, the translet.ScheduleRuleRegistry: Manages rules related to scheduling tasks (ScheduleRule).
c. Environment Access
- Provides access to the
Environmentobject, which represents the application’s execution environment. - This allows each component to behave differently depending on the currently active profile or the property values defined in
.propertiesfiles. For example, you can configure it to use a local database in thedevprofile and a production database in theprodprofile.
d. Activity Management
- Manages the lifecycle of the
Activityobject, which is a single unit of execution in the application. AnActivityrepresents the processing of a single request, such as a web request, a scheduled task, or a shell command. - Thread Safety based on
ThreadLocal: It uses aThreadLocalvariable calledcurrentActivityHolderto independently manage the currently executingActivityfor each thread. This is a very important feature that ensures that each request does not affect the state of other requests in a multi-threaded environment (e.g., a web application). - Through
defaultActivity, it supports the ability to perform basic functions even in a non-request context where there is no ongoingActivity.
e. Resource Loading & i18n
- Manages the
ClassLoaderthat loads the application’s classes and resources, and theApplicationAdapterthat abstracts interaction with the external environment. - Provides an access point to
MessageSourcefor internationalization (i18n), allowing the application to respond to various language environments.
3. Overall Operational Flow
- Bootstrapping: When an Aspectran application starts,
ActivityContextBuilderparses configuration files (XML or Apon) and annotations to create aDefaultActivityContextinstance. - Registry Configuration: Based on the parsed configuration information, various registries such as
BeanRegistryandAspectRuleRegistryare created and populated with rule information. - Context Injection: The created registries and the
Environmentobject are injected intoDefaultActivityContext. - Initialization: As the
ActivityContextis initialized, it serially initializes all the components it manages (registries, singleton beans, renderers, etc.). - Waiting for Requests: Once initialization is complete, the context is ready to handle external requests (HTTP requests, schedule executions, etc.).
- Activity Creation: When a request comes in, a new
Activityobject is created to handle that request. - Thread Context Registration: The created
Activityis registered in theActivityContextas the “Current Activity” for the current thread. - Request Processing: The
Activityobtains the necessary beans through theActivityContext, executes translets, and applies aspects to process the request. - Thread Context Removal: When request processing is complete, the “CurrentActivity” for that thread is removed from the
ActivityContext. - Destruction: When the application shuts down, the
destroy()method ofActivityContextis called to safely destroy all the components it managed.
4. Summary
ActivityContext is the central control unit of an Aspectran application and the starting point for everything. As a container that holds all core components and configuration information, it manages their lifecycle and provides a consistent method of access. In particular, it ensures stability in a multi-threaded environment by independently managing the Activity for each thread. Therefore, understanding ActivityContext is most crucial for understanding the entire architecture of the Aspectran framework.