增加挂起操作
This commit is contained in:
@@ -129,6 +129,11 @@ public abstract class FlowableManager {
|
|||||||
throw new IllegalArgumentException("节点执行结果不能为空");
|
throw new IllegalArgumentException("节点执行结果不能为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果是挂起操作,就直接返回,不做操作
|
||||||
|
if (FlowableAction.SUSPEND.equals(action)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (FlowableAction.TERMINAL.equals(action)) {
|
if (FlowableAction.TERMINAL.equals(action)) {
|
||||||
saveInstance(instance, FlowableInstance.Status.ERROR, metadata, action, comment);
|
saveInstance(instance, FlowableInstance.Status.ERROR, metadata, action, comment);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -10,4 +10,5 @@ public enum FlowableAction {
|
|||||||
APPROVE,
|
APPROVE,
|
||||||
REJECT,
|
REJECT,
|
||||||
TERMINAL,
|
TERMINAL,
|
||||||
|
SUSPEND,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public class FlowableInstance {
|
|||||||
|
|
||||||
private String currentNodeId;
|
private String currentNodeId;
|
||||||
private Status status;
|
private Status status;
|
||||||
private LocalDateTime updatedTime = LocalDateTime.now();
|
private LocalDateTime updatedTime;
|
||||||
|
|
||||||
public FlowableInstance(String instanceId, String currentNodeId) {
|
public FlowableInstance(String instanceId, String currentNodeId) {
|
||||||
this(instanceId, currentNodeId, MapHelper.empty(), Status.RUNNING, LocalDateTime.now(), LocalDateTime.now());
|
this(instanceId, currentNodeId, MapHelper.empty(), Status.RUNNING, LocalDateTime.now(), LocalDateTime.now());
|
||||||
|
|||||||
@@ -28,11 +28,15 @@ public class FlowableNode {
|
|||||||
private LocalDateTime updatedTime = LocalDateTime.now();
|
private LocalDateTime updatedTime = LocalDateTime.now();
|
||||||
|
|
||||||
public FlowableNode(String nodeId, String name, String description, Map<FlowableAction, String> targets) {
|
public FlowableNode(String nodeId, String name, String description, Map<FlowableAction, String> targets) {
|
||||||
this(nodeId, name, description, Type.MANUAL, FlowableHandler.DefaultFlowableHandler.class.getName(), targets, ListHelper.empty(), LocalDateTime.now());
|
this(nodeId, name, description, Type.MANUAL, FlowableHandler.DefaultFlowableHandler.class, targets);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlowableNode(String nodeId, String name, String description, Class<? extends FlowableHandler> actionClass, Map<FlowableAction, String> targets) {
|
public FlowableNode(String nodeId, String name, String description, Class<? extends FlowableHandler> actionClass, Map<FlowableAction, String> targets) {
|
||||||
this(nodeId, name, description, Type.AUTOMATIC, actionClass.getName(), targets, ListHelper.empty(), LocalDateTime.now());
|
this(nodeId, name, description, Type.AUTOMATIC, actionClass, targets);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FlowableNode(String nodeId, String name, String description, Type type, Class<? extends FlowableHandler> actionClass, Map<FlowableAction, String> targets) {
|
||||||
|
this(nodeId, name, description, type, actionClass.getName(), targets, ListHelper.empty(), LocalDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlowableNode(String nodeId, String name, String description, Type type, String handler, Map<FlowableAction, String> targets, List<String> listeners, LocalDateTime createdTime) {
|
public FlowableNode(String nodeId, String name, String description, Type type, String handler, Map<FlowableAction, String> targets, List<String> listeners, LocalDateTime createdTime) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.lanyuanxiaoyao.flowable.core.model.FlowableAction;
|
|||||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
|
||||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance;
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance;
|
||||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableNode;
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableNode;
|
||||||
|
import com.lanyuanxiaoyao.flowable.test.handler.TwoApproveHandler;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -121,6 +122,27 @@ public abstract class TestFlowableManager {
|
|||||||
Assertions.assertThrows(IllegalArgumentException.class, () -> manager.approve(instanceId));
|
Assertions.assertThrows(IllegalArgumentException.class, () -> manager.approve(instanceId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSuspend() {
|
||||||
|
FlowableManager manager = flowableManager();
|
||||||
|
FlowableNode node = new FlowableNode(
|
||||||
|
"edf1b7b0-f45b-4256-b696-ce84857f03f7",
|
||||||
|
"edf1b7b0-f45b-4256-b696-ce84857f03f7",
|
||||||
|
"edf1b7b0-f45b-4256-b696-ce84857f03f7",
|
||||||
|
FlowableNode.Type.MANUAL,
|
||||||
|
TwoApproveHandler.class,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
manager.create(node);
|
||||||
|
|
||||||
|
String instanceId = manager.start(node.getNodeId());
|
||||||
|
Assertions.assertEquals(FlowableInstance.Status.RUNNING, manager.getInstance(instanceId).getStatus());
|
||||||
|
manager.approve(instanceId);
|
||||||
|
Assertions.assertEquals(FlowableInstance.Status.RUNNING, manager.getInstance(instanceId).getStatus());
|
||||||
|
manager.approve(instanceId);
|
||||||
|
Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract Class<? extends FlowableHandler> getAutomaticNodeClass();
|
protected abstract Class<? extends FlowableHandler> getAutomaticNodeClass();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.lanyuanxiaoyao.flowable.test.handler;
|
||||||
|
|
||||||
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableAction;
|
||||||
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
|
||||||
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance;
|
||||||
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableNode;
|
||||||
|
import java.util.Map;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同意两次才能通过的自动节点
|
||||||
|
*
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250103
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class TwoApproveHandler implements FlowableHandler {
|
||||||
|
private static final String KEY = "approve-times";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FlowableAction handle(FlowableAction action, FlowableInstance instance, FlowableNode node, Map<String, Object> metadata) {
|
||||||
|
log.info("{}", metadata);
|
||||||
|
int approveCount = (int) metadata.getOrDefault(KEY, 0);
|
||||||
|
if (approveCount + 1 > 1) {
|
||||||
|
return FlowableAction.APPROVE;
|
||||||
|
}
|
||||||
|
metadata.put(KEY, approveCount + 1);
|
||||||
|
return FlowableAction.SUSPEND;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user