引擎启动流程如图
// ProcessEngineFactoryBean.getObjectpublic ProcessEngine getObject() throws Exception {// 如果 processEngine 为空if (processEngine == null) {// 初始化表达式管理器initializeExpressionManager();// 初始化事务外部管理initializeTransactionExternallyManaged();// 构建 processEngineprocessEngine = (ProcessEngineImpl) processEngineConfiguration.buildProcessEngine();}// 返回 processEnginereturn processEngine;}
//ProcessEngineConfigurationImpl.buildProcessEngine/*** 重写父类方法,用于构建流程引擎* @return 返回构建好的流程引擎对象*/@Overridepublic ProcessEngine buildProcessEngine() {// 初始化init();// 创建 ProcessEngine 实例processEngine = new ProcessEngineImpl(this);// 调用构建后处理方法invokePostProcessEngineBuild(processEngine);// 返回 ProcessEngine 实例return processEngine;}
init初始化
//ProcessEngineConfigurationImpl.initprotected void init() {// 调用preInit方法invokePreInit();// 初始化默认字符集initDefaultCharset();// 初始化历史级别initHistoryLevel();// 初始化历史事件生产者initHistoryEventProducer();// 初始化Cmmn历史事件生产者initCmmnHistoryEventProducer();// 初始化Dmn历史事件生产者initDmnHistoryEventProducer();// 初始化历史事件处理器initHistoryEventHandler();// 初始化表达式管理器initExpressionManager();// 初始化beaninitBeans();// 初始化artifact工厂initArtifactFactory();// 初始化表单引擎initFormEngines();// 初始化表单类型initFormTypes();// 初始化表单字段校验器initFormFieldValidators();// 初始化脚本inginitScripting();// 初始化Dmn引擎initDmnEngine();// 初始化业务日历管理器initBusinessCalendarManager();// 初始化命令上下文工厂initCommandContextFactory();// 初始化事务上下文工厂initTransactionContextFactory();// 数据库类型需要在命令执行器初始化之前进行检测initDataSource();// 初始化异常代码提供者initExceptionCodeProvider();// 初始化命令执行器initCommandExecutors();// 初始化服务initServices();// 初始化ID生成器initIdGenerator();// 初始化失败的作业命令工厂initFailedJobCommandFactory();// 初始化部署器initDeployers();// 初始化作业提供者initJobProvider();// 初始化外部任务优先级提供者initExternalTaskPriorityProvider();// 初始化批处理处理器initBatchHandlers();// 初始化作业执行器initJobExecutor();// 初始化事务工厂initTransactionFactory();// 初始化Sql会话工厂initSqlSessionFactory();// 初始化身份提供会话工厂initIdentityProviderSessionFactory();// 初始化会话工厂initSessionFactories();// 初始化值类型解析器initValueTypeResolver();// 初始化类型校验器initTypeValidator();// 初始化序列化initSerialization();// 初始化JpainitJpa();// 初始化代理拦截器initDelegateInterceptor();// 初始化事件处理initEventHandlers();// 初始化流程应用管理器initProcessApplicationManager();// 初始化关联处理器initCorrelationHandler();// 初始化条件处理器initConditionHandler();// 初始化异常处理器initIncidentHandlers();// 初始化密码摘要initPasswordDigest();// 初始化部署注册initDeploymentRegistration();// 初始化部署处理器工厂initDeploymentHandlerFactory();// 初始化资源授权提供者initResourceAuthorizationProvider();// 初始化权限提供者initPermissionProvider();// 初始化主机名initHostName();// 初始化指标initMetrics();// 初始化遥测initTelemetry();// 初始化迁移initMigration();// 初始化命令检查器initCommandCheckers();// 初始化默认用户权限initDefaultUserPermissionForTask();// 初始化历史删除时间initHistoryRemovalTime();// 初始化历史清理initHistoryCleanup();// 初始化每批次调用次数initInvocationsPerBatchJobByBatchType();// 初始化管理员initAdminUser();// 初始化管理员组initAdminGroups();// 初始化密码策略initPasswordPolicy();// 调用postInit方法invokePostInit();}
流程引擎实现类 ProcessEngineImpl
public ProcessEngineImpl(ProcessEngineConfigurationImpl processEngineConfiguration) {// 初始化变量this.processEngineConfiguration = processEngineConfiguration;this.name = processEngineConfiguration.getProcessEngineName();// 获取各服务接口this.repositoryService = processEngineConfiguration.getRepositoryService();this.runtimeService = processEngineConfiguration.getRuntimeService();this.historicDataService = processEngineConfiguration.getHistoryService();this.identityService = processEngineConfiguration.getIdentityService();this.taskService = processEngineConfiguration.getTaskService();this.formService = processEngineConfiguration.getFormService();this.managementService = processEngineConfiguration.getManagementService();this.authorizationService = processEngineConfiguration.getAuthorizationService();this.caseService = processEngineConfiguration.getCaseService();this.filterService = processEngineConfiguration.getFilterService();this.externalTaskService = processEngineConfiguration.getExternalTaskService();this.decisionService = processEngineConfiguration.getDecisionService();// 获取数据库模式更新设置、作业执行器和命令执行器this.databaseSchemaUpdate = processEngineConfiguration.getDatabaseSchemaUpdate();this.jobExecutor = processEngineConfiguration.getJobExecutor();this.commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired();this.commandExecutorSchemaOperations = processEngineConfiguration.getCommandExecutorSchemaOperations();// 获取会话工厂集合和历史级别this.sessionFactories = processEngineConfiguration.getSessionFactories();this.historyLevel = processEngineConfiguration.getHistoryLevel();// 获取事务上下文工厂this.transactionContextFactory = processEngineConfiguration.getTransactionContextFactory();// 执行数据库模式变更操作executeSchemaOperations();// 注册过程引擎if (name == null) {LOG.processEngineCreated(ProcessEngines.NAME_DEFAULT);} else {LOG.processEngineCreated(name);}ProcessEngines.registerProcessEngine(this);// 注册过程引擎到作业执行器if (jobExecutor != null) {jobExecutor.registerProcessEngine(this);}// 如果启用了指标采集,则注册数据库指标报告器if (processEngineConfiguration.isMetricsEnabled()) {String reporterId;// 仅当没有静态主机名且未设置自定义主机名提供程序时,使用已弃用的自定义MetricsReporterIdProvider// 见ProcessEngineConfigurationImpl#initHostname()if (processEngineConfiguration.getMetricsReporterIdProvider() != null&& processEngineConfiguration.getHostnameProvider() instanceof SimpleIpBasedProvider) {reporterId = processEngineConfiguration.getMetricsReporterIdProvider().provideId(this);} else {reporterId = processEngineConfiguration.getHostname();}DbMetricsReporter dbMetricsReporter = processEngineConfiguration.getDbMetricsReporter();dbMetricsReporter.setReporterId(reporterId);// 启动数据库指标报告器if (processEngineConfiguration.isDbMetricsReporterActivate()) {dbMetricsReporter.start();}}}
调用构建后处理方法 invokePostProcessEngineBuild
//ProcessEngineConfigurationImpl.invokePostProcessEngineBuildprotected void invokePostProcessEngineBuild(ProcessEngine engine) {for (ProcessEnginePlugin plugin : processEnginePlugins) {plugin.postProcessEngineBuild(engine);}}
流程引擎拥有如下插件类