狂野飙车6鸿蒙版
471.84M · 2025-10-31
在现代软件开发中,责任链模式是一种常见的设计模式,它允许将请求沿着处理者链进行传递,直到有一个对象处理它为止。这种模式可以有效地解耦请求的发送者和接收者,使得系统更加灵活和可扩展。本文将详细介绍Atlas-Chain责任链框架的设计思路、核心组件以及使用方法。
Atlas-Chain是一个基于Java的责任链框架,旨在简化责任链模式的实现与管理。该框架支持核心模块独立使用和Spring Boot集成,为Java开发者提供了一套完整、灵活且易于使用的责任链解决方案。
Atlas-Chain框架具有以下核心特性:
@ChainHandler注解的处理者自动注册机制Atlas-Chain框架由以下核心组件构成:
graph TD
A[ChainExecutor] --> B[ChainRegistry]
B --> C[BaseHandler]
A --> D[HandlerContext]
C --> D
BaseHandler<P, R>是所有处理者的基类,定义了责任链中处理者的标准接口:
doHandle(HandlerContext<P, R> context):核心业务逻辑方法,返回true继续执行,false中断链路shouldSkip(HandlerContext<P, R> context):判断是否跳过当前处理者onCompleted(HandlerContext<P, R> context):处理完成回调onError(HandlerContext<P, R> context, Exception e):处理异常回调@Slf4j
public abstract class BaseHandler<P, R> {
/**
* 子类必须实现的业务逻辑方法
* 返回true表示继续执行下一个处理者,false表示中断链,不再执行后续节点
* @param context 处理上下文
* @return true继续执行,false中断执行
*/
public abstract boolean doHandle(HandlerContext<P, R> context);
/**
* 可选实现:判断是否跳过当前处理者,默认不跳过
* @param context 处理上下文
* @return true跳过当前处理者,false不跳过
*/
public boolean shouldSkip(HandlerContext<P, R> context) {
return false;
}
// ... 其他回调方法
}
HandlerContext<P, R>用于在处理者之间传递数据,包含:
request:请求参数response:响应结果attributes:用于处理者间共享数据的键值对集合@Data
@AllArgsConstructor
@NoArgsConstructor
public class HandlerContext<P, R> {
// 请求参数
private P request;
// 响应结果
private R response;
// attributes用于在责任链的不同节点之间传递数据
private Map<String, Object> attributes = new HashMap<>();
/**
* 提供类型安全的属性获取方法
* @param key 属性键
* @param type 属性值类型
* @param <V> 泛型类型
* @return 指定类型的属性值
*/
@SuppressWarnings("unchecked")
public <V> V getAttribute(String key, Class<V> type) {
return (V) attributes.get(key);
}
// ... 其他方法
}
ChainRegistry<P, R>负责处理者的注册和管理:
@Data
@NoArgsConstructor
public class ChainRegistry<P, R> {
// 存储每个链ID对应的处理者列表
private Map<String, List<BaseHandler<P, R>>> handlerMap = new HashMap<>();
/**
* 注册处理者
* @param chainId 链ID
* @param handler 处理者
*/
public void registerHandler(String chainId, BaseHandler<P, R> handler) {
handlerMap.computeIfAbsent(chainId, k -> new ArrayList<>()).add(handler);
}
// ... 其他方法
}
ChainExecutor<P, R>负责责任链的执行:
@Data
@AllArgsConstructor
public class ChainExecutor<P, R> {
// 链注册器
private ChainRegistry<P, R> chainRegistry;
// 自定义线程池,用于异步执行
private ExecutorService executorService = Executors.newFixedThreadPool(10);
/**
* 同步执行责任链
* @param chainId 链ID
* @param context 处理上下文
* @return 响应结果
* @throws Exception 执行异常
*/
public R execute(String chainId, HandlerContext<P, R> context) throws Exception {
List<BaseHandler<P, R>> handlers = chainRegistry.buildChain(chainId);
// 同步执行责任链
for (BaseHandler<P, R> handler : handlers) {
try {
// 判断是否跳过当前处理者
if (handler.shouldSkip(context)) {
continue;
}
// 执行处理逻辑
boolean shouldContinue = handler.doHandle(context);
// 执行完成回调
handler.onCompleted(context);
// 判断是否继续执行下一个处理者
if (!shouldContinue) {
break; // 明确返回false,中断责任链
}
} catch (Exception e) {
// 执行错误回调
handler.onError(context, e);
// 异常会直接抛出到最上层,责任链中断执行
throw e;
}
}
return context.getResponse();
}
// ... 异步执行方法
}
Spring Boot集成模块提供了以下特性:
@ChainHandler注解用于标记处理者类,支持:
value():指定链的IDorder():指定处理者在链中的顺序(数值越小优先级越高)/**
* 责任链处理者注解,用于标记处理者类
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface ChainHandler {
/**
* 链的ID
* @return 链ID
*/
String value();
/**
* 处理者在链中的顺序,数值越小优先级越高
* @return 顺序值
*/
int order() default 0;
}
ChainHandlerAutoConfiguration自动配置类提供了:
ChainRegistry Bean的自动创建ChainExecutor Bean的自动创建@Data
@Configuration
@ComponentScan(basePackages = "io.github.nemoob.atlas.chain.spring")
@EnableConfigurationProperties(ChainHandlerProperties.class)
public class ChainHandlerAutoConfiguration {
/**
* 创建链注册器Bean
* @param <P> Param类型
* @param <R> Response类型
* @return 链注册器
*/
@Bean
@ConditionalOnMissingBean
public <P, R> ChainRegistry<P, R> chainRegistry() {
return new ChainRegistry<>();
}
// ... 其他Bean创建方法
}
ChainHandlerRegistrar负责扫描并注册带有@ChainHandler注解的处理者。
@Slf4j
@Component
public class ChainHandlerRegistrar implements BeanPostProcessor, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// 检查bean是否有@ChainHandler注解
ChainHandler chainHandler = AnnotationUtils.findAnnotation(bean.getClass(), ChainHandler.class);
if (chainHandler != null && bean instanceof BaseHandler) {
// 获取链注册器
Map<String, ChainRegistry> registryBeans = applicationContext.getBeansOfType(ChainRegistry.class);
if (!registryBeans.isEmpty()) {
ChainRegistry registry = registryBeans.values().iterator().next();
// 注册处理者
String chainId = chainHandler.value();
registry.registerHandler(chainId, (BaseHandler) bean);
log.info("Registered handler {} for chain {}", bean.getClass().getSimpleName(), chainId);
}
}
return bean;
}
}
// 1. 定义请求和响应类
public class UserRequest {
private String username;
private String password;
// getter/setter
}
public class UserResponse {
private String message;
private boolean success;
// getter/setter
}
// 2. 实现处理者
public class AuthenticationHandler extends BaseHandler<UserRequest, UserResponse> {
@Override
protected boolean doHandle(HandlerContext<UserRequest, UserResponse> context) {
UserRequest request = context.getRequest();
// 执行认证逻辑
context.setAttribute("userId", "12345");
return true; // 继续执行下一个处理者
}
}
// 3. 注册和执行
ChainRegistry<UserRequest, UserResponse> registry = new ChainRegistry<>();
registry.registerHandler("user-auth", new AuthenticationHandler());
ChainExecutor<UserRequest, UserResponse> executor = new ChainExecutor<>(registry);
HandlerContext<UserRequest, UserResponse> context = new HandlerContext<>(new UserRequest(), new UserResponse());
executor.execute("user-auth", context);
// 1. 配置文件
chain:
handler:
core-pool-size: 5
max-pool-size: 10
keep-alive-time: 60
queue-capacity: 100
// 2. 实现处理者
@ChainHandler(value = "user-process", order = 1)
@Component
public class UserValidateHandler extends BaseHandler<UserRequest, UserResponse> {
@Override
protected boolean doHandle(HandlerContext<UserRequest, UserResponse> context) {
UserRequest request = context.getRequest();
if (request.getUserId() == null) {
context.setResponse(new UserResponse("Validation failed"));
return false; // 验证失败,终止链路
}
context.setAttribute("validated", true);
return true;
}
}
// 3. 使用
@Autowired
private ChainExecutor<UserRequest, UserResponse> chainExecutor;
public UserResponse processUser(UserRequest request) {
HandlerContext<UserRequest, UserResponse> context = new HandlerContext<>(request, new UserResponse());
try {
return chainExecutor.execute("user-process", context);
} catch (Exception e) {
// 处理异常
}
}
框架支持自定义线程池配置,可根据业务场景调整线程池参数:
通过CompletableFuture实现异步执行,提高系统吞吐量。
doHandle方法应明确返回true/false,避免歧义onError回调处理异常情况Atlas-Chain责任链框架通过简洁的设计和强大的功能,为Java开发者提供了一套完整的责任链解决方案。无论是独立使用还是与Spring Boot集成,都能满足不同场景下的业务需求。框架的模块化设计和良好的扩展性使其能够适应各种复杂的应用场景。
通过使用Atlas-Chain,开发者可以更加专注于业务逻辑的实现,而无需关心责任链的底层实现细节。同时,框架提供的丰富特性和灵活配置选项,使得系统具有更好的可维护性和可扩展性。