An open API service indexing awesome lists of open source software.

https://github.com/nemoob/atlas-chain

一个基于Java的责任链框架,支持核心模块独立使用和Spring Boot集成。
https://github.com/nemoob/atlas-chain

chain java spring

Last synced: 8 months ago
JSON representation

一个基于Java的责任链框架,支持核心模块独立使用和Spring Boot集成。

Awesome Lists containing this project

README

          

# Atlas-Chain 责任链框架

[![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)
[![GitHub stars](https://img.shields.io/github/stars/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/network)
[![GitHub issues](https://img.shields.io/github/issues/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/issues)
[![GitHub license](https://img.shields.io/github/license/nemoob/atlas-chain?style=flat-square)](https://github.com/nemoob/atlas-chain/blob/main/LICENSE)
[![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)
[![Java Version](https://img.shields.io/badge/Java-8%2B-brightgreen?style=flat-square)](https://www.oracle.com/java/)
[![Spring Boot](https://img.shields.io/badge/Spring%20Boot-2.2%2B-brightgreen?style=flat-square)](https://spring.io/projects/spring-boot)

一个基于Java的责任链框架,支持核心模块独立使用和Spring Boot集成。

## 项目结构

```
atlas-chain/
├── atlas-chain-core/ # 核心模块,无Spring依赖,可独立使用
├── atlas-chain-spring-boot-starter/ # Spring Boot集成模块
├── examples/ # 使用示例
│ ├── core-example/ # core模块使用示例
│ └── spring-example/ # spring模块使用示例
└── pom.xml # 父项目POM
```

## 模块说明

### atlas-chain-core(核心模块)

核心模块不依赖Spring框架,可以在任何Java 8+环境中独立使用。

#### 主要组件

1. **HandlerContext

** - 责任链上下文,用于在处理者之间传递数据
2. **BaseHandler

** - 抽象处理者类,所有具体处理者需要继承此类
3. **ChainRegistry

** - 链注册器,负责注册和管理处理者
4. **ChainExecutor

** - 链执行器,负责执行责任链

### atlas-chain-spring-boot-starter(Spring Boot集成模块)

Spring Boot集成模块提供了自动配置和注解驱动功能。

#### 主要组件

1. **@ChainHandler** - 注解,用于标记处理者类
2. **ChainHandlerAutoConfiguration** - 自动配置类
3. **ChainHandlerProperties** - 配置属性类
4. **ChainHandlerRegistrar** - 处理者注册器

## 使用方法

### Maven依赖

对于纯Java项目使用core模块:

```xml

io.github.nemoob
atlas-chain-core
0.1.0

```

对于Spring Boot项目使用starter模块:

```xml

io.github.nemoob
atlas-chain-spring-boot-starter
0.1.0

```

### 核心模块使用示例

```java
// 创建注册器
ChainRegistry registry = new ChainRegistry<>();

// 创建处理者
AuthenticationHandler authHandler = new AuthenticationHandler();
AuthorizationHandler authzHandler = new AuthorizationHandler();
BusinessLogicHandler businessHandler = new BusinessLogicHandler();

// 注册处理者到指定链路
String chainId = "user-process-chain";
registry.registerHandler(chainId, authHandler);
registry.registerHandler(chainId, authzHandler);
registry.registerHandler(chainId, businessHandler);

// 创建执行器
ChainExecutor executor = new ChainExecutor<>(registry);

// 创建上下文
UserRequest request = new UserRequest("testUser", "testPassword");
HandlerContext context = new HandlerContext<>(request, null);

// 执行责任链
UserResponse response = executor.execute(chainId, context);

// 关闭执行器
executor.shutdown();
```

### Spring Boot集成使用示例

1. 在处理者类上添加@ChainHandler和@Component注解:

```java
@Component // 必须添加,让Spring管理此Bean
@ChainHandler(value = "user-process", order = 1)
public class UserValidateHandler extends BaseHandler {
@Override
protected boolean doHandle(HandlerContext context) {
// 处理逻辑
return true;
}
}
```

2. 在服务类中注入ChainExecutor并使用:

```java
@Service
public class UserService {

@Autowired
private ChainExecutor chainExecutor;

public UserResponse processUser(Long userId) {
UserRequest request = new UserRequest(userId, "testUser");
HandlerContext context = new HandlerContext<>(request, null);
return chainExecutor.execute("user-process", context);
}
}
```

## 配置属性

在Spring Boot项目中,可以通过以下属性配置线程池:

```yaml
chain:
handler:
core-pool-size: 5
max-pool-size: 10
keep-alive-time: 60
queue-capacity: 100
```

### 🧵 多线程池支持

Atlas-Chain 支持灵活的线程池配置,既可以使用默认配置,也可以自定义线程池。

#### 默认配置(推荐)

**零配置开箱即用**!框架会自动创建 `ChainExecutor` Bean:

```java
// 自动配置 - 使用默认线程池
@Bean("chainExecutor")
@ConditionalOnMissingBean(name = "chainExecutor")
public ChainExecutor chainExecutor(ChainRegistry chainRegistry) {
return new ChainExecutor<>(chainRegistry); // 内部创建默认线程池
}
```

**默认线程池配置:**
- **核心线程数**: 5
- **最大线程数**: 10
- **空闲时间**: 60秒
- **队列容量**: 100
- **线程名称**: `atlas-chain-{id}`
- **拒绝策略**: CallerRunsPolicy

**使用方式:**
```java
@Service
public class UserService {

@Autowired
private ChainExecutor chainExecutor; // 直接注入即可

public Object processUser(Object request) {
HandlerContext context = new HandlerContext<>(request);
return chainExecutor.execute("userChain", context);
}
}
```

#### 自定义线程池

如果需要自定义线程池,直接创建自己的 `ChainExecutor` Bean:

```java
@Configuration
public class CustomChainConfig {

@Bean("chainExecutor")
@Primary // 覆盖默认配置
public ChainExecutor customChainExecutor(ChainRegistry chainRegistry) {
// 创建自定义线程池
ExecutorService customPool = new ThreadPoolExecutor(
10, 20, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(200),
r -> {
Thread t = new Thread(r);
t.setName("custom-chain-" + t.getId());
return t;
}
);

return new ChainExecutor<>(chainRegistry, customPool);
}
}
```

**方式三:为不同责任链配置专用线程池**

```java
@Configuration
public class MultiChainConfig {

// 用户处理专用线程池
@Bean("userChainExecutor")
public ChainExecutor userChainExecutor(
ChainRegistry registry) {

ExecutorService userPool = new ThreadPoolExecutor(
5, 10, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(50),
r -> new Thread(r, "user-chain-" + System.currentTimeMillis())
);

return new ChainExecutor<>(registry, userPool);
}

// 订单处理专用线程池
@Bean("orderChainExecutor")
public ChainExecutor orderChainExecutor(
ChainRegistry registry) {

ExecutorService orderPool = new ThreadPoolExecutor(
8, 15, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(100),
r -> new Thread(r, "order-chain-" + System.currentTimeMillis())
);

return new ChainExecutor<>(registry, orderPool);
}

@Bean("highPriorityChainExecutor")
public ChainExecutor highPriorityChainExecutor(
ChainRegistry registry,
@Qualifier("highPriorityExecutorService") ExecutorService executorService) {
// 使用现有的高优先级线程池
return new ChainExecutor<>(registry, executorService);
}
}
```

然后在服务中使用@Qualifier注入指定的执行器:

```java
@Service
public class UserService {

@Autowired
@Qualifier("userChainExecutor")
private ChainExecutor userChainExecutor;

@Autowired
@Qualifier("orderChainExecutor")
private ChainExecutor orderChainExecutor;

public UserResponse processUser(UserRequest request) {
HandlerContext context =
new HandlerContext<>(request, null);
return userChainExecutor.execute("user-chain", context);
}

public OrderResponse processOrder(OrderRequest request) {
HandlerContext context =
new HandlerContext<>(request, null);
return orderChainExecutor.execute("order-chain", context);
}
}
```

#### 配置建议

**🎯 选择合适的配置方式:**

1. **默认配置** - 适用于大多数场景,无需任何配置
2. **方式一** - 需要调整线程池参数,但使用单一执行器
3. **方式二** - 需要完全控制 ChainExecutor 的创建
4. **方式三** - 不同业务链需要不同的线程池策略

**⚡ 性能优化建议:**

```java
// 高性能配置示例
@Bean("handlerExecutorService")
public ExecutorService handlerExecutorService() {
return new ThreadPoolExecutor(
Runtime.getRuntime().availableProcessors(), // 核心线程数 = CPU核数
Runtime.getRuntime().availableProcessors() * 2, // 最大线程数 = CPU核数 * 2
60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000), // 适当的队列大小
new ThreadFactoryBuilder()
.setNameFormat("atlas-chain-%d")
.setDaemon(false)
.build(),
new ThreadPoolExecutor.CallerRunsPolicy() // 优雅降级
);
}
```

**🔧 Bean冲突解决:**

当项目中存在多个 `ExecutorService` Bean 时:

```java
@Configuration
public class ConflictResolver {

@Bean("handlerExecutorService") // 框架会优先使用这个
public ExecutorService handlerExecutorService() {
return new ThreadPoolExecutor(/* 配置 */);
}

@Bean("otherExecutorService") // 其他业务使用
public ExecutorService otherExecutorService() {
return Executors.newCachedThreadPool();
}
}
```

## 特性

1. **基于注解的链路注册机制** - 通过@ChainHandler注解和ID执行责任链
2. **节点跳过机制** - boolean doHandle返回false时终止后续节点执行
3. **异步执行机制** - 支持异步执行,可配置自定义线程池
4. **泛型约束** - HandlerContext使用泛型约束,支持参数和响应类型分离
5. **类型安全属性** - 节点间通信属性支持类型约束
6. **模块化设计** - core包(独立使用) + spring-boot-starter包
7. **兼容性** - JDK 1.8 + Spring Boot 2.2 + Spring 5.2
8. **代码简化** - 全包使用Lombok,添加完整注释

## 构建和测试

```bash
# 构建整个项目
mvn clean install

# 运行核心模块示例
mvn exec:java -pl examples/core-example

# 运行Spring Boot示例
mvn spring-boot:run -pl examples/spring-example

# 运行测试
mvn test
```

## 📊 项目统计

### 基础统计
![GitHub repo size](https://img.shields.io/github/repo-size/nemoob/atlas-chain?style=flat-square)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/nemoob/atlas-chain?style=flat-square)
![Lines of code](https://img.shields.io/tokei/lines/github/nemoob/atlas-chain?style=flat-square)
![GitHub language count](https://img.shields.io/github/languages/count/nemoob/atlas-chain?style=flat-square)
![GitHub top language](https://img.shields.io/github/languages/top/nemoob/atlas-chain?style=flat-square)

### 活跃度统计
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/nemoob/atlas-chain?style=flat-square)
![GitHub commit activity](https://img.shields.io/github/commit-activity/w/nemoob/atlas-chain?style=flat-square)
![GitHub last commit](https://img.shields.io/github/last-commit/nemoob/atlas-chain?style=flat-square)
![GitHub contributors](https://img.shields.io/github/contributors/nemoob/atlas-chain?style=flat-square)

### 下载统计
![GitHub all releases](https://img.shields.io/github/downloads/nemoob/atlas-chain/total?style=flat-square)
![GitHub release (latest by date)](https://img.shields.io/github/downloads/nemoob/atlas-chain/latest/total?style=flat-square)

### 历史趋势
![GitHub Repo stars](https://img.shields.io/github/stars/nemoob/atlas-chain?style=social)
![GitHub watchers](https://img.shields.io/github/watchers/nemoob/atlas-chain?style=social)

**Star History**

[![Star History Chart](https://api.star-history.com/svg?repos=nemoob/atlas-chain&type=Date)](https://star-history.com/#nemoob/atlas-chain&Date)