1
0

优化监听器

This commit is contained in:
2025-01-07 09:56:56 +08:00
parent 465cc8cd3f
commit dddc9d7171
7 changed files with 135 additions and 10 deletions

View File

@@ -76,7 +76,7 @@ public abstract class FlowableManager {
.metadata(new FlowableMetadata(metadata))
.build();
repository.saveInstance(instance);
callListeners(node.getListeners(), listener -> listener.onFlowStart(instance, node));
if (FlowableNode.Type.AUTOMATIC.equals(node.getType())) {
automaticAction(instance, node);
}
@@ -134,6 +134,7 @@ public abstract class FlowableManager {
private void action(FlowableInstance instance, FlowableNode node, FlowableAction action, String comment) {
if (FlowableInstance.Status.COMPLETED.equals(instance.getStatus()) || FlowableInstance.Status.ERROR.equals(instance.getStatus())) {
callListeners(node.getListeners(), listener -> listener.onActionStart(instance, node));
throw new IllegalArgumentException("ID为" + instance.getInstanceId() + "的流程已结束,无法操作");
}
@@ -156,6 +157,9 @@ public abstract class FlowableManager {
throw new IllegalArgumentException("节点执行结果不能为空");
}
FlowableAction tempAction = action;
callListeners(node.getListeners(), listener -> listener.onAction(instance, tempAction));
// 如果是挂起操作,就直接返回,不做操作
if (FlowableAction.SUSPEND.equals(action)) {
saveInstance(instance, instance.getStatus(), action, comment);
@@ -170,6 +174,7 @@ public abstract class FlowableManager {
|| !node.getTargets().containsKey(action)
|| StringHelper.isBlank(node.getTargets().get(action))) {
saveInstance(instance, FlowableInstance.Status.COMPLETED, action, comment);
callListeners(node.getListeners(), listener -> listener.onFlowEnd(instance, node));
return;
}
String nextNodeId = node.getTargets().get(action);
@@ -177,6 +182,7 @@ public abstract class FlowableManager {
instance.setCurrentNodeId(nextNode.getNodeId());
saveInstance(instance, FlowableInstance.Status.RUNNING, action, comment);
callListeners(node.getListeners(), listener -> listener.onActionComplete(instance, node));
if (FlowableNode.Type.AUTOMATIC.equals(nextNode.getType())) {
automaticAction(instance, node);
}
@@ -190,8 +196,9 @@ public abstract class FlowableManager {
repository.saveInstanceAndHistory(instance, history);
}
private void callListeners(List<FlowableListener> listeners, Consumer<FlowableListener> handler) {
for (FlowableListener listener : listeners) {
private void callListeners(List<String> listeners, Consumer<FlowableListener> handler) {
for (String listenerClass : listeners) {
FlowableListener listener = createBean(listenerClass);
handler.accept(listener);
}
}

View File

@@ -7,13 +7,13 @@ package com.lanyuanxiaoyao.flowable.core.model;
* @version 20241231
*/
public interface FlowableListener {
void onStart(FlowableInstance instance);
void onFlowStart(FlowableInstance instance, FlowableNode node);
void onError(FlowableInstance instance, Throwable throwable);
void onFlowEnd(FlowableInstance instance, FlowableNode node);
void onComplete(FlowableInstance instance);
void onActionStart(FlowableInstance instance, FlowableNode node);
void onApprove(FlowableInstance instance);
void onAction(FlowableInstance instance, FlowableAction action);
void onReject(FlowableInstance instance);
void onActionComplete(FlowableInstance instance, FlowableNode node);
}