Activiti 工作流引擎 ~ ProcessEngine实例创建

本文使用的Activiti版本为5.22.0

ProcessEngineActiviti 的核心。

首先我们来看下它的配置信息

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <!-- //... -->
  </bean>

  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean" destroy-method="destroy">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
  </bean>

  <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
  <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
  <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
  <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />

以上配置信息从Activiti官方提供的demo中拷贝的

由这段配置我们可以看出,ProcessEngine依赖ProcessEngineConfiguration,而我们在程序中需要用到的多个serviceProcessEngine提供

接下来我们看下代码中具体是如何实现的

public class ProcessEngineFactoryBean implements FactoryBean<ProcessEngine>, DisposableBean, ApplicationContextAware {


    protected ProcessEngineConfigurationImpl processEngineConfiguration;

    protected ApplicationContext applicationContext;
    protected ProcessEngine processEngine;

    public void destroy() throws Exception {
        if (processEngine != null) {
            processEngine.close();
        }
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public ProcessEngine getObject() throws Exception {
        configureExpressionManager();
        configureExternallyManagedTransactions();

        if (processEngineConfiguration.getBeans() == null) {
            processEngineConfiguration.setBeans(new SpringBeanFactoryProxyMap(applicationContext));
        }

        this.processEngine = processEngineConfiguration.buildProcessEngine();
        return this.processEngine;
    }

    public Class<ProcessEngine> getObjectType() {
        return ProcessEngine.class;
    }

    public boolean isSingleton() {
        return true;
    }

}

可以看出ProcessEngineFactoryBean类实现了spring中的FactoryBean接口,当初始化spring应用上下文环境时,会调用该接口(FactoryBean)的实现类(ProcessEngineFactoryBean)中的getObject()生成指定的对象,此处生成的是ProcessEngine对象

this.processEngine = processEngineConfiguration.buildProcessEngine();

public class SpringProcessEngineConfiguration extends ProcessEngineConfigurationImpl implements ApplicationContextAware {
    @Override
    public ProcessEngine buildProcessEngine() {
        ProcessEngine processEngine = super.buildProcessEngine();
        ProcessEngines.setInitialized(true);
        autoDeployResources(processEngine);
        return processEngine;
    }
}

SpringProcessEngineConfigurationbuildProcessEngine()调用了其父类的buildProcessEngine()
父类中的buildProcessEngine()将当前ProcessEngineConfiguration对象作为参数初始化ProcessEngineImpl

public abstract class ProcessEngineConfigurationImpl extends ProcessEngineConfiguration {
    // SERVICES /////////////////////////////////////////////////////////////////

    protected RepositoryService repositoryService = new RepositoryServiceImpl();
    protected RuntimeService runtimeService = new RuntimeServiceImpl();
    protected HistoryService historyService = new HistoryServiceImpl(this);
    protected IdentityService identityService = new IdentityServiceImpl();
    protected TaskService taskService = new TaskServiceImpl(this);
    protected FormService formService = new FormServiceImpl();
    protected ManagementService managementService = new ManagementServiceImpl();
    protected DynamicBpmnService dynamicBpmnService = new DynamicBpmnServiceImpl(this);

    public ProcessEngine buildProcessEngine() {
        init();
        return new ProcessEngineImpl(this);
    }
}
public abstract class ProcessEngineConfiguration implements EngineServices {
    protected String processEngineName = ProcessEngines.NAME_DEFAULT;
}

ProcessEngineImpl初始化完成后,我们就可以利用ProcessEngine获取相关service,如TaskService

public class ProcessEngineImpl implements ProcessEngine {

  protected String name;
  protected TaskService taskService;
  protected ProcessEngineConfigurationImpl processEngineConfiguration;

  public ProcessEngineImpl(ProcessEngineConfigurationImpl processEngineConfiguration) {
    this.processEngineConfiguration = processEngineConfiguration;
    this.name = processEngineConfiguration.getProcessEngineName();
    this.taskService = processEngineConfiguration.getTaskService();
    ProcessEngines.registerProcessEngine(this);
    //...
  }

  public String getName() {
    return name;
  }

  public TaskService getTaskService() {
    return taskService;
  }  

}

ProcessEngine实例创建完成后,如何在业务代码中获取该对象呢?
1.利用spring从上下文中获取
2.利用Activiti提供的ProcessEngines抽象类获取

ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

为什么通过ProcessEngines就可以拿到ProcessEngine实例呢?
ProcessEngineImplProcessEngineImpl(ProcessEngineConfigurationImpl processEngineConfiguration)构造函数中可以看到如下语句:

ProcessEngines.registerProcessEngine(this);

registerProcessEngine()processEngine实例保存到Map中,getDefaultProcessEngine()Map中获取名为defaultprocessEngine

public abstract class ProcessEngines {

  public static final String NAME_DEFAULT = "default";

  protected static boolean isInitialized = false; 
  protected static Map<String, ProcessEngine> processEngines = new HashMap<String, ProcessEngine>();

  public static void registerProcessEngine(ProcessEngine processEngine) {
    processEngines.put(processEngine.getName(), processEngine);
  }

  public static ProcessEngine getDefaultProcessEngine() {
    return getProcessEngine(NAME_DEFAULT);
  }

  public static ProcessEngine getProcessEngine(String processEngineName) {
    if (!isInitialized()) {
      init();
    }
    return processEngines.get(processEngineName);
  }

}

猜你喜欢

转载自blog.csdn.net/quan20111992/article/details/79827769