Skip to content

Latest commit

 

History

History
68 lines (52 loc) · 2.71 KB

gateway-context.md

File metadata and controls

68 lines (52 loc) · 2.71 KB
name flow-engine-framework/gateway-context
module flow-engine-framework
description 网关上下文,提供操作者网关的注入和线程级缓存
status 已实现
scope 后端
source 项目自有
import com.codingapi.flow:flow-engine-framework
symbols
GatewayContext
FlowOperatorGateway
FlowOperatorLocalThreadCache
content_hash 9a35fe960766c3a944ad60043ea71ccd5915e14add45650eed22e6771b95473b

解决什么问题

提供流程引擎与外部系统集成的网关抽象,解决以下问题:

  • 操作者信息获取:FlowOperatorGateway 抽象操作者(用户)信息的获取,框架不依赖具体的用户系统
  • 网关上下文注入:GatewayContext 作为单例持有网关实现,通过 Spring 自动配置注入
  • 线程级缓存:FlowOperatorLocalThreadCache 在同一次请求中缓存操作者信息,避免重复查询

如何使用

核心组件

组件 职责
FlowOperatorGateway 网关接口,定义 get(OperatorKey) 和 findByKeys(keys) 方法;默认身份便捷方法由它们派生
GatewayContext 单例上下文,持有 FlowOperatorGateway 实现
FlowOperatorLocalThreadCache ThreadLocal 缓存,同请求内缓存操作者信息

FlowOperatorGateway 接口

业务系统需实现此接口以对接自身的用户系统:

public interface FlowOperatorGateway {
    IFlowOperator get(OperatorKey key);
    List<IFlowOperator> findByKeys(List<OperatorKey> keys);
}

参与人统一由 OperatorKey 表达,批量查询允许同一人员对应多个身份。接口不提供忽略身份的重载; 默认身份便捷方法 get(long) / findByIds(List<Long>) 内部转换为 OperatorKey.ofDefault(...)。 详见 多身份参与 3.2。

缓存机制

  • 每次 FlowService 操作前调用 FlowOperatorLocalThreadCache.clear() 清理缓存
  • 同一请求内,通过 GatewayContext.getFlowOperator(OperatorKey) 获取操作者时自动缓存
  • 批量查询通过 GatewayContext.findByKeys(keys) 获取,同样走缓存;显式非默认身份不一致的结果不会缓存

使用实例

完整可运行的网关实现见 flow-engine-example/src/main/java/com/codingapi/example/gateway/impl/FlowOperatorGatewayImpl.java。

// 按参与人获取(自动缓存)
IFlowOperator operator = GatewayContext.getInstance().getFlowOperator(
        OperatorKey.of(userId, "department1"));

// 混合身份批量获取(自动缓存,按入参顺序返回)
List<IFlowOperator> operators = GatewayContext.getInstance().findByKeys(List.of(
        OperatorKey.ofDefault(userId),
        OperatorKey.of(userId, "department1")));