diff --git a/pom.xml b/pom.xml
index cb8485f636f0e73a7173fdebde18f960b272cdac..d524125a82507e7fae8d476c7b6001a8fae7b65a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,37 +5,17 @@
org.smartboot
flow-engine
- 1.0.0
+ 1.0.1
4.0.0
pom
smart-flow-core
smart-flow-spring-extension
+ smart-flow-manager
+ smart-flow-example
-
-
- com.google.guava
- guava
- 31.1-jre
-
-
-
-
- org.slf4j
- slf4j-api
- 2.0.3
-
-
-
- org.slf4j
- slf4j-log4j12
- 2.0.3
-
-
-
-
flow-engine
diff --git a/smart-flow-core/pom.xml b/smart-flow-core/pom.xml
index 85b85114352fe9fecb1d5236a1eac1a9afe6fc9c..5fefaf48bf2c81e57e34c278dc0398ed7040f5ed 100644
--- a/smart-flow-core/pom.xml
+++ b/smart-flow-core/pom.xml
@@ -5,7 +5,7 @@
org.smartboot
flow-engine
- 1.0.0
+ 1.0.1
4.0.0
@@ -26,26 +26,25 @@
- ch.qos.logback
- logback-core
- 1.4.4
+ org.junit.jupiter
+ junit-jupiter-engine
+ 5.9.1
+ test
+
org.slf4j
- jcl-over-slf4j
+ slf4j-api
2.0.3
+
- ch.qos.logback
- logback-classic
- 1.2.3
-
-
- org.slf4j
- slf4j-api
-
-
+ org.slf4j
+ slf4j-log4j12
+ 2.0.3
+ test
+
\ No newline at end of file
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/DefaultIdentifierManager.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/DefaultIdentifierManager.java
index 0dbe68234661072952153c3810106c791bf86126..92a75c491050e022e1e51449746f7dcd08225a39 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/DefaultIdentifierManager.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/DefaultIdentifierManager.java
@@ -1,9 +1,10 @@
package org.smartboot.flow.core;
-import org.smartboot.flow.core.parser.ElementUtils;
+import org.smartboot.flow.core.util.ContactUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
/**
* @author qinluo
@@ -16,14 +17,15 @@ public class DefaultIdentifierManager implements IdentifierManager {
* Generated identifiers.
*/
private final Map identifiers = new ConcurrentHashMap<>();
+ private final AtomicInteger sequence = new AtomicInteger(0);
@Override
public String generateIdentifier(String prefix) {
- String identifier = ElementUtils.random(prefix);
+ String identifier = ContactUtils.contact(prefix, sequence.getAndAdd(1));
// Ensure identifier is unique.
while (identifiers.containsKey(identifier) || identifiers.put(identifier, 1) != null) {
- identifier = ElementUtils.random(prefix);
+ identifier = ContactUtils.contact(prefix, sequence.getAndAdd(1));
}
return identifier;
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/EngineContext.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/EngineContext.java
index d3cc257fe199892217f8ad8f273abf0948a2281e..6aa5b360b5652d55c2244faed828acc09b68df85 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/EngineContext.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/EngineContext.java
@@ -20,6 +20,10 @@ import java.util.concurrent.Future;
*/
public class EngineContext {
+ public static final int DISABLED = -1;
+ public static final int EXECUTING = 1;
+ public static final int ROLLBACK = 2;
+
public static final Logger LOGGER = LoggerFactory.getLogger(EngineContext.class);
private T req;
private S result;
@@ -31,6 +35,12 @@ public class EngineContext {
private ExceptionHandler handler;
private boolean rollback;
private final Map> asyncInvokes = new ConcurrentHashMap<>();
+ private String engineName;
+ private ExecutionListener listener;
+ /**
+ * 执行状态
+ */
+ private int executing;
/**
* Returns current invoked trace.
@@ -93,6 +103,26 @@ public class EngineContext {
this.handler = handler;
}
+ public int getExecuting() {
+ return executing;
+ }
+
+ public void setExecuting(int executing) {
+ this.executing = executing;
+ }
+
+ public void setListener(ExecutionListener listener) {
+ this.listener = listener;
+ }
+
+ public String getEngineName() {
+ return engineName;
+ }
+
+ public void setEngineName(String engineName) {
+ this.engineName = engineName;
+ }
+
public void addAsyncInvoke(Component component, Future future, List> belongs) {
AsyncCallResult result = new AsyncCallResult<>();
result.setFuture(future);
@@ -111,19 +141,30 @@ public class EngineContext {
this.asyncInvokes.forEach((k, v) -> v.checkAndWait(this));
}
- /*
- Delegate methods
- */
- public void enter(String message) {
+ public void enter(Object obj) {
+ String message = this.executing == ROLLBACK ? "rollback " : "";
+ if (obj instanceof Describable) {
+ message += ("rollback " + ((Describable) obj).describe());
+ } else if (obj instanceof String) {
+ message += ("rollback " + obj);
+ }
+
this.tracer.enter(message);
- }
- public void exit() {
- this.tracer.exit();
+ if (executing == EXECUTING) {
+ listener.beforeExecute(this, obj);
+ } else if (executing == ROLLBACK) {
+ listener.beforeRollback(this, obj);
+ }
}
- public void enterRollback(String message) {
- this.tracer.enter("rollback " + message);
+ public void exit(Object obj) {
+ this.tracer.exit();
+ if (executing == EXECUTING) {
+ listener.afterExecute(this, obj);
+ } else if (executing == ROLLBACK) {
+ listener.afterRollback(this, obj);
+ }
}
@SuppressWarnings("unchecked")
@@ -154,6 +195,9 @@ public class EngineContext {
this.broken = false;
this.rollback = false;
this.fatal = null;
+ this.listener = null;
+ this.engineName = null;
+ this.executing = DISABLED;
}
private static class Value implements Serializable {
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListener.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListener.java
new file mode 100644
index 0000000000000000000000000000000000000000..88328754706b07fedb262a843fadfb6034e47c1e
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListener.java
@@ -0,0 +1,14 @@
+package org.smartboot.flow.core;
+
+/**
+ * @author qinluo
+ * @date 2022-11-25 20:34:35
+ * @since 1.0.0
+ */
+public interface ExecutionListener {
+
+ void beforeExecute(EngineContext context, Object object);
+ void afterExecute(EngineContext context, Object object);
+ void beforeRollback(EngineContext context, Object object);
+ void afterRollback(EngineContext context, Object object);
+}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerRegistry.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerRegistry.java
new file mode 100644
index 0000000000000000000000000000000000000000..2e7b7521f02e05d9799e56b6d7b67969dd352ad9
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerRegistry.java
@@ -0,0 +1,28 @@
+package org.smartboot.flow.core;
+
+import org.smartboot.flow.core.util.AssertUtil;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author qinluo
+ * @date 2022-11-25 21:15:22
+ * @since 1.0.0
+ */
+public class ExecutionListenerRegistry {
+
+ private static final List REGISTERED = new ArrayList<>();
+
+ public synchronized static void register(ExecutionListener listener) {
+ AssertUtil.notNull(listener, "listener must not be null");
+
+ if (!REGISTERED.contains(listener)) {
+ REGISTERED.add(listener);
+ }
+ }
+
+ public static List getRegistered() {
+ return new ArrayList<>(REGISTERED);
+ }
+}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerSupport.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerSupport.java
new file mode 100644
index 0000000000000000000000000000000000000000..f8062d7cfdc5031e65b3faa1e0108d0c24867f0e
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerSupport.java
@@ -0,0 +1,29 @@
+package org.smartboot.flow.core;
+
+/**
+ * @author qinluo
+ * @date 2022-11-25 21:29:22
+ * @since 1.0.0
+ */
+public class ExecutionListenerSupport implements ExecutionListener {
+
+ @Override
+ public void beforeExecute(EngineContext context, Object object) {
+
+ }
+
+ @Override
+ public void afterExecute(EngineContext context, Object object) {
+
+ }
+
+ @Override
+ public void beforeRollback(EngineContext context, Object object) {
+
+ }
+
+ @Override
+ public void afterRollback(EngineContext context, Object object) {
+
+ }
+}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListeners.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListeners.java
new file mode 100644
index 0000000000000000000000000000000000000000..5e62709104ec5b9e6f13070b312dacceec22bd74
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListeners.java
@@ -0,0 +1,66 @@
+package org.smartboot.flow.core;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * @author qinluo
+ * @date 2022-11-25 21:54:14
+ * @since 1.0.0
+ */
+public class ExecutionListeners implements ExecutionListener {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ExecutionListeners.class);
+
+ private final List listeners;
+
+ public ExecutionListeners(List listeners) {
+ this.listeners = listeners;
+ }
+
+ @Override
+ public void beforeExecute(EngineContext context, Object object) {
+ for (ExecutionListener listener : listeners) {
+ try {
+ listener.beforeExecute(context, object);
+ } catch (Throwable e) {
+ LOGGER.warn("execute listener {} failed", listener.getClass().getName(), e);
+ }
+ }
+ }
+
+ @Override
+ public void afterExecute(EngineContext context, Object object) {
+ for (ExecutionListener listener : listeners) {
+ try {
+ listener.afterExecute(context, object);
+ } catch (Throwable e) {
+ LOGGER.warn("execute listener {} failed", listener.getClass().getName(), e);
+ }
+ }
+ }
+
+ @Override
+ public void beforeRollback(EngineContext context, Object object) {
+ for (ExecutionListener listener : listeners) {
+ try {
+ listener.beforeRollback(context, object);
+ } catch (Throwable e) {
+ LOGGER.warn("execute listener {} failed", listener.getClass().getName(), e);
+ }
+ }
+ }
+
+ @Override
+ public void afterRollback(EngineContext context, Object object) {
+ for (ExecutionListener listener : listeners) {
+ try {
+ listener.afterRollback(context, object);
+ } catch (Throwable e) {
+ LOGGER.warn("execute listener {} failed", listener.getClass().getName(), e);
+ }
+ }
+ }
+}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/FlowEngine.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/FlowEngine.java
index a29bcbbdfed0f5ad0b10930152026ccb8c71e14a..3389146a518fe809f5add7e3e5cf023510a9ab4e 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/FlowEngine.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/FlowEngine.java
@@ -3,6 +3,7 @@ package org.smartboot.flow.core;
import org.smartboot.flow.core.exception.ExceptionHandler;
import org.smartboot.flow.core.manager.DefaultEngineManager;
+import org.smartboot.flow.core.metrics.MetricExecutionListener;
import org.smartboot.flow.core.util.AssertUtil;
import org.smartboot.flow.core.visitor.EngineVisitor;
import org.smartboot.flow.core.visitor.PipelineVisitor;
@@ -15,7 +16,7 @@ import java.util.concurrent.ExecutorService;
* @date 2022-11-12 21:58:12
* @since 1.0.0
*/
-public class FlowEngine implements Describable, Validator {
+public class FlowEngine implements Describable, Validator, Measurable {
private Pipeline pipeline;
private ExceptionHandler exceptionHandler;
@@ -43,7 +44,7 @@ public class FlowEngine implements Describable, Validator {
initContext(context);
- context.enter(describe());
+ context.enter(this);
boolean rollback = false;
try {
@@ -56,10 +57,13 @@ public class FlowEngine implements Describable, Validator {
context.ensureFinished();
if (rollback || context.getRollback()) {
+ context.setExecuting(EngineContext.ROLLBACK);
pipeline.rollback(context);
}
- context.exit();
+ // For end flow-engine.
+ context.setExecuting(EngineContext.EXECUTING);
+ context.exit(this);
if (context.getFatal() != null && exceptionHandler != null) {
context.getHandler().handle(context, context.getFatal());
@@ -72,6 +76,12 @@ public class FlowEngine implements Describable, Validator {
context.clear();
context.setHandler(exceptionHandler);
context.executor = executor;
+ context.setEngineName(this.name);
+ context.setExecuting(EngineContext.EXECUTING);
+
+ // Execution Listener.
+ ExecutionListeners listeners = new ExecutionListeners(ExecutionListenerRegistry.getRegistered());
+ context.setListener(listeners);
}
public void setExceptionHandler(ExceptionHandler exceptionHandler) {
@@ -125,9 +135,11 @@ public class FlowEngine implements Describable, Validator {
}
AssertUtil.notBlank(name, "engine's name must not be null");
- AssertUtil.notNull(pipeline, "engine[ " + getName() + " ]pipeline must not be null");
+ AssertUtil.notNull(pipeline, "engine[ " + name + " ]pipeline must not be null");
pipeline.validate();
validateCalled = true;
+ // Register metrics listener.
+ ExecutionListenerRegistry.register(MetricExecutionListener.getInstance());
DefaultEngineManager.getDefaultManager().register(this);
}
}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/Measurable.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/Measurable.java
new file mode 100644
index 0000000000000000000000000000000000000000..57a12ca6d35c46b6cac5710b40c3e106a78ef05c
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/Measurable.java
@@ -0,0 +1,31 @@
+package org.smartboot.flow.core;
+
+import org.smartboot.flow.core.metrics.Metrics;
+import org.smartboot.flow.core.metrics.MetricsManager;
+
+/**
+ * @author qinluo
+ * @date 2022-11-25 20:31:12
+ * @since 1.0.0
+ */
+public interface Measurable {
+
+ /**
+ * 获取可度量的指标数据
+ *
+ * @return 指标数据
+ */
+ default Metrics getMetrics() {
+ return MetricsManager.allocate(this);
+ }
+
+ /**
+ * Reset metrics
+ */
+ default void reset() {
+ Metrics metrics = MetricsManager.allocate(this);
+ if (metrics != null) {
+ metrics.reset();
+ }
+ }
+}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/NamedCondition.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/NamedCondition.java
new file mode 100644
index 0000000000000000000000000000000000000000..ee5e559cf480df5f6f01becb505dc290c95ef30e
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/NamedCondition.java
@@ -0,0 +1,20 @@
+package org.smartboot.flow.core;
+
+/**
+ * @author qinluo
+ * @date 2022-11-11 21:57:29
+ * @since 1.0.0
+ */
+public abstract class NamedCondition extends Condition {
+
+ private String name;
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String describe() {
+ return this.name;
+ }
+}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java
index efbdc092adf2699c955b50a1d9be40e40cb84173..b9d845758b0b2539464067363fb5f49b987c4eec 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java
@@ -15,7 +15,7 @@ import java.util.concurrent.Future;
* @date 2022-11-12 21:57:43
* @since 1.0.0
*/
-public class Pipeline implements Rollback, Describable, Validator {
+public class Pipeline implements Rollback, Describable, Validator, Measurable {
private final List> components = new ArrayList<>();
private String name;
@@ -35,7 +35,7 @@ public class Pipeline implements Rollback, Describable, Validator {
context.putExt(Key.of(this), executed);
// Enter record track
- context.enter(describe());
+ context.enter(this);
for (Component component : components) {
if (context.isBroken()) {
@@ -67,7 +67,7 @@ public class Pipeline implements Rollback, Describable, Validator {
}
// Exit record track
- context.exit();
+ context.exit(this);
}
private void ensureAllDependsExecuted(Component component, EngineContext context) {
@@ -118,7 +118,7 @@ public class Pipeline implements Rollback, Describable, Validator {
return;
}
- context.enterRollback(describe());
+ context.enter(this);
// Execute rollback desc.
for (int i = executed.size() - 1; i >= 0; i--) {
@@ -130,7 +130,7 @@ public class Pipeline implements Rollback, Describable, Validator {
}
}
- context.exit();
+ context.exit(this);
}
public boolean isRollbackable(EngineContext context) {
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/builder/EngineBuilder.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/builder/EngineBuilder.java
index 4a813398770f2d4641fe790704fb6e23185ca973..d54e1c0acda8926ba5608f7d56d1cbd5e2061e3e 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/builder/EngineBuilder.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/builder/EngineBuilder.java
@@ -18,8 +18,8 @@ public class EngineBuilder {
private String name;
private ExecutorService executor;
- public EngineBuilder() {
- this.name = "defaultEngine";
+ public EngineBuilder(String name) {
+ this.name = name;
}
public EngineBuilder pipeline(Pipeline pipeline) {
@@ -29,7 +29,7 @@ public class EngineBuilder {
}
public EngineBuilder name(String name) {
- AssertUtil.notNull(name, "must not be null");
+ AssertUtil.notBlank(name, "engine's name is required");
this.name = name;
return this;
}
@@ -42,6 +42,8 @@ public class EngineBuilder {
public FlowEngine build() {
+ AssertUtil.notBlank(name, "engine's name is required");
+
FlowEngine engine = new FlowEngine<>();
engine.setName(name);
engine.setPipeline(pipeline);
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/builder/PipelineBuilder.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/builder/PipelineBuilder.java
index c0337043583894cad7dcccac3689e8858cbe718a..4b5adff317125ba071336f4b8c55ee80fd9389ac 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/builder/PipelineBuilder.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/builder/PipelineBuilder.java
@@ -32,15 +32,11 @@ public class PipelineBuilder {
}
public PipelineBuilder(String name) {
- this((PipelineBuilder)null, name);
- }
-
- public PipelineBuilder() {
- this((PipelineBuilder)null, "DefaultPipeline");
+ this(null, name);
}
public PipelineBuilder name(String name) {
- AssertUtil.notNull(name, "must not be null");
+ AssertUtil.notBlank(name, "must not be null");
this.name = name;
return this;
}
@@ -68,12 +64,8 @@ public class PipelineBuilder {
return new ChooseBuilder<>(this, condition);
}
- public PipelineBuilder pipeline() {
- return pipeline("Subprocess");
- }
-
public PipelineBuilder pipeline(String name) {
- AssertUtil.notNull(name, "must not be null");
+ AssertUtil.notBlank(name, "must not be null");
return new PipelineBuilder<>(this, name);
}
@@ -85,12 +77,15 @@ public class PipelineBuilder {
Pipeline pipeline = build();
PipelineComponent pipelineComponent = new PipelineComponent<>(pipeline);
+ pipelineComponent.setName("anonymous@" + name);
parent.next(pipelineComponent);
return parent;
}
public Pipeline build() {
+ AssertUtil.notBlank(name, "pipeline's name is required");
+
Pipeline pipeline = new Pipeline<>();
pipeline.setComponents(this.components);
pipeline.setName(name);
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/ChooseComponent.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/ChooseComponent.java
index d831e6f3ca78577b65b33b75b0adeae2261aff42..4e4563e5c1a378d9b019b109a931fe287e7a045e 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/ChooseComponent.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/ChooseComponent.java
@@ -50,7 +50,7 @@ public class ChooseComponent extends Component {
@Override
public int invoke(EngineContext context) throws Exception {
- context.enter(describe());
+ context.enter(this);
try {
Object branch = condition.test(context);
Component execute = null;
@@ -73,11 +73,11 @@ public class ChooseComponent extends Component {
try {
return execute.invoke(context);
} finally {
- context.exit();
+ context.exit("branch##" + branch);
}
}
} finally {
- context.exit();
+ context.exit(this);
}
return 1;
@@ -96,18 +96,18 @@ public class ChooseComponent extends Component {
return;
}
- context.enterRollback(describe());
+ context.enter(this);
try {
executed.rollback(context);
} finally {
- context.exit();
+ context.exit(this);
}
}
@Override
public String describe() {
- return "choose##" + condition.describe();
+ return "choose@" + condition.describe();
}
@Override
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/Component.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/Component.java
index bb5b75c9cb443a50460d344cd6a03105deef762b..25f24159f0cfbd3fa26610860f1445d8842fad04 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/Component.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/Component.java
@@ -3,6 +3,7 @@ package org.smartboot.flow.core.component;
import org.smartboot.flow.core.Describable;
import org.smartboot.flow.core.EngineContext;
+import org.smartboot.flow.core.Measurable;
import org.smartboot.flow.core.Rollback;
import org.smartboot.flow.core.Validator;
import org.smartboot.flow.core.visitor.ComponentVisitor;
@@ -16,7 +17,7 @@ import java.util.List;
* @date 2022-11-12 17:58:34
* @since 1.0.0
*/
-public abstract class Component implements Rollback, Describable, Validator {
+public abstract class Component implements Rollback, Describable, Validator, Measurable {
/**
* 是否可降级
@@ -30,7 +31,7 @@ public abstract class Component implements Rollback, Describable, Va
private boolean async;
private long timeout;
private List dependsOn;
- private String name = "none";
+ private String name;
private boolean enabled = true;
protected final List attributes = new ArrayList<>(0);
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/IfComponent.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/IfComponent.java
index 25706d2928c383f623b2dc0eeec4a4ca2069b278..e04fa635486bb04bdb08029faf666c52974734d3 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/IfComponent.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/IfComponent.java
@@ -40,7 +40,7 @@ public class IfComponent extends Component{
@Override
public int invoke(EngineContext context) throws Exception {
- context.enter(describe());
+ context.enter(this);
try {
Object test = condition.test(context);
Component execute = null;
@@ -56,7 +56,7 @@ public class IfComponent extends Component{
return execute.invoke(context);
}
} finally {
- context.exit();
+ context.exit(this);
}
return 1;
@@ -75,17 +75,17 @@ public class IfComponent extends Component{
return;
}
- context.enterRollback(describe());
+ context.enter(this);
try {
executed.rollback(context);
} finally {
- context.exit();
+ context.exit(this);
}
}
@Override
public String describe() {
- return "if-component condition = " + condition.describe();
+ return "if@" + condition.describe();
}
@Override
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/PipelineComponent.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/PipelineComponent.java
index 8c2b18c89be3152795433cedbd29e1b41ddc12e9..8f8b01b640ac9682c2d9877af7397afc93d7be60 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/PipelineComponent.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/PipelineComponent.java
@@ -43,17 +43,17 @@ public class PipelineComponent extends Component {
if (!isRollbackable(context)) {
return;
}
- context.enterRollback(describe());
+ context.enter(this);
try {
pipeline.rollback(context);
} finally {
- context.exit();
+ context.exit(this);
}
}
@Override
public String describe() {
- return "pipeline##" + pipeline.describe();
+ return "pipeline@" + pipeline.describe();
}
@Override
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/executable/ExecutableAdapter.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/executable/ExecutableAdapter.java
index 43c5848e3f444f29ab3e8abc9d7d58c373cd9d4a..b5423dcca0c4847638ac3f1ccc76953daf703f85 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/executable/ExecutableAdapter.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/executable/ExecutableAdapter.java
@@ -29,12 +29,12 @@ public class ExecutableAdapter extends Component implements Rollback<
}
public int invoke(EngineContext context) {
- context.enter(describe());
+ context.enter(this);
try {
executable.execute(context);
} finally {
- context.exit();
+ context.exit(this);
}
return 1;
@@ -43,12 +43,12 @@ public class ExecutableAdapter extends Component implements Rollback<
@Override
public void rollback(EngineContext context) {
- context.enter("rollback " + describe());
+ context.enter(this);
try {
executable.rollback(context);
} finally {
- context.exit();
+ context.exit(this);
}
}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/extension/ExtensionFactory.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/extension/ExtensionFactory.java
deleted file mode 100644
index 9e30d23855be138de414f2de52d487e453fd567e..0000000000000000000000000000000000000000
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/extension/ExtensionFactory.java
+++ /dev/null
@@ -1,27 +0,0 @@
-package org.smartboot.flow.core.extension;
-
-import org.smartboot.flow.core.util.AssertUtil;
-
-import java.util.ServiceLoader;
-
-/**
- * @author qinluo
- * @date 2022/11/19 14:23
- * @since 1.0.0
- */
-public class ExtensionFactory {
-
- /**
- * 获取扩展实现
- */
- public static T getExtension(Class type) {
- AssertUtil.notNull(type, "type must not be null!");
-
- ServiceLoader loaded = ServiceLoader.load(type);
- if (!loaded.iterator().hasNext()) {
- return null;
- }
-
- return loaded.iterator().next();
- }
-}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/ComponentModel.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/ComponentModel.java
index e844c38d9ec7f3f548456bd64aea53f6c2ca509e..417690129baf31254c63288adc35eeb46ba3e77b 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/ComponentModel.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/ComponentModel.java
@@ -5,6 +5,7 @@ import org.smartboot.flow.core.common.Uniqueness;
import org.smartboot.flow.core.component.AttributeHolder;
import org.smartboot.flow.core.component.Attributes;
import org.smartboot.flow.core.component.Component;
+import org.smartboot.flow.core.metrics.Metrics;
import java.util.HashMap;
import java.util.List;
@@ -31,13 +32,15 @@ public class ComponentModel extends Uniqueness {
// Used with type pipeline.
PipelineModel pipeline;
String condition;
- String branch;
+ private String branch;
+ private final Metrics metrics;
ComponentModel(String identifier, Component component) {
this.identifier = identifier;
this.component = component;
this.name = component.getName();
this.describe = component.describe();
+ this.metrics = component.getMetrics();
}
public Map getHolders() {
@@ -70,17 +73,40 @@ public class ComponentModel extends Uniqueness {
return branch;
}
+ public void setBranch(String branch) {
+ this.branch = branch;
+ }
+
public Map getComponents() {
+ return new HashMap<>(components);
+ }
+
+ Map collect() {
if (type == ComponentType.PIPELINE) {
- this.pipeline.collect();
- return pipeline.getComponents();
+ Map collected = this.pipeline.collect();
+ collected.put(this.identifier, this);
+ return collected;
+ } else if (type == ComponentType.BASIC) {
+ Map temp = new HashMap<>();
+ temp.put(this.identifier, this);
+ return temp;
+ } else {
+ Map temp = new HashMap<>();
+ this.components.forEach((k, v) -> temp.putAll(v.collect()));
+ temp.put(this.identifier, this);
+ return temp;
}
-
- return components;
}
void addComponent(ComponentModel model) {
- this.components.put(model.getIdentifier(), model);
+ if (this.type == ComponentType.CHOOSE) {
+ this.components.put(model.getBranch(), model);
+ } else if (this.type == ComponentType.IF) {
+ String key = this.components.size() > 0 ? "else" : "then";
+ this.components.put(key, model);
+ } else {
+ this.components.put(model.getIdentifier(), model);
+ }
}
public String getName() {
@@ -104,4 +130,13 @@ public class ComponentModel extends Uniqueness {
}
}
+
+ public Metrics getMetrics() {
+ return metrics;
+ }
+
+ public void reset() {
+ this.metrics.reset();
+ components.forEach((k, v) -> v.reset());
+ }
}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/DefaultEngineManager.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/DefaultEngineManager.java
index 722214f2346abbbae4acc98eaa4f4f479e7fe95f..1af1541724b33aef5c3844bcbca0de7fe86ceb15 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/DefaultEngineManager.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/DefaultEngineManager.java
@@ -1,10 +1,12 @@
package org.smartboot.flow.core.manager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.smartboot.flow.core.FlowEngine;
-import org.smartboot.flow.core.IdentifierManager;
import org.smartboot.flow.core.component.AttributeHolder;
import org.smartboot.flow.core.util.AssertUtil;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -16,6 +18,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class DefaultEngineManager implements EngineManager {
+ private static final Logger LOGGER = LoggerFactory.getLogger(DefaultEngineManager.class);
private static final DefaultEngineManager INSTANCE = new DefaultEngineManager();
private final Map registeredEngines = new ConcurrentHashMap<>();
@@ -26,8 +29,18 @@ public class DefaultEngineManager implements EngineManager {
@Override
public void register(FlowEngine engine) {
- IdentifierManager identifierManager = new NamedIdentifierManager(engine.getName());
- RegisterEngineVisitor visitor = new RegisterEngineVisitor(identifierManager);
+ AssertUtil.notNull(engine, "registered engine must not be null");
+ AssertUtil.notBlank(engine.getName(), "registered engine name must not be blank");
+
+ if (registeredEngines.get(engine.getName()) != null) {
+ LOGGER.error("engine {} already registered", engine.getName());
+ return;
+ }
+
+ // Ensure engine and components is valid
+ engine.validate();
+
+ RegisterEngineVisitor visitor = new RegisterEngineVisitor();
visitor.visit(engine);
EngineModel model = visitor.getEngine();
@@ -36,12 +49,13 @@ public class DefaultEngineManager implements EngineManager {
@Override
public EngineModel getEngineModel(String name) {
- AssertUtil.notNull(name, "name must not be null!");
+ AssertUtil.notBlank(name, "name must not be blank!");
return registeredEngines.get(name);
}
@Override
public void changeAttributes(String identifier, List attributeHolders) {
+ AssertUtil.notBlank(identifier, "identifier must not be blank!");
if (registeredEngines.containsKey(identifier)) {
// change engine attributes.
return;
@@ -54,4 +68,40 @@ public class DefaultEngineManager implements EngineManager {
}
}
}
+
+ @Override
+ public List getRegisteredEngineNames() {
+ return new ArrayList<>(registeredEngines.keySet());
+ }
+
+ @Override
+ public void resetStatistic(String identifier) {
+ AssertUtil.notBlank(identifier, "identifier must not be blank!");
+ for (Map.Entry entry : registeredEngines.entrySet()) {
+ if (entry.getKey().equals(identifier) || entry.getValue().getPipeline().getIdentifier().equals(identifier)) {
+ entry.getValue().reset();
+ break;
+ }
+
+ }
+
+ for (Map.Entry entry : registeredEngines.entrySet()) {
+ if (entry.getValue().containsComponent(identifier)) {
+ entry.getValue().reset(identifier);
+ break;
+ }
+
+ }
+ }
+
+ @Override
+ public void detachAll() {
+ this.registeredEngines.clear();
+ }
+
+ @Override
+ public void detach(String name) {
+ AssertUtil.notBlank(name, "name must not be blank!");
+ this.registeredEngines.remove(name);
+ }
}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/EngineManager.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/EngineManager.java
index accf946240c5e140d3c351e1d18ac2261699b066..92406ee080e7aae6536d143eef248c272de1b879 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/EngineManager.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/EngineManager.java
@@ -38,6 +38,13 @@ public interface EngineManager {
*/
void changeAttributes(String identifier, List attributeHolders);
+ /**
+ * Returns all registered engine's name.
+ *
+ * @return engine's names.
+ */
+ List getRegisteredEngineNames();
+
/**
* Dynamic change component attributes.
*
@@ -47,4 +54,23 @@ public interface EngineManager {
default void changeAttributes(String identifier, AttributeHolder holder) {
this.changeAttributes(identifier, Collections.singletonList(holder));
}
+
+ /**
+ * Reset statistic data.
+ *
+ * @param identifier component identifier
+ */
+ void resetStatistic(String identifier);
+
+ /**
+ * Make all engine detach from manager.
+ */
+ void detachAll();
+
+ /**
+ * Make specific engine detach from manager
+ *
+ * @param name engine name.
+ */
+ void detach(String name);
}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/EngineModel.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/EngineModel.java
index 88febc21425ce505d4284b7f63e73949026ddc0e..cd5ee5a9d272b6923a199fce6a9797d5382817e3 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/EngineModel.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/EngineModel.java
@@ -4,6 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smartboot.flow.core.common.Uniqueness;
import org.smartboot.flow.core.component.AttributeHolder;
+import org.smartboot.flow.core.metrics.Metrics;
import java.util.HashMap;
import java.util.List;
@@ -20,7 +21,8 @@ public class EngineModel extends Uniqueness {
private static final Logger LOGGER = LoggerFactory.getLogger(EngineModel.class);
private PipelineModel pipeline;
- private final Map components = new ConcurrentHashMap<>();
+ private transient final Map components = new ConcurrentHashMap<>();
+ private Metrics metrics;
public EngineModel(String name) {
// Engine's name must be global unique.
@@ -54,7 +56,29 @@ public class EngineModel extends Uniqueness {
}
void collect() {
- this.pipeline.collect();
- this.components.putAll(this.pipeline.getComponents());
+ this.components.putAll(this.pipeline.collect());
+ }
+
+ public Metrics getMetrics() {
+ return metrics;
+ }
+
+ public void setMetrics(Metrics metrics) {
+ this.metrics = metrics;
+ }
+
+ public void reset() {
+ this.metrics.reset();
+ this.pipeline.reset();
+ }
+
+ public void reset(String identifier) {
+ ComponentModel model = components.get(identifier);
+ if (model == null) {
+ LOGGER.warn("change component attributes failed, identifier = {}", identifier);
+ return;
+ }
+
+ model.reset();
}
}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/ManagerAction.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/ManagerAction.java
new file mode 100644
index 0000000000000000000000000000000000000000..b0d536f8ed0e0e5674d9ba9470dad965a3cdd61d
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/ManagerAction.java
@@ -0,0 +1,32 @@
+package org.smartboot.flow.core.manager;
+
+import java.util.Objects;
+
+/**
+ * @author qinluo
+ * @date 2022/11/22 22:02
+ * @since 1.0.0
+ */
+public enum ManagerAction {
+ /**
+ * Change component attributes.
+ */
+ CHANGE_ATTRIBUTES,
+
+ /**
+ * Reset statistic metrics.
+ */
+ RESET_METRICS,
+
+ ;
+
+ public static ManagerAction get(String name) {
+ if (Objects.equals(name, CHANGE_ATTRIBUTES.name())) {
+ return CHANGE_ATTRIBUTES;
+ } else if (Objects.equals(name, RESET_METRICS.name())) {
+ return RESET_METRICS;
+ }
+
+ return null;
+ }
+}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/NamedIdentifierManager.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/NamedIdentifierManager.java
deleted file mode 100644
index 3e1d8867ffde818c135389c2199cf763dc69f051..0000000000000000000000000000000000000000
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/NamedIdentifierManager.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package org.smartboot.flow.core.manager;
-
-import org.smartboot.flow.core.DefaultIdentifierManager;
-import org.smartboot.flow.core.IdentifierManager;
-
-/**
- * @author qinluo
- * @date 2022/11/19 12:37
- * @since 1.0.0
- */
-public class NamedIdentifierManager implements IdentifierManager {
-
- private final String name;
- private final IdentifierManager delegate = new DefaultIdentifierManager();
-
- public NamedIdentifierManager(String name) {
- this.name = name;
- }
-
- @Override
- public String generateIdentifier(String prefix) {
- return delegate.generateIdentifier(name + "-" + prefix);
- }
-}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/PipelineModel.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/PipelineModel.java
index 121226188d247d124ae475b98f791153375d9294..0c03cf7994662ad50771b1ac1a0234dce85f7ba9 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/PipelineModel.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/PipelineModel.java
@@ -1,8 +1,11 @@
package org.smartboot.flow.core.manager;
import org.smartboot.flow.core.common.Uniqueness;
+import org.smartboot.flow.core.metrics.Metrics;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -13,29 +16,46 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class PipelineModel extends Uniqueness {
- private final Map components = new ConcurrentHashMap<>();
+ private final List components = new ArrayList<>();
private final String name;
+ private Metrics metrics;
PipelineModel(String name, String identifier) {
this.name = name;
this.identifier = identifier;
}
- public Map getComponents() {
- return new HashMap<>(components);
+ public List getComponents() {
+ return new ArrayList<>(components);
}
void addComponent(ComponentModel component) {
- this.components.put(component.getIdentifier(), component);
+ this.components.add(component);
}
public String getName() {
return name;
}
- void collect() {
- for (ComponentModel model : components.values()) {
- this.components.putAll(model.getComponents());
+ Map collect() {
+ HashMap collected = new HashMap<>(components.size());
+ for (ComponentModel model : components) {
+ collected.putAll(model.collect());
}
+
+ return collected;
+ }
+
+ public Metrics getMetrics() {
+ return metrics;
+ }
+
+ public void setMetrics(Metrics metrics) {
+ this.metrics = metrics;
+ }
+
+ public void reset() {
+ this.metrics.reset();
+ components.forEach(ComponentModel::reset);
}
}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisterEngineVisitor.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisterEngineVisitor.java
index 4163e3e3565586d8c0434d899068c11ff9c123e9..6b66aa47e1e27dfb65865e3a60f0a5136d2c1be7 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisterEngineVisitor.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisterEngineVisitor.java
@@ -1,7 +1,8 @@
package org.smartboot.flow.core.manager;
-import org.smartboot.flow.core.IdentifierManager;
+import org.smartboot.flow.core.FlowEngine;
import org.smartboot.flow.core.Pipeline;
+import org.smartboot.flow.core.util.ContactUtils;
import org.smartboot.flow.core.visitor.EngineVisitor;
import org.smartboot.flow.core.visitor.PipelineVisitor;
@@ -13,10 +14,12 @@ import org.smartboot.flow.core.visitor.PipelineVisitor;
public class RegisterEngineVisitor extends EngineVisitor {
private EngineModel model;
- private final IdentifierManager identifierManager;
- public RegisterEngineVisitor(IdentifierManager identifierManager) {
- this.identifierManager = identifierManager;
+ @Override
+ public void visit(FlowEngine flowEngine) {
+ this.model = new EngineModel(flowEngine.getName());
+ this.model.setMetrics(flowEngine.getMetrics());
+ super.visit(flowEngine);
}
@Override
@@ -24,16 +27,12 @@ public class RegisterEngineVisitor extends EngineVisitor {
this.model.collect();
}
- @Override
- public void visit(String engine) {
- this.model = new EngineModel(engine);
- }
-
@Override
public PipelineVisitor visitPipeline(Pipeline pipeline) {
- PipelineModel pipelineModel = new PipelineModel(pipeline.describe(), identifierManager.generateIdentifier(pipeline.describe()));
+ PipelineModel pipelineModel = new PipelineModel(pipeline.describe(), ContactUtils.contact(this.model.getIdentifier(), pipeline.describe()));
this.model.setPipeline(pipelineModel);
- return new RegisteredPipelineVisitor(pipelineModel, identifierManager);
+ pipelineModel.setMetrics(pipeline.getMetrics());
+ return new RegisteredPipelineVisitor(pipelineModel);
}
public EngineModel getEngine() {
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisteredComponentVisitor.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisteredComponentVisitor.java
index e383a794aa7f7856e8a5b91a791796dbfc753f94..0193f4acc29b892952365173c9f0e7e94f028fe1 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisteredComponentVisitor.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisteredComponentVisitor.java
@@ -1,10 +1,11 @@
package org.smartboot.flow.core.manager;
-import org.smartboot.flow.core.IdentifierManager;
import org.smartboot.flow.core.Pipeline;
import org.smartboot.flow.core.common.ComponentType;
import org.smartboot.flow.core.component.AttributeHolder;
import org.smartboot.flow.core.component.Component;
+import org.smartboot.flow.core.util.AuxiliaryUtils;
+import org.smartboot.flow.core.util.ContactUtils;
import org.smartboot.flow.core.visitor.ComponentVisitor;
import org.smartboot.flow.core.visitor.PipelineVisitor;
@@ -18,19 +19,18 @@ import java.util.List;
public class RegisteredComponentVisitor extends ComponentVisitor {
private final ComponentModel model;
- private final IdentifierManager identifierManager;
- public RegisteredComponentVisitor(ComponentModel comp, IdentifierManager identifierManager) {
+ public RegisteredComponentVisitor(ComponentModel comp) {
this.model = comp;
- this.identifierManager = identifierManager;
}
@Override
public PipelineVisitor visitPipeline(Pipeline pipeline) {
- PipelineModel pipelineModel = new PipelineModel(pipeline.describe(), identifierManager.generateIdentifier("pipeline"));
+ PipelineModel pipelineModel = new PipelineModel(pipeline.describe(), ContactUtils.contact(model.getIdentifier(), pipeline.describe()));
this.model.pipeline = pipelineModel;
this.model.type = (ComponentType.PIPELINE);
- return new RegisteredPipelineVisitor(pipelineModel, identifierManager);
+ pipelineModel.setMetrics(pipeline.getMetrics());
+ return new RegisteredPipelineVisitor(pipelineModel);
}
@Override
@@ -41,9 +41,17 @@ public class RegisteredComponentVisitor extends ComponentVisitor {
@Override
public ComponentVisitor visitComponent(Component component) {
- ComponentModel comp = new ComponentModel(identifierManager.generateIdentifier(component.getName()), component);
+ String identifier = ContactUtils.contact(model.getIdentifier(), AuxiliaryUtils.or(component.getName(), component.describe()));
+ String branch = null;
+ if (this.model.type == ComponentType.CHOOSE) {
+ identifier = ContactUtils.contact(model.getIdentifier(), "default");
+ branch = "default";
+ }
+
+ ComponentModel comp = new ComponentModel(identifier, component);
+ comp.setBranch(branch);
this.model.addComponent(comp);
- return new RegisteredComponentVisitor(comp, identifierManager);
+ return new RegisteredComponentVisitor(comp);
}
@Override
@@ -53,11 +61,11 @@ public class RegisteredComponentVisitor extends ComponentVisitor {
@Override
public ComponentVisitor visitBranch(Object branch, Component component) {
- ComponentModel model = new ComponentModel(identifierManager.generateIdentifier(component.getName()), component);
- model.branch = (String.valueOf(branch));
- this.model.addComponent(model);
+ ComponentModel model = new ComponentModel(ContactUtils.contact(this.model.getIdentifier(), "branch", String.valueOf(branch)), component);
+ model.setBranch((String.valueOf(branch)));
this.model.type = (ComponentType.CHOOSE);
- return new RegisteredComponentVisitor(model, identifierManager);
+ this.model.addComponent(model);
+ return new RegisteredComponentVisitor(model);
}
@Override
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisteredPipelineVisitor.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisteredPipelineVisitor.java
index e79ae84638e51e304b7160cfda17ed4342b58829..9c62badb17c047310e0570d3977fe343bdb981bf 100644
--- a/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisteredPipelineVisitor.java
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/manager/RegisteredPipelineVisitor.java
@@ -1,7 +1,8 @@
package org.smartboot.flow.core.manager;
-import org.smartboot.flow.core.IdentifierManager;
import org.smartboot.flow.core.component.Component;
+import org.smartboot.flow.core.util.AuxiliaryUtils;
+import org.smartboot.flow.core.util.ContactUtils;
import org.smartboot.flow.core.visitor.ComponentVisitor;
import org.smartboot.flow.core.visitor.PipelineVisitor;
@@ -13,17 +14,15 @@ import org.smartboot.flow.core.visitor.PipelineVisitor;
public class RegisteredPipelineVisitor extends PipelineVisitor {
private final PipelineModel pipelineModel;
- private final IdentifierManager identifierManager;
- public RegisteredPipelineVisitor(PipelineModel pipelineModel, IdentifierManager identifierManager) {
+ public RegisteredPipelineVisitor(PipelineModel pipelineModel) {
this.pipelineModel = pipelineModel;
- this.identifierManager = identifierManager;
}
@Override
public ComponentVisitor visitComponent(Component component) {
- ComponentModel comp = new ComponentModel(identifierManager.generateIdentifier(this.pipelineModel.getName() + "-" +component.getName()), component);
+ ComponentModel comp = new ComponentModel(ContactUtils.contact(this.pipelineModel.getIdentifier(), AuxiliaryUtils.or(component.getName(), component.describe())), component);
this.pipelineModel.addComponent(comp);
- return new RegisteredComponentVisitor(comp, identifierManager);
+ return new RegisteredComponentVisitor(comp);
}
}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/metrics/DefaultMetricsCreator.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/metrics/DefaultMetricsCreator.java
new file mode 100644
index 0000000000000000000000000000000000000000..2f15888110ac9ab340f40154177593fac15ac3e9
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/metrics/DefaultMetricsCreator.java
@@ -0,0 +1,14 @@
+package org.smartboot.flow.core.metrics;
+
+/**
+ * @author qinluo
+ * @date 2022-11-25 21:51:52
+ * @since 1.0.0
+ */
+public class DefaultMetricsCreator implements MetricsCreator {
+
+ @Override
+ public Metrics create() {
+ return new Metrics();
+ }
+}
diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/metrics/MetricExecutionListener.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/metrics/MetricExecutionListener.java
new file mode 100644
index 0000000000000000000000000000000000000000..0d441c7b2fbd4fb2081dee56e7d169d501117402
--- /dev/null
+++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/metrics/MetricExecutionListener.java
@@ -0,0 +1,108 @@
+package org.smartboot.flow.core.metrics;
+
+import org.smartboot.flow.core.EngineContext;
+import org.smartboot.flow.core.ExecutionListener;
+import org.smartboot.flow.core.Key;
+import org.smartboot.flow.core.Measurable;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author qinluo
+ * @date 2022-11-25 21:23:28
+ * @since 1.0.0
+ */
+public class MetricExecutionListener implements ExecutionListener {
+
+ private static final ExecutionListener INSTANCE = new MetricExecutionListener();
+
+ public static ExecutionListener getInstance() {
+ return INSTANCE;
+ }
+
+ @Override
+ public void beforeExecute(EngineContext context, Object object) {
+ if (!(object instanceof Measurable)) {
+ return;
+ }
+
+ Map