flowable工作流 - 全局监听设置CATEGORY_

基于开源项目KonBAI / RuoYi-Flowable-Plus使用的部分功能调整。利用全局监听器,设置act_ru_task表中的CATEGORY_(流程分类key)的值。

流程实例创建流程

(1)创建流程分类:category
(2)创建自定义表单:form
(3)设计模型流程图、部署:model、procdef、deployment

一个流程分类下可以有多个模型,是一对多的关系。

代码解析

在部署模型时,将部署id回写到模型表中

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deployModel(String modelId) {
        // 获取流程模型
        Model model = repositoryService.getModel(modelId);
        if (ObjectUtil.isNull(model)) {
            throw new RuntimeException("流程模型不存在!");
        }
        // 获取流程图
        byte[] bpmnBytes = repositoryService.getModelEditorSource(modelId);
        if (bpmnBytes == null) {
            throw new ServiceException("流程设计未定义!");
        }
        String bpmnXml = StringUtils.toEncodedString(bpmnBytes, StandardCharsets.UTF_8);
        BpmnModel bpmnModel = ModelUtils.getBpmnModel(bpmnXml);
        String processName = model.getName() + ProcessConstants.SUFFIX;
        // 部署流程
        Deployment deployment = repositoryService.createDeployment()
            .name(model.getName())
            .key(model.getKey())
            .category(model.getCategory())
            .addBytes(processName, bpmnBytes)
            .deploy();
        ProcessDefinition procDef = repositoryService.createProcessDefinitionQuery()
            .deploymentId(deployment.getId())
            .singleResult();

        // 修改流程定义的分类,便于搜索流程
        repositoryService.setProcessDefinitionCategory(procDef.getId(), model.getCategory());

		// new >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        // 保存部署id到模型表中
        model.setDeploymentId(deployment.getId());
        // --------------------------------------------------
        repositoryService.saveModel(model);
		
        // 保存部署表单
        return deployFormService.saveInternalDeployForm(deployment.getId(), bpmnModel);
    }
 }

添加全局监听,给每个任务写入category_

@Slf4j
@Component
public class GlobalEventListener implements FlowableEventListener {

    /**
     * 事件触发时,设置category_
     * @param flowableEvent
     */
    @Override
    public void onEvent(FlowableEvent flowableEvent) {
        FlowableEventType eventType = flowableEvent.getType();
        log.info(">>执行 任务全局监听 --》 GlobalEventListener eventType={}", eventType.name());

		// 在任务创建时,将category填入任务中
        if (TASK_CREATED == eventType) {
            FlowableEntityEvent entityEvent = (FlowableEntityEvent) flowableEvent;
            TaskEntity entity = (TaskEntity) entityEvent.getEntity();
            if (StrUtil.isBlank(entity.getCategory())) {
                //前提是Deployment已经设置过Category
                ProcessDefinitionEntity processDefinitionEntity = CommandContextUtil.getProcessDefinitionEntityManager().findById(entity.getProcessDefinitionId());
                DeploymentEntity deploymentEntity = CommandContextUtil.getDeploymentEntityManager().findById(processDefinitionEntity.getDeploymentId());
                //直接修改act_ru_task实体的属性
                //因为事件是同步事件,所以会包裹在整体事务中提交
                entity.setCategory(deploymentEntity.getCategory());
            }
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}

参考资料

KonBAI / RuoYi-Flowable-Plus 基于 RuoYi-Vue-Plus 进行二次开发扩展Flowable工作流功能项目
Flowable6.4 – 设置流程分类 by 字痕随行

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Flowable设置全局监听器,需要实现一个实现 `org.flowable.common.engine.api.delegate.event.FlowableEventListener` 接口的监听器类,并将其添加到流程引添构建器中。以下是设置全局监听器的步骤: 1. 实现 FlowableEventListener 接口: ```java public class CustomEventListener implements FlowableEventListener { @Override public void onEvent(FlowableEvent event) { // 处理事件逻辑 } @Override public boolean isFailOnException() { return false; } } ``` 2. 添加监听器到流程引擎配置: ```java ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration() .setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1") .setJdbcUsername("sa") .setJdbcPassword("") .setJdbcDriver("org.h2.Driver") .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE) .setAsyncExecutorActivate(false) .setEventListeners(Collections.singletonList(new CustomEventListener())); ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); ``` 在这个例子中,我们创建了一个 `ProcessEngineConfiguration` 对象,并将 `CustomEventListener` 添加到 `eventListeners` 列表中。这样就可以在整个流程中监听所有事件。 注意:如果您使用 Spring 集成 Flowable,则可以使用 `@Component` 注解将 `CustomEventListener` 类标记为组件,并在 Spring 配置文件中将其添加到流程引擎配置中。 希望这个回答能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值