1.flow中的运用
flowable中主要运用了命令模式来处理几个主要方法的执行
1.1 CommandExecutor是在系统启动时,初始化流程引擎时加载出来
public class ProcessEngineFactoryBean implements FactoryBean<ProcessEngine>, DisposableBean, ApplicationContextAware {
@Override
public ProcessEngine getObject() throws Exception {
this.configureExpressionManager();
this.configureExternallyManagedTransactions();
if (this.processEngineConfiguration.getBeans() == null) {
this.processEngineConfiguration.setBeans(new SpringBeanFactoryProxyMap(this.applicationContext));
}
//初始化流程引擎
this.processEngine = this.processEngineConfiguration.buildProcessEngine();
return this.processEngine;
}
@Override
public ProcessEngine buildProcessEngine() {
//初始化
init();
//....
}
}
流程引擎工厂实现了FactoryBean,执行getObject方法时会初始流程引擎
1.2 流程在执行方法时(通过各种已经提前注册好的service),首先构建Command对象,通过CommandExecutor执行
@Override
public void complete(String taskId, Map<String, Object> variables) {
commandExecutor.execute(new CompleteTaskCmd(taskId, variables));
}
public class CompleteTaskCmd extends NeedsActiveTaskCmd<Void> {
@Override
protected Void execute(CommandContext commandContext, TaskEntity task) {
//所有业务执行都在Cmd的实现类实现,最后通过CommandInvoker执行
}
}
由于各个业务的service都是继承CommonEngineServiceImpl ,此时CommandExecutor已经提前注入。
1.3CommandExecutor执行CommandInterceptor责任链模式
public class CommandExecutorImpl implements CommandExecutor {
protected CommandInterceptor first;
@Override
public <T> T execute(CommandConfig config, Command<T> command) {
return first.execute(config, command, this);
}
}
//责任链模式,每个CommandInterceptor实现类都会存储next
public interface CommandInterceptor {
<T> T execute(CommandConfig config, Command<T> command, CommandExecutor commandExecutor);
CommandInterceptor getNext();
void setNext(CommandInterceptor next);
}
1.4 总结
flowable的业务逻辑通过命令模式配合责任链模式解决了每个业务的执行与调度解耦。通过封装业务需要的命令,配合责任链共同组成每一个业务执行需要的逻辑,逻辑间相互解耦。请求者只需要找到对应的service,然后创建命令类,拦截器相当于命令模式中的接收者,CommandExecutor相当于执行者,执行已经接收组装好的命令