Solon v3.1.2

statefulflow - Hello World [预览]

</> markdown

1、新建项目

新建项目之后,添加依赖

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-flow</artifactId>
    <version>3.1.3-SNAPSHOT</version>
</dependency>

2、添加配置

添加应用配置:

solon.flow:
  - "classpath:flow/*.yml"

添加流处理配置(支持 json 或 yml 格式),例: flow/demo1.chain.yml

id: "c1"
layout: 
  - { id: "n1", type: "start", link: "n2"}
  - { id: "n2", type: "activity", link: "n3", task: "System.out.println(\"hello world!\");"}
  - { id: "n3", type: "end"}

示意图:

3、代码应用

注解模式

@Configuration
public class DemoCom {
    //替换掉默认引擎(会自动加载 solon.flow 配置的链资源)//之后可注入 StatefulFlowEngine 或 FlowEngine 
    @Bean
    public StatefulFlowEngine statefulFlowEngine() {
        return StatefulFlowEngine.newInstance(StatefulSimpleFlowDriver.builder()
                .stateController(new BlockStateController())
                .stateRepository(new InMemoryStateRepository()) //状态仓库(支持持久化)
                .build());
    }
    
    @Bean
    public void test(StatefulFlowEngine flowEngine) throws Throwable {
        //兼容无状态执行
        flowEngine.eval("c1", new FlowContext()); //不使用实例id
    
        //单步前进(上下文需要配置,实例id)
        flowEngine.stepForward("c1", new FlowContext("instance-1")); //使用实例id
    }
}

原生 Java 模式

StatefulFlowEngine engine = StatefulFlowEngine.newInstance(StatefulSimpleFlowDriver.builder()
                .stateController(new BlockStateController())
                .stateRepository(new InMemoryStateRepository()) //状态仓库(支持持久化)
                .build());

//加载链配置
engine.load("classpath:flow/*.yml");

//兼容无状态执行
flowEngine.eval("c1", new FlowContext()); //不使用实例id

//单步前进(上下文需要配置,实例id)
flowEngine.stepForward("c1", new FlowContext("instance-1")); //使用实例id