优化监听器
This commit is contained in:
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.flowable.jpa;
|
|||||||
|
|
||||||
import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager;
|
import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager;
|
||||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
|
||||||
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableListener;
|
||||||
import com.lanyuanxiaoyao.flowable.test.TestFlowableManager;
|
import com.lanyuanxiaoyao.flowable.test.TestFlowableManager;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
@@ -21,7 +22,12 @@ public class TestSpringFlowableManager extends TestFlowableManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<? extends FlowableHandler> getAutomaticNodeClass() {
|
protected Class<? extends FlowableHandler> getHandler() {
|
||||||
return SimpleAutoAction.class;
|
return SimpleAutoAction.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<? extends FlowableListener> getListenerClass() {
|
||||||
|
return SimpleListener.class;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ public abstract class FlowableManager {
|
|||||||
.metadata(new FlowableMetadata(metadata))
|
.metadata(new FlowableMetadata(metadata))
|
||||||
.build();
|
.build();
|
||||||
repository.saveInstance(instance);
|
repository.saveInstance(instance);
|
||||||
|
callListeners(node.getListeners(), listener -> listener.onFlowStart(instance, node));
|
||||||
if (FlowableNode.Type.AUTOMATIC.equals(node.getType())) {
|
if (FlowableNode.Type.AUTOMATIC.equals(node.getType())) {
|
||||||
automaticAction(instance, node);
|
automaticAction(instance, node);
|
||||||
}
|
}
|
||||||
@@ -134,6 +134,7 @@ public abstract class FlowableManager {
|
|||||||
|
|
||||||
private void action(FlowableInstance instance, FlowableNode node, FlowableAction action, String comment) {
|
private void action(FlowableInstance instance, FlowableNode node, FlowableAction action, String comment) {
|
||||||
if (FlowableInstance.Status.COMPLETED.equals(instance.getStatus()) || FlowableInstance.Status.ERROR.equals(instance.getStatus())) {
|
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() + "的流程已结束,无法操作");
|
throw new IllegalArgumentException("ID为" + instance.getInstanceId() + "的流程已结束,无法操作");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,6 +157,9 @@ public abstract class FlowableManager {
|
|||||||
throw new IllegalArgumentException("节点执行结果不能为空");
|
throw new IllegalArgumentException("节点执行结果不能为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FlowableAction tempAction = action;
|
||||||
|
callListeners(node.getListeners(), listener -> listener.onAction(instance, tempAction));
|
||||||
|
|
||||||
// 如果是挂起操作,就直接返回,不做操作
|
// 如果是挂起操作,就直接返回,不做操作
|
||||||
if (FlowableAction.SUSPEND.equals(action)) {
|
if (FlowableAction.SUSPEND.equals(action)) {
|
||||||
saveInstance(instance, instance.getStatus(), action, comment);
|
saveInstance(instance, instance.getStatus(), action, comment);
|
||||||
@@ -170,6 +174,7 @@ public abstract class FlowableManager {
|
|||||||
|| !node.getTargets().containsKey(action)
|
|| !node.getTargets().containsKey(action)
|
||||||
|| StringHelper.isBlank(node.getTargets().get(action))) {
|
|| StringHelper.isBlank(node.getTargets().get(action))) {
|
||||||
saveInstance(instance, FlowableInstance.Status.COMPLETED, action, comment);
|
saveInstance(instance, FlowableInstance.Status.COMPLETED, action, comment);
|
||||||
|
callListeners(node.getListeners(), listener -> listener.onFlowEnd(instance, node));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String nextNodeId = node.getTargets().get(action);
|
String nextNodeId = node.getTargets().get(action);
|
||||||
@@ -177,6 +182,7 @@ public abstract class FlowableManager {
|
|||||||
instance.setCurrentNodeId(nextNode.getNodeId());
|
instance.setCurrentNodeId(nextNode.getNodeId());
|
||||||
saveInstance(instance, FlowableInstance.Status.RUNNING, action, comment);
|
saveInstance(instance, FlowableInstance.Status.RUNNING, action, comment);
|
||||||
|
|
||||||
|
callListeners(node.getListeners(), listener -> listener.onActionComplete(instance, node));
|
||||||
if (FlowableNode.Type.AUTOMATIC.equals(nextNode.getType())) {
|
if (FlowableNode.Type.AUTOMATIC.equals(nextNode.getType())) {
|
||||||
automaticAction(instance, node);
|
automaticAction(instance, node);
|
||||||
}
|
}
|
||||||
@@ -190,8 +196,9 @@ public abstract class FlowableManager {
|
|||||||
repository.saveInstanceAndHistory(instance, history);
|
repository.saveInstanceAndHistory(instance, history);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void callListeners(List<FlowableListener> listeners, Consumer<FlowableListener> handler) {
|
private void callListeners(List<String> listeners, Consumer<FlowableListener> handler) {
|
||||||
for (FlowableListener listener : listeners) {
|
for (String listenerClass : listeners) {
|
||||||
|
FlowableListener listener = createBean(listenerClass);
|
||||||
handler.accept(listener);
|
handler.accept(listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ package com.lanyuanxiaoyao.flowable.core.model;
|
|||||||
* @version 20241231
|
* @version 20241231
|
||||||
*/
|
*/
|
||||||
public interface FlowableListener {
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,4 +199,25 @@ public abstract class TestFlowableManager {
|
|||||||
manager.approve(instanceId, MapHelper.of("role", "admin"));
|
manager.approve(instanceId, MapHelper.of("role", "admin"));
|
||||||
Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());
|
Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract Class<? extends FlowableListener> 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.flowable.test;
|
|||||||
|
|
||||||
import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager;
|
import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager;
|
||||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableHandler;
|
||||||
|
import com.lanyuanxiaoyao.flowable.core.model.FlowableListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lanyuanxiaoyao
|
* @author lanyuanxiaoyao
|
||||||
@@ -14,7 +15,12 @@ public class TestSimpleFlowableManager extends TestFlowableManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<? extends FlowableHandler> getAutomaticNodeClass() {
|
protected Class<? extends FlowableHandler> getHandler() {
|
||||||
return SimpleAutoHandler.class;
|
return SimpleAutoHandler.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<? extends FlowableListener> getListenerClass() {
|
||||||
|
return SimpleListener.class;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user