数据上下文

LiteFlow中,在一个流程启动后框架会对流程实例分配数据上下文对象,不同的流程数据上下文是彼此隔离的。流程中的不同组件之间不会直接传递参数,而是所有组件都通过读写上下文数据实现交互,这很容易理解,因为不这样做组件就没法统一编排了。这篇笔记我们继续介绍LiteFlow中上下文对象的使用。

读写数据上下文

LiteFlow中上下文数据类可以自定义,不过框架也默认提供了一个DefaultContext类,它的内部其实只是封装了一个ConcurrentHashMap用于读写数据。下面两个业务流程节点中,step1向数据上下文写入数据,step2向数据上下文读取数据。

package com.gacfox.demo.service;

import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@LiteflowComponent("step1")
public class Step1 extends NodeComponent {
    @Override
    public void process() throws Exception {
        DefaultContext context = this.getContextBean(DefaultContext.class);
        context.setData("message", "Hello, world!");
    }
}
package com.gacfox.demo.service;

import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.slot.DefaultContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@LiteflowComponent("step2")
public class Step2 extends NodeComponent {
    @Override
    public void process() throws Exception {
        DefaultContext context = this.getContextBean(DefaultContext.class);
        log.info("message: {}", (String) context.getData("message"));
    }
}

使用执行器启动流程时,我们需要传入数据上下文类的类型,它会自动实例化并传入流程中。

package com.gacfox.demo.controller;

import com.gacfox.demo.common.ApiResult;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/demo")
public class DemoController {
    @Resource
    private FlowExecutor flowExecutor;

    @GetMapping("/startFlow")
    public ApiResult<?> startFlow() {
        LiteflowResponse response = flowExecutor.execute2Resp("demoChain", null, DefaultContext.class);
        return ApiResult.success("流程执行结果为: " + response.isSuccess());
    }
}

不过这个DefaultContext由于是弱类型的Map,一般实际开发中都推荐自定义数据上下文类型,这样代码更清晰。

使用流程入参

除了数据上下文,其实流程启动时还可以向其中传一些初始化参数,这些被称为流程入参,它在业务流程节点类中使用getRequestData()方法获取。

package com.gacfox.demo.service;

import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@LiteflowComponent("step1")
public class Step1 extends NodeComponent {
    @Override
    public void process() throws Exception {
        String message = this.getRequestData();
        log.info("message: {}", message);
    }
}

启动流程时,流程入参通过第2个参数传入。

package com.gacfox.demo.controller;

import com.gacfox.demo.common.ApiResult;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.slot.DefaultContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/demo")
public class DemoController {
    @Resource
    private FlowExecutor flowExecutor;

    @GetMapping("/startFlow")
    public ApiResult<?> startFlow() {
        LiteflowResponse response = flowExecutor.execute2Resp("demoChain", "Hello, world!", DefaultContext.class);
        return ApiResult.success("流程执行结果为: " + response.isSuccess());
    }
}
作者:Gacfox
版权声明:本网站为非盈利性质,文章如非特殊说明均为原创,版权遵循知识共享协议CC BY-NC-ND 4.0进行授权,转载必须署名,禁止用于商业目的或演绎修改后转载。