Class VariantRouter<T>
java.lang.Object
cloud.opencode.base.feature.proxy.VariantRouter<T>
- Type Parameters:
T- the variant type | 变体类型Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Variant Router
变体路由器
Routes method calls to appropriate variants for A/B testing.
将方法调用路由到适当的A/B测试变体。
Features | 主要功能:
- Multiple variant registration - 多变体注册
- Consistent user routing - 一致性用户路由
- Percentage-based traffic split - 基于百分比的流量分配
- Context-aware selection - 上下文感知选择
Usage Examples | 使用示例:
// Define variant implementations
CheckoutService variantA = new CheckoutServiceV1();
CheckoutService variantB = new CheckoutServiceV2();
// Create router with percentage-based traffic split
VariantRouter<CheckoutService> router = VariantRouter.<CheckoutService>builder("checkout-flow")
.variant("A", variantA, 50) // 50% traffic
.variant("B", variantB, 50) // 50% traffic
.build();
// Route based on user context
FeatureContext ctx = FeatureContext.ofUser("user123");
CheckoutService service = router.route(ctx);
service.checkout(order);
// Or use direct invocation
router.execute(ctx, s -> s.checkout(order));
- Since:
- JDK 25, opencode-base-feature V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for VariantRouter VariantRouter构建器static classWrapper for method-based variants 方法变体的包装器static classException thrown when no variant is available 当没有可用变体时抛出的异常 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> VariantRouter.Builder<T> Create a builder for the router 创建路由器构建器<R> Rexecute(FeatureContext context, Function<T, R> action) Execute action on selected variant 在选择的变体上执行操作voidexecuteVoid(FeatureContext context, Consumer<T> action) Execute void action on selected variant 在选择的变体上执行无返回值操作static <T> VariantRouter<VariantRouter.MethodVariant<T>> fromAnnotations(String featureKey, T target) Create router from annotated methods 从注解方法创建路由器Get the feature key 获取功能键getSelectedVariantId(FeatureContext context) Get the variant that would be selected for a context 获取将为上下文选择的变体IDgetVariant(String variantId) Get a specific variant by ID 根据ID获取特定变体Get all registered variant IDs 获取所有注册的变体IDroute()Route with empty context 使用空上下文路由route(FeatureContext context) Route to a variant based on context 根据上下文路由到变体routeForUser(String userId) Route for a specific user 为特定用户路由
-
Method Details
-
builder
Create a builder for the router 创建路由器构建器- Type Parameters:
T- the variant type | 变体类型- Parameters:
featureKey- the feature key for the A/B test | A/B测试的功能键- Returns:
- new builder | 新的构建器
-
fromAnnotations
public static <T> VariantRouter<VariantRouter.MethodVariant<T>> fromAnnotations(String featureKey, T target) Create router from annotated methods 从注解方法创建路由器- Type Parameters:
T- the target type | 目标类型- Parameters:
featureKey- the feature key | 功能键target- the target object containing @FeatureVariant methods | 包含@FeatureVariant方法的目标对象- Returns:
- variant router | 变体路由器
-
route
Route to a variant based on context 根据上下文路由到变体- Parameters:
context- the feature context | 功能上下文- Returns:
- the selected variant | 选择的变体
- Throws:
VariantRouter.NoVariantException- if no variants are registered | 如果没有注册变体
-
route
-
routeForUser
-
execute
Execute action on selected variant 在选择的变体上执行操作- Type Parameters:
R- the return type | 返回类型- Parameters:
context- the feature context | 功能上下文action- the action to execute | 要执行的操作- Returns:
- the result | 结果
-
executeVoid
Execute void action on selected variant 在选择的变体上执行无返回值操作- Parameters:
context- the feature context | 功能上下文action- the action to execute | 要执行的操作
-
getVariant
-
getSelectedVariantId
Get the variant that would be selected for a context 获取将为上下文选择的变体ID- Parameters:
context- the feature context | 功能上下文- Returns:
- the variant identifier | 变体标识符
-
getVariantIds
-
getFeatureKey
-