Flowable实用技巧-自动完成首个UserTask

对于复杂的流程可能会有BUG,欢迎指出~

 

org.flowable.engine.common.api.delegate.event.FlowableEventListener是Flowable的全局监听

public interface FlowableEventListener {

       void onEvent(FlowableEvent event);
}

其下的实现类有:

Flowable实用技巧-自动完成首个UserTask_第1张图片

核心接口void onEvent(FlowableEvent event);

经过onEvent的事件有多个,目前要达到自动完成首个任务的主要思路是event到任务实体,也就是TaskEntity,再通过TaskService.complete完成TaskEntity任务即可,TaskEntity的基类就是

FlowableEntityEventImpl

 

public class FlowableEntityEventImpl extends FlowableEventImpl implements FlowableEngineEntityEvent {

    protected Object entity;

    public FlowableEntityEventImpl(Object entity, FlowableEngineEventType type) {
        super(type);
        if (entity == null) {
            throw new FlowableIllegalArgumentException("Entity cannot be null.");
        }
        this.entity = entity;
    }

    @Override
    public Object getEntity() {
        return entity;
    }
}

完整代码如下

/**
 * 全局自动完成首个任务监听器
 * Created by Vim 2018/11/21 9:46
 *
 * @author Vim
 */
public class AutoCompleteFirstTaskListener implements FlowableEventListener {
    @Override
    public void onEvent(FlowableEvent event) {
        if (!(event instanceof FlowableEntityEventImpl)) {
            return;
        }

        FlowableEntityEventImpl entityEvent = (FlowableEntityEventImpl) event;

        Object entity = entityEvent.getEntity();

        //是否是任务实体类
        if (!(entity instanceof TaskEntity)) {
            return;
        }

        TaskEntity taskEntity = (TaskEntity) entity;

        //是否是在任务节点创建时
        if (FlowableEngineEventType.TASK_CREATED.equals(event.getType())) {
            //找到流程第一个userTask节点
            FlowElement firstElement = this.findFirstFlowElement(taskEntity);

            //对比节点是否相同,因为有可能有子流程的节点进来
            if (firstElement != null && taskEntity.getTaskDefinitionKey().equals(firstElement.getId())) {
                Context.getProcessEngineConfiguration().getTaskService().complete(taskEntity.getId());
            }
        }

    }

    /**
     * 查找流程第一个userTask
     */
    private FlowElement findFirstFlowElement(TaskEntity taskEntity) {
        RepositoryService repositoryService = SpringContextListener.getBean(RepositoryService.class);
        BpmnModel bpmnModel = repositoryService.getBpmnModel(taskEntity.getProcessDefinitionId());
        for (FlowElement flowElement : bpmnModel.getProcesses().get(0).getFlowElements()) {
            if (flowElement instanceof StartEvent) {
                return bpmnModel.getFlowElement(((StartEvent) flowElement).getOutgoingFlows().get(0).getTargetRef());
            }
        }
        return null;
    }


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

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

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

监听类写好后在配置文件里配置好,我这里是springboot,配置如下:

Flowable实用技巧-自动完成首个UserTask_第2张图片 

 

你可能感兴趣的:(Java,activiti,flowable)