Chenile Trajectories
Copied from [`chenile-core/docs/trajectories.md`](https://github.com/ajapros/chenile-core/blob/main/docs/trajectories.md) on 1 December 2026.
Trajectories are Chenile’s runtime service-override mechanism. They allow the framework to keep the same logical service id and operation contract while switching the actual service implementation for selected requests.
This document explains:
- what a trajectory is
- how trajectory JSON is registered
- how requests activate a trajectory
- how the interceptor chain uses trajectory data during dispatch
What a trajectory is
A trajectory is a named set of overrides for one or more logical Chenile services.
The core model classes are:
chenile-core/src/main/java/org/chenile/core/model/TrajectoryDefinition.javachenile-core/src/main/java/org/chenile/core/model/TrajectoryOverride.java
A TrajectoryDefinition contains:
- an id such as
t1 - a map of service ids to
TrajectoryOverride
Each TrajectoryOverride can specify:
newServiceReferenceIdnewHealthCheckerReferenceId
Those fields are Spring bean names, not class names.
Why trajectories exist
The intended use cases include:
- A/B testing
- tenant-specific behavior
- region-specific behavior
- experimental service implementations
- alternate health-check behavior for selected traffic
The important point is that trajectories change the concrete bean used at runtime without changing the logical Chenile service definition.
How trajectory JSON is loaded
Trajectory JSON is loaded by chenile-core/src/main/java/org/chenile/core/init/ChenileTrajectoryInitializer.java.
The resource pattern comes from:
chenile.trajectory.json.package
Example from the tests:
chenile.trajectory.json.package=classpath*:org/chenile/core/test/trajectories/*.json
Example trajectory JSON
From chenile-core/src/test/resources/org/chenile/core/test/trajectories/t1.json:
{
"id": "t1",
"trajectoryOverrides": {
"mockService": {
"newServiceReferenceId": "t1MockService"
}
}
}
This means:
- trajectory id is
t1 - if logical service
mockServiceis invoked under trajectoryt1 - use Spring bean
t1MockServiceinstead of the default service bean
Registration flow
During initialization, ChenileTrajectoryInitializer.registerTrajectoryDefinition(...):
- iterates over each
TrajectoryOverride - resolves
newServiceReferenceIdto a Spring bean - resolves
newHealthCheckerReferenceIdto aHealthCheckerbean if present - stores the fully wired trajectory in
ChenileConfiguration
This means trajectory JSON is not passive configuration. It is resolved against the live Spring context at startup.
How requests activate a trajectory
The request activates a trajectory through the header:
x-chenile-trajectory-id
This constant is defined in chenile-core/src/main/java/org/chenile/core/context/HeaderUtils.java.
Example:
exchange.setHeader("x-chenile-trajectory-id", "t1");
That is exactly what the test does in chenile-core/src/test/java/org/chenile/core/test/TestChenileCore.java.
A pre-processor can also derive the trajectory from something else in the request, such as the tenant or the
region. It must set both the exchange header and the request context (ContextContainer.setTrajectory), because
the service-reference step reads the exchange while cconfig and workflow resolvers read the context.
RegionToTrajectoryConverter (utils) does this for the region: register it with
chenile.pre.processors=regionToTrajectoryConverter, and a request with x-chenile-region-id but no trajectory gets
the region as its trajectory.
Where the trajectory is applied in the chain
The trajectory is applied in:
chenile-core/src/main/java/org/chenile/core/interceptors/ConstructServiceReference.java
ConstructServiceReference checks the exchange in this order:
- if a service reference is already set, leave it alone
- if mock mode is enabled, use the mock bean
- check the trajectory id from headers and use an override when configured
- otherwise invoke the service’s optional
ServiceSelector - if the selector returns a bean name, use that Spring bean; if it returns
nullor is not configured, use the normal service bean fromChenileServiceDefinition
The selector is a configurable service-level strategy. Trajectory overrides retain precedence over it, and mock invocation retains precedence over both.
This is why trajectories fit naturally into the interceptor chain: they modify dispatch just before invocation.
What changes and what stays the same
When a trajectory is active:
- the logical
serviceDefinitionstays the same - the
operationDefinitionstays the same - the runtime
serviceReferencechanges - the reflected
methodmay be recomputed on the new bean class
So trajectories do not redefine the contract. They only swap the implementation behind the contract.
Relationship to service ids
Trajectory overrides are keyed by Chenile service id, not by controller class or URL.
That means:
- JSON-defined services can be overridden
- annotation-defined services can also be overridden
- the same override mechanism works once both are registered into
ChenileConfiguration
Health checker overrides
TrajectoryOverride can also carry:
newHealthCheckerReferenceId
That allows health checks for a service to vary by trajectory as well.
The registration logic resolves this to a HealthChecker bean during startup.
Test example
The test testTrajectoryRouting() in chenile-core/src/test/java/org/chenile/core/test/TestChenileCore.java shows the full pattern:
- create an exchange for logical service
mockService - set header
x-chenile-trajectory-idtot1 - invoke Chenile
- observe that the result comes from
t1MockServicerather than the default service
That is the clearest practical example in the repository.
Relationship to @ConditionalOnTrajectory
Chenile also provides:
@ConditionalOnTrajectory@ConditionalHealthCheckOnTrajectory
These annotations document the idea of trajectory-specific replacement at the Spring configuration level.
The runtime dispatch decision still uses the same trajectory concepts:
- trajectory id
- logical service id
- alternate bean reference
Mental model
Use this model:
- service definitions describe the stable logical contract
- trajectories choose which implementation bean serves that contract for a request
ConstructServiceReferenceis the point where that choice is applied
Most relevant source files
chenile-core/src/main/java/org/chenile/core/model/TrajectoryDefinition.javachenile-core/src/main/java/org/chenile/core/model/TrajectoryOverride.javachenile-core/src/main/java/org/chenile/core/init/ChenileTrajectoryInitializer.javachenile-core/src/main/java/org/chenile/core/model/ChenileConfiguration.javachenile-core/src/main/java/org/chenile/core/interceptors/ConstructServiceReference.javachenile-core/src/main/java/org/chenile/core/context/HeaderUtils.javachenile-core/src/test/resources/org/chenile/core/test/trajectories/t1.jsonchenile-core/src/test/java/org/chenile/core/test/TestChenileCore.java