flow - 拦截器(FlowInterceptor)
2025年12月20日 下午11:58:46
在流程引擎身上附加拦截器,可实现计时、异常控制、上下文参数调整等。
1、使用示例
组件注解模式
import org.noear.solon.annotation.Component;
import org.noear.solon.flow.intercept.FlowInterceptor;
import org.noear.solon.flow.intercept.FlowInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
public class FlowInterceptorImpl implements FlowInterceptor {
static Logger log = LoggerFactory.getLogger(ChainInterceptorImpl.class);
@Override
public void doIntercept(FlowInvocation inv) throws Throwable {
long start = System.currentTimeMillis();
try {
inv.invoke();
} catch (Throwable ex) {
log.error("Graph eval failure: " + inv.getStartNode().getGraph().getId(), ex);
} finally {
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
}
原生 java 模式:
flowEngine.addInterceptor(new FlowInterceptorImpl());
2、FlowInterceptor 接口参考
public interface FlowInterceptor {
/**
* 拦截执行
*
* @param invocation 调用者
*/
void doIntercept(FlowInvocation invocation) throws FlowException;
/**
* 节点运行开始时
*
* @since 3.4
*/
default void onNodeStart(FlowContext context, Node node) {
}
/**
* 节点运行结束时
*
* @since 3.4
*/
default void onNodeEnd(FlowContext context, Node node) {
}
}