From dddc9d7171154d41cc474d6aba604bebeb2baec7 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Tue, 7 Jan 2025 09:56:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flowable/jpa/SimpleListener.java | 46 +++++++++++++++++++ .../jpa/TestSpringFlowableManager.java | 8 +++- .../core/manager/FlowableManager.java | 13 ++++-- .../flowable/core/model/FlowableListener.java | 10 ++-- .../flowable/test/TestFlowableManager.java | 21 +++++++++ .../flowable/test/SimpleListener.java | 39 ++++++++++++++++ .../test/TestSimpleFlowableManager.java | 8 +++- 7 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/SimpleListener.java create mode 100644 flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleListener.java diff --git a/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/SimpleListener.java b/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/SimpleListener.java new file mode 100644 index 0000000..6a7b5f1 --- /dev/null +++ b/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/SimpleListener.java @@ -0,0 +1,46 @@ +package com.lanyuanxiaoyao.flowable.jpa; + +import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager; +import com.lanyuanxiaoyao.flowable.core.model.FlowableAction; +import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance; +import com.lanyuanxiaoyao.flowable.core.model.FlowableListener; +import com.lanyuanxiaoyao.flowable.core.model.FlowableNode; +import javax.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * @author lanyuanxiaoyao + * @version 20250106 + */ +@Slf4j +@Service +public class SimpleListener implements FlowableListener { + @Resource + private FlowableManager flowableManager; + + @Override + public void onFlowStart(FlowableInstance instance, FlowableNode node) { + log.info("onFlowStart with spring: {}", flowableManager.listNodes()); + } + + @Override + public void onFlowEnd(FlowableInstance instance, FlowableNode node) { + log.info("onFlowEnd with spring: {}", flowableManager.listNodes()); + } + + @Override + public void onActionStart(FlowableInstance instance, FlowableNode node) { + log.info("onActionStart with spring: {}", flowableManager.listNodes()); + } + + @Override + public void onAction(FlowableInstance instance, FlowableAction action) { + log.info("onAction with spring: {}", flowableManager.listNodes()); + } + + @Override + public void onActionComplete(FlowableInstance instance, FlowableNode node) { + log.info("onActionComplete with spring: {}", flowableManager.listNodes()); + } +} diff --git a/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/TestSpringFlowableManager.java b/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/TestSpringFlowableManager.java index eae35e1..47fa4bd 100644 --- a/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/TestSpringFlowableManager.java +++ b/adapter/flowable-spring-boot-jpa-starter/src/test/java/com/lanyuanxiaoyao/flowable/jpa/TestSpringFlowableManager.java @@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.flowable.jpa; import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager; import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; +import com.lanyuanxiaoyao.flowable.core.model.FlowableListener; import com.lanyuanxiaoyao.flowable.test.TestFlowableManager; import javax.annotation.Resource; import org.springframework.boot.test.context.SpringBootTest; @@ -21,7 +22,12 @@ public class TestSpringFlowableManager extends TestFlowableManager { } @Override - protected Class getAutomaticNodeClass() { + protected Class getHandler() { return SimpleAutoAction.class; } + + @Override + protected Class getListenerClass() { + return SimpleListener.class; + } } diff --git a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/manager/FlowableManager.java b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/manager/FlowableManager.java index 74c4f10..00501a9 100644 --- a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/manager/FlowableManager.java +++ b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/manager/FlowableManager.java @@ -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 listeners, Consumer handler) { - for (FlowableListener listener : listeners) { + private void callListeners(List listeners, Consumer handler) { + for (String listenerClass : listeners) { + FlowableListener listener = createBean(listenerClass); handler.accept(listener); } } diff --git a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableListener.java b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableListener.java index 53a01b5..261fae0 100644 --- a/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableListener.java +++ b/flowable-core/src/main/java/com/lanyuanxiaoyao/flowable/core/model/FlowableListener.java @@ -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); } diff --git a/flowable-example/src/main/java/com/lanyuanxiaoyao/flowable/test/TestFlowableManager.java b/flowable-example/src/main/java/com/lanyuanxiaoyao/flowable/test/TestFlowableManager.java index c35646d..0fdc689 100644 --- a/flowable-example/src/main/java/com/lanyuanxiaoyao/flowable/test/TestFlowableManager.java +++ b/flowable-example/src/main/java/com/lanyuanxiaoyao/flowable/test/TestFlowableManager.java @@ -199,4 +199,25 @@ public abstract class TestFlowableManager { manager.approve(instanceId, MapHelper.of("role", "admin")); Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus()); } + + protected abstract Class getListenerClass(); + + @Test + @Order(9) + public void testListener() { + FlowableManager manager = flowableManager(); + FlowableNode node = FlowableNode.builder() + .nodeId("1c81366f-4102-4a9e-abb2-1dbcb09062e9") + .name("1c81366f-4102-4a9e-abb2-1dbcb09062e9") + .description("1c81366f-4102-4a9e-abb2-1dbcb09062e9") + .type(FlowableNode.Type.MANUAL) + .listeners(ListHelper.of( + getListenerClass().getName() + )) + .build(); + manager.create(node); + String instanceId = manager.start(node.getNodeId()); + manager.approve(instanceId); + Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus()); + } } diff --git a/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleListener.java b/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleListener.java new file mode 100644 index 0000000..32c5fbb --- /dev/null +++ b/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/SimpleListener.java @@ -0,0 +1,39 @@ +package com.lanyuanxiaoyao.flowable.test; + +import com.lanyuanxiaoyao.flowable.core.model.FlowableAction; +import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance; +import com.lanyuanxiaoyao.flowable.core.model.FlowableListener; +import com.lanyuanxiaoyao.flowable.core.model.FlowableNode; +import lombok.extern.slf4j.Slf4j; + +/** + * @author lanyuanxiaoyao + * @version 20250106 + */ +@Slf4j +public class SimpleListener implements FlowableListener { + @Override + public void onFlowStart(FlowableInstance instance, FlowableNode node) { + log.info("onFlowStart"); + } + + @Override + public void onFlowEnd(FlowableInstance instance, FlowableNode node) { + log.info("onFlowEnd"); + } + + @Override + public void onActionStart(FlowableInstance instance, FlowableNode node) { + log.info("onActionStart"); + } + + @Override + public void onAction(FlowableInstance instance, FlowableAction action) { + log.info("onAction"); + } + + @Override + public void onActionComplete(FlowableInstance instance, FlowableNode node) { + log.info("onActionComplete"); + } +} diff --git a/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/TestSimpleFlowableManager.java b/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/TestSimpleFlowableManager.java index aab23f8..3ae1fdf 100644 --- a/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/TestSimpleFlowableManager.java +++ b/flowable-example/src/test/java/com/lanyuanxiaoyao/flowable/test/TestSimpleFlowableManager.java @@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.flowable.test; import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager; import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler; +import com.lanyuanxiaoyao.flowable.core.model.FlowableListener; /** * @author lanyuanxiaoyao @@ -14,7 +15,12 @@ public class TestSimpleFlowableManager extends TestFlowableManager { } @Override - protected Class getAutomaticNodeClass() { + protected Class getHandler() { return SimpleAutoHandler.class; } + + @Override + protected Class getListenerClass() { + return SimpleListener.class; + } }