flow - 拦截器及作用(FlowInterceptor)
2025年12月5日 上午10:37:07
在流程引擎身上附加拦截器,可实现计时、异常控制、上下文参数调整等。
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 Throwable;
}