Solon v3.1.2

flow - 流上下文的作用(FlowContext)

</> markdown

作用1:中断控制(阻断当前分支,或停止)

带状态的业务场景中,根据状态中断流的驱动。比如,业务审批。

对应字段或方法描述
context.isInterrupted()是否已阻断
context.interrupt()阻断(当前分支不再后流)
context.isStopped()是否已停止
context.stop()停止(整个流不再后流)
context.manualNext(node)手动下一步(一般用不到)

应用示例(一般,用于驱动或任务定制):

@Component
public class ApproveFlowDriver extends SimpleFlowDriver implements FlowDriver{
    @Inject
    FlowService flowService;
    
    @Override
    public void handleTask(FlowContext context, Task task) throws Throwable {
        String instance_id = context.get("instance_id");
        String task_id = task.node().id();

        //获取任务状态
        TaskState taskState = flowService.getState(instance_id, task_id);

        if (taskState == null) {
            //没有状态,说明未完成;;;等待人工审批完成
            context.interrupt();
            //发送电子邮件催办
            flowService.sendEmail();
        }
    }
}

作用2:数据输入输出

对应字段或方法描述
context.model()提供多数据输入输出支持(在脚本中,可直接使用字段)
context.result提供单数据输出支持

应用示例:

#c1.chain.yml
id: c1
layout:
  - task: 'if(a > 0){context.result=1;}'
FlowContext context = new FlowContext();
context.put("a",2);

flowEngine.eval(c1,context);
System.out.println(context.result); //打印执行结果

作用3:广播事件(基于大米事件总线 - DamiBus

(v3.1.3 后支持)基于流上下文(FlowContext)的事件总线。可以在流执行中广播事件

对应字段或方法描述
context.eventBus()事件总线
context.<String,String>eventBus()支持泛型(类型按需指定,不指定时为 object)

流配置中的应用示例:

id: event1
layout:
  - task: '@DemoCom'
  - task: 'context.<String,String>eventBus().send("demo.topic", "hello");'

代码执行中的应用示例:

public class DemoController {
    ...
    public void test(){
        FlowContext context = new FlowContext();
        context.<String,String>eventBus().listen("demo.topic", event -> {
            System.out.println(event.getContent());
        });

        flowEngine.eval("event1", context);
    }
}

 @Component
public static class DemoCom implements TaskComponent {

    @Override
    public void run(FlowContext context, Node node) throws Throwable {
        context.<String, String>eventBus().send("demo.topic", "hello-com");
    }
}

作用4:脚本运行

对应字段或方法描述
context.run(script)动态运行脚本

应用示例(可在脚本里,再动态运行脚本):

#c1.chain.yml
id: c1
layout:
  - meta.script: "1+1"
    task: 'context.run(node.meta("script"))' #执行 meta.script 申明的脚本