{"id":31999609,"url":"https://github.com/nemoob/atlas-chain","last_synced_at":"2025-10-15T14:33:01.992Z","repository":{"id":312710285,"uuid":"1048449406","full_name":"nemoob/atlas-chain","owner":"nemoob","description":"一个基于Java的责任链框架，支持核心模块独立使用和Spring Boot集成。","archived":false,"fork":false,"pushed_at":"2025-09-02T01:58:29.000Z","size":54,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-20T09:24:58.840Z","etag":null,"topics":["chain","java","spring"],"latest_commit_sha":null,"homepage":"https://nemoob.cn","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nemoob.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-01T13:10:38.000Z","updated_at":"2025-09-17T08:38:08.000Z","dependencies_parsed_at":"2025-09-01T15:22:47.923Z","dependency_job_id":"2c015107-3190-44c0-9e75-80b7fff84929","html_url":"https://github.com/nemoob/atlas-chain","commit_stats":null,"previous_names":["nemoob/atlas-chain"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nemoob/atlas-chain","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nemoob%2Fatlas-chain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nemoob%2Fatlas-chain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nemoob%2Fatlas-chain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nemoob%2Fatlas-chain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nemoob","download_url":"https://codeload.github.com/nemoob/atlas-chain/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nemoob%2Fatlas-chain/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279085465,"owners_count":26100017,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-15T02:00:07.814Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chain","java","spring"],"created_at":"2025-10-15T14:32:58.102Z","updated_at":"2025-10-15T14:33:01.963Z","avatar_url":"https://github.com/nemoob.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Atlas-Chain 责任链框架\n\n[![GitHub release (latest by date)](https://img.shields.io/github/v/release/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/releases)\n[![GitHub stars](https://img.shields.io/github/stars/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/stargazers)\n[![GitHub forks](https://img.shields.io/github/forks/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/network)\n[![GitHub issues](https://img.shields.io/github/issues/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/issues)\n[![GitHub license](https://img.shields.io/github/license/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/blob/main/LICENSE)\n[![Maven Central](https://img.shields.io/maven-central/v/io.github.nemoob/atlas-chain-core?style=flat-square)](https://search.maven.org/artifact/io.github.nemoob/atlas-chain-core)\n[![Java Version](https://img.shields.io/badge/Java-8%2B-brightgreen?style=flat-square)](https://www.oracle.com/java/)\n[![Spring Boot](https://img.shields.io/badge/Spring%20Boot-2.2%2B-brightgreen?style=flat-square)](https://spring.io/projects/spring-boot)\n\n一个基于Java的责任链框架，支持核心模块独立使用和Spring Boot集成。\n\n## 项目结构\n\n```\natlas-chain/\n├── atlas-chain-core/              # 核心模块，无Spring依赖，可独立使用\n├── atlas-chain-spring-boot-starter/ # Spring Boot集成模块\n├── examples/                      # 使用示例\n│   ├── core-example/              # core模块使用示例\n│   └── spring-example/            # spring模块使用示例\n└── pom.xml                        # 父项目POM\n```\n\n## 模块说明\n\n### atlas-chain-core（核心模块）\n\n核心模块不依赖Spring框架，可以在任何Java 8+环境中独立使用。\n\n#### 主要组件\n\n1. **HandlerContext\u003cP, R\u003e** - 责任链上下文，用于在处理者之间传递数据\n2. **BaseHandler\u003cP, R\u003e** - 抽象处理者类，所有具体处理者需要继承此类\n3. **ChainRegistry\u003cP, R\u003e** - 链注册器，负责注册和管理处理者\n4. **ChainExecutor\u003cP, R\u003e** - 链执行器，负责执行责任链\n\n### atlas-chain-spring-boot-starter（Spring Boot集成模块）\n\nSpring Boot集成模块提供了自动配置和注解驱动功能。\n\n#### 主要组件\n\n1. **@ChainHandler** - 注解，用于标记处理者类\n2. **ChainHandlerAutoConfiguration** - 自动配置类\n3. **ChainHandlerProperties** - 配置属性类\n4. **ChainHandlerRegistrar** - 处理者注册器\n\n## 使用方法\n\n### Maven依赖\n\n对于纯Java项目使用core模块：\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.nemoob\u003c/groupId\u003e\n    \u003cartifactId\u003eatlas-chain-core\u003c/artifactId\u003e\n    \u003cversion\u003e0.1.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n对于Spring Boot项目使用starter模块：\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.nemoob\u003c/groupId\u003e\n    \u003cartifactId\u003eatlas-chain-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e0.1.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### 核心模块使用示例\n\n```java\n// 创建注册器\nChainRegistry\u003cUserRequest, UserResponse\u003e registry = new ChainRegistry\u003c\u003e();\n\n// 创建处理者\nAuthenticationHandler authHandler = new AuthenticationHandler();\nAuthorizationHandler authzHandler = new AuthorizationHandler();\nBusinessLogicHandler businessHandler = new BusinessLogicHandler();\n\n// 注册处理者到指定链路\nString chainId = \"user-process-chain\";\nregistry.registerHandler(chainId, authHandler);\nregistry.registerHandler(chainId, authzHandler);\nregistry.registerHandler(chainId, businessHandler);\n\n// 创建执行器\nChainExecutor\u003cUserRequest, UserResponse\u003e executor = new ChainExecutor\u003c\u003e(registry);\n\n// 创建上下文\nUserRequest request = new UserRequest(\"testUser\", \"testPassword\");\nHandlerContext\u003cUserRequest, UserResponse\u003e context = new HandlerContext\u003c\u003e(request, null);\n\n// 执行责任链\nUserResponse response = executor.execute(chainId, context);\n\n// 关闭执行器\nexecutor.shutdown();\n```\n\n### Spring Boot集成使用示例\n\n1. 在处理者类上添加@ChainHandler和@Component注解：\n\n```java\n@Component  // 必须添加，让Spring管理此Bean\n@ChainHandler(value = \"user-process\", order = 1)\npublic class UserValidateHandler extends BaseHandler\u003cUserRequest, UserResponse\u003e {\n    @Override\n    protected boolean doHandle(HandlerContext\u003cUserRequest, UserResponse\u003e context) {\n        // 处理逻辑\n        return true;\n    }\n}\n```\n\n2. 在服务类中注入ChainExecutor并使用：\n\n```java\n@Service\npublic class UserService {\n    \n    @Autowired\n    private ChainExecutor\u003cUserRequest, UserResponse\u003e chainExecutor;\n    \n    public UserResponse processUser(Long userId) {\n        UserRequest request = new UserRequest(userId, \"testUser\");\n        HandlerContext\u003cUserRequest, UserResponse\u003e context = new HandlerContext\u003c\u003e(request, null);\n        return chainExecutor.execute(\"user-process\", context);\n    }\n}\n```\n\n## 配置属性\n\n在Spring Boot项目中，可以通过以下属性配置线程池：\n\n```yaml\nchain:\n  handler:\n    core-pool-size: 5\n    max-pool-size: 10\n    keep-alive-time: 60\n    queue-capacity: 100\n```\n\n### 🧵 多线程池支持\n\nAtlas-Chain 支持灵活的线程池配置，既可以使用默认配置，也可以自定义线程池。\n\n#### 默认配置（推荐）\n\n**零配置开箱即用**！框架会自动创建 `ChainExecutor` Bean：\n\n```java\n// 自动配置 - 使用默认线程池\n@Bean(\"chainExecutor\")\n@ConditionalOnMissingBean(name = \"chainExecutor\")\npublic ChainExecutor\u003cObject, Object\u003e chainExecutor(ChainRegistry\u003cObject, Object\u003e chainRegistry) {\n    return new ChainExecutor\u003c\u003e(chainRegistry); // 内部创建默认线程池\n}\n```\n\n**默认线程池配置：**\n- **核心线程数**: 5\n- **最大线程数**: 10  \n- **空闲时间**: 60秒\n- **队列容量**: 100\n- **线程名称**: `atlas-chain-{id}`\n- **拒绝策略**: CallerRunsPolicy\n\n**使用方式：**\n```java\n@Service\npublic class UserService {\n    \n    @Autowired\n    private ChainExecutor\u003cObject, Object\u003e chainExecutor; // 直接注入即可\n    \n    public Object processUser(Object request) {\n        HandlerContext\u003cObject, Object\u003e context = new HandlerContext\u003c\u003e(request);\n        return chainExecutor.execute(\"userChain\", context);\n    }\n}\n```\n\n#### 自定义线程池\n\n如果需要自定义线程池，直接创建自己的 `ChainExecutor` Bean：\n\n```java\n@Configuration\npublic class CustomChainConfig {\n    \n    @Bean(\"chainExecutor\")\n    @Primary  // 覆盖默认配置\n    public ChainExecutor\u003cObject, Object\u003e customChainExecutor(ChainRegistry\u003cObject, Object\u003e chainRegistry) {\n        // 创建自定义线程池\n        ExecutorService customPool = new ThreadPoolExecutor(\n            10, 20, 60L, TimeUnit.SECONDS,\n            new LinkedBlockingQueue\u003c\u003e(200),\n            r -\u003e {\n                Thread t = new Thread(r);\n                t.setName(\"custom-chain-\" + t.getId());\n                return t;\n            }\n        );\n        \n        return new ChainExecutor\u003c\u003e(chainRegistry, customPool);\n    }\n}\n```\n\n**方式三：为不同责任链配置专用线程池**\n\n```java\n@Configuration\npublic class MultiChainConfig {\n    \n    // 用户处理专用线程池\n    @Bean(\"userChainExecutor\")\n    public ChainExecutor\u003cUserRequest, UserResponse\u003e userChainExecutor(\n            ChainRegistry\u003cUserRequest, UserResponse\u003e registry) {\n        \n        ExecutorService userPool = new ThreadPoolExecutor(\n            5, 10, 60L, TimeUnit.SECONDS,\n            new LinkedBlockingQueue\u003c\u003e(50),\n            r -\u003e new Thread(r, \"user-chain-\" + System.currentTimeMillis())\n        );\n        \n        return new ChainExecutor\u003c\u003e(registry, userPool);\n    }\n    \n    // 订单处理专用线程池\n    @Bean(\"orderChainExecutor\")\n    public ChainExecutor\u003cOrderRequest, OrderResponse\u003e orderChainExecutor(\n            ChainRegistry\u003cOrderRequest, OrderResponse\u003e registry) {\n        \n        ExecutorService orderPool = new ThreadPoolExecutor(\n            8, 15, 60L, TimeUnit.SECONDS,\n            new LinkedBlockingQueue\u003c\u003e(100),\n            r -\u003e new Thread(r, \"order-chain-\" + System.currentTimeMillis())\n        );\n        \n        return new ChainExecutor\u003c\u003e(registry, orderPool);\n    }\n    \n    @Bean(\"highPriorityChainExecutor\")\n    public ChainExecutor\u003cHighPriorityRequest, HighPriorityResponse\u003e highPriorityChainExecutor(\n            ChainRegistry\u003cHighPriorityRequest, HighPriorityResponse\u003e registry,\n            @Qualifier(\"highPriorityExecutorService\") ExecutorService executorService) {\n        // 使用现有的高优先级线程池\n        return new ChainExecutor\u003c\u003e(registry, executorService);\n    }\n}\n```\n\n然后在服务中使用@Qualifier注入指定的执行器：\n\n```java\n@Service\npublic class UserService {\n    \n    @Autowired\n    @Qualifier(\"userChainExecutor\")\n    private ChainExecutor\u003cUserRequest, UserResponse\u003e userChainExecutor;\n    \n    @Autowired\n    @Qualifier(\"orderChainExecutor\")\n    private ChainExecutor\u003cOrderRequest, OrderResponse\u003e orderChainExecutor;\n    \n    public UserResponse processUser(UserRequest request) {\n        HandlerContext\u003cUserRequest, UserResponse\u003e context = \n            new HandlerContext\u003c\u003e(request, null);\n        return userChainExecutor.execute(\"user-chain\", context);\n    }\n    \n    public OrderResponse processOrder(OrderRequest request) {\n        HandlerContext\u003cOrderRequest, OrderResponse\u003e context = \n            new HandlerContext\u003c\u003e(request, null);\n        return orderChainExecutor.execute(\"order-chain\", context);\n    }\n}\n```\n\n#### 配置建议\n\n**🎯 选择合适的配置方式：**\n\n1. **默认配置** - 适用于大多数场景，无需任何配置\n2. **方式一** - 需要调整线程池参数，但使用单一执行器\n3. **方式二** - 需要完全控制 ChainExecutor 的创建\n4. **方式三** - 不同业务链需要不同的线程池策略\n\n**⚡ 性能优化建议：**\n\n```java\n// 高性能配置示例\n@Bean(\"handlerExecutorService\")\npublic ExecutorService handlerExecutorService() {\n    return new ThreadPoolExecutor(\n        Runtime.getRuntime().availableProcessors(), // 核心线程数 = CPU核数\n        Runtime.getRuntime().availableProcessors() * 2, // 最大线程数 = CPU核数 * 2\n        60L, TimeUnit.SECONDS,\n        new LinkedBlockingQueue\u003c\u003e(1000), // 适当的队列大小\n        new ThreadFactoryBuilder()\n            .setNameFormat(\"atlas-chain-%d\")\n            .setDaemon(false)\n            .build(),\n        new ThreadPoolExecutor.CallerRunsPolicy() // 优雅降级\n    );\n}\n```\n\n**🔧 Bean冲突解决：**\n\n当项目中存在多个 `ExecutorService` Bean 时：\n\n```java\n@Configuration\npublic class ConflictResolver {\n    \n    @Bean(\"handlerExecutorService\")  // 框架会优先使用这个\n    public ExecutorService handlerExecutorService() {\n        return new ThreadPoolExecutor(/* 配置 */);\n    }\n    \n    @Bean(\"otherExecutorService\")  // 其他业务使用\n    public ExecutorService otherExecutorService() {\n        return Executors.newCachedThreadPool();\n    }\n}\n```\n\n## 特性\n\n1. **基于注解的链路注册机制** - 通过@ChainHandler注解和ID执行责任链\n2. **节点跳过机制** - boolean doHandle返回false时终止后续节点执行\n3. **异步执行机制** - 支持异步执行，可配置自定义线程池\n4. **泛型约束** - HandlerContext使用泛型约束，支持参数和响应类型分离\n5. **类型安全属性** - 节点间通信属性支持类型约束\n6. **模块化设计** - core包(独立使用) + spring-boot-starter包\n7. **兼容性** - JDK 1.8 + Spring Boot 2.2 + Spring 5.2\n8. **代码简化** - 全包使用Lombok，添加完整注释\n\n## 构建和测试\n\n```bash\n# 构建整个项目\nmvn clean install\n\n# 运行核心模块示例\nmvn exec:java -pl examples/core-example\n\n# 运行Spring Boot示例\nmvn spring-boot:run -pl examples/spring-example\n\n# 运行测试\nmvn test\n```\n\n## 📊 项目统计\n\n### 基础统计\n![GitHub repo size](https://img.shields.io/github/repo-size/nemoob/atlas-chain?style=flat-square)\n![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/nemoob/atlas-chain?style=flat-square)\n![Lines of code](https://img.shields.io/tokei/lines/github/nemoob/atlas-chain?style=flat-square)\n![GitHub language count](https://img.shields.io/github/languages/count/nemoob/atlas-chain?style=flat-square)\n![GitHub top language](https://img.shields.io/github/languages/top/nemoob/atlas-chain?style=flat-square)\n\n### 活跃度统计\n![GitHub commit activity](https://img.shields.io/github/commit-activity/m/nemoob/atlas-chain?style=flat-square)\n![GitHub commit activity](https://img.shields.io/github/commit-activity/w/nemoob/atlas-chain?style=flat-square)\n![GitHub last commit](https://img.shields.io/github/last-commit/nemoob/atlas-chain?style=flat-square)\n![GitHub contributors](https://img.shields.io/github/contributors/nemoob/atlas-chain?style=flat-square)\n\n### 下载统计\n![GitHub all releases](https://img.shields.io/github/downloads/nemoob/atlas-chain/total?style=flat-square)\n![GitHub release (latest by date)](https://img.shields.io/github/downloads/nemoob/atlas-chain/latest/total?style=flat-square)\n\n### 历史趋势\n![GitHub Repo stars](https://img.shields.io/github/stars/nemoob/atlas-chain?style=social)\n![GitHub watchers](https://img.shields.io/github/watchers/nemoob/atlas-chain?style=social)\n\n**Star History**\n\n[![Star History Chart](https://api.star-history.com/svg?repos=nemoob/atlas-chain\u0026type=Date)](https://star-history.com/#nemoob/atlas-chain\u0026Date)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnemoob%2Fatlas-chain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnemoob%2Fatlas-chain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnemoob%2Fatlas-chain/lists"}