statefulflow - Hello World
2025年12月5日 上午11:02:32
1、新建项目
新建项目之后,添加依赖
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-flow</artifactId>
</dependency>
2、添加配置
添加应用配置:
solon.flow:
- "classpath:flow/*.yml"
添加流处理配置(支持 json 或 yml 格式),例: flow/demo1.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 {
//构建状态控制器
@Bean
public StateController stateController() {
return new NotBlockStateController(); //BlockStateController, ActorStateController
}
//构建状态仓库(持久化状态)
@Bean
public StateRepository stateRepository() {
return new InMemoryStateRepository();
}
@Bean
public void test(FlowEngine engine, StateController stateController, StateRepository stateRepository) throws Throwable {
//无状态执行
engine.eval("c1", FlowContext.of());
//有状态服务(上下文需要配置,实例id)
StateResult result = engine.forStateful()
.eval("c1", FlowContext.of("i-1", stateController, stateRepository));
}
}
原生 Java 模式
//使用有状态的流程驱动器
FlowEngine engine = FlowEngine.newInstance();
StateController stateController = new NotBlockStateController();
StateRepository stateRepository = new InMemoryStateRepository();
//加载配置
engine.load("classpath:flow/*.yml");
//无状态执行
engine.eval("c1", FlowContext.of()); //不使用实例id
//有状态服务(上下文需要配置,实例id)
StateResult result = engine.forStateful()
.eval("c1", FlowContext.of("i-1", stateController, stateRepository));