Activiti5工作主要流主要源码解析

本文详细解析了Activiti流程引擎的初始化过程,包括ProcessEngineConfiguration的实现类ProcessEngineConfigurationImpl中的关键初始化方法。这些方法涵盖了历史级别设置、表达式管理器、变量类型管理、表单引擎等组件的初始化细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从整体结构来看ProcessEngineConfiguration很关键,其实现类为org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl,其中其初始化的方法为:
protectedvoidinit(){
initHistoryLevel();
initExpressionManager();
initVariableTypes();
initFormEngines();
initFormTypes();
initScriptingEngines();
initBusinessCalendarManager();
initCommandContextFactory();
initTransactionContextFactory();
initCommandExecutors();
initServices();
initIdGenerator();
initDeployers();
initJobExecutor();
initDataSource();
initTransactionFactory();
initSqlSessionFactory();
initSessionFactories();
initJpa();
}
上面的各个初始化语句的作用如下:
1、initHistoryLevel():初始化HistoryLevel,主要内容如下:
publicvoidinitHistoryLevel(){
if(HISTORY_NONE.equalsIgnoreCase(history)){
historyLevel=0;
}elseif(HISTORY_ACTIVITY.equalsIgnoreCase(history)){
historyLevel=1;
}elseif(HISTORY_AUDIT.equalsIgnoreCase(history)){
historyLevel=2;
}elseif(HISTORY_FULL.equalsIgnoreCase(history)){
historyLevel=3;
}else{
thrownewActivitiException("invalidhistorylevel:"+history);
}
}
2、initExpressionManager():初始化ExpressionManager,主要内容如下:
protectedvoidinitExpressionManager(){
if(expressionManager==null){
expressionManager=newExpressionManager();
}
}
3、initVariableTypes():初始化VariableTypes,主要内容如下:
protectedvoidinitVariableTypes(){
if(variableTypes==null){
variableTypes=newDefaultVariableTypes();
if(customPreVariableTypes!=null){
for(VariableTypecustomVariableType:customPreVariableTypes){
variableTypes.addType(customVariableType);
}
}
variableTypes.addType(newNullType());
variableTypes.addType(newStringType());
variableTypes.addType(newBooleanType());
variableTypes.addType(newShortType());
variableTypes.addType(newIntegerType());
variableTypes.addType(newLongType());
variableTypes.addType(newDateType());
variableTypes.addType(newDoubleType());
variableTypes.addType(newByteArrayType());
variableTypes.addType(newSerializableType());
variableTypes.addType(newCustomObjectType("item",ItemInstance.class));
variableTypes.addType(newCustomObjectType("message",MessageInstance.class));
if(customPostVariableTypes!=null){
for(VariableTypecustomVariableType:customPostVariableTypes){
variableTypes.addType(customVariableType);
}
}
}
}
4、initFormEngines():初始化FormEngines,主要内容如下:
protectedvoidinitFormEngines(){
if(formEngines==null){
formEngines=newHashMap();
FormEnginedefaultFormEngine=newJuelFormEngine();
formEngines.put(null,defaultFormEngine);//defaultformengineislookedupwithnull
formEngines.put(defaultFormEngine.getName(),defaultFormEngine);
}
if(customFormEngines!=null){
for(FormEngineformEngine:customFormEngines){
formEngines.put(formEngine.getName(),formEngine);
}
}
}
5、initFormTypes():
6、initScriptingEngines():
7、initBusinessCalendarManager():
8、initCommandContextFactory():初始化CommandContextFactory,主要内容如下:
protectedvoidinitCommandContextFactory(){
if(commandContextFactory==null){
commandContextFactory=newCommandContextFactory();
commandContextFactory.setProcessEngineConfiguration(this);
}
}
9、initTransactionContextFactory():
10、initCommandExecutors():初始化CommandExecutors,主要内容如下:
protectedvoidinitCommandExecutors(){
initCommandInterceptorsTxRequired();
initCommandExecutorTxRequired();
initCommandInterceptorsTxRequiresNew();
initCommandExecutorTxRequiresNew();
}
注意:从上面的四个方法的实现(源代码不在这里贴出)可以看出,“命令执行拦截器”可以增加自定义的拦截器,分别可以指定成预处理和后处理两部分,具体的变量为customPreCommandInterceptorsTxRequired、customPostCommandInterceptorsTxRequired、customPreCommandInterceptorsTxRequiresNew、customPostCommandInterceptorsTxRequiresNew。这些都是集合类型的变量,另外“命令执行拦截器”之所以要区分为“TxRequired”和“TxRequiresNew”还不是很清楚,从目前源代码中可以看出initIdGenerator()初始化的方法中采用了commandExecutorTxRequiresNew的“命令执行拦截器”的集合。拦截器的类结构图如下:


11、initServices():初始化工作流引擎的各个Service,主要内容如下:
protectedvoidinitServices(){
initService(repositoryService);
initService(runtimeService);
initService(historyService);
initService(identityService);
initService(taskService);
initService(formService);
initService(managementService);
}
protectedvoidinitService(Objectservice){
if(serviceinstanceofServiceImpl){
((ServiceImpl)service).setCommandExecutor(commandExecutorTxRequired);
}
}

注意:此处初始化只是为各个Service设置“命令执行拦截器”,并且该“命令执行拦截器”已经在initCommandExecutors()方法中初始化过了,另外从源代码中可以看出,每个Service的“命令执行者”都是由“LogInterceptor-->CommandContextInterceptor-->CommandExecutorImpl”组成的执行链状结构。注意,其中CommandContextInterceptor方法中对数据库操作的作用较大,要多关注。
12、initIdGenerator():初始化自增长序列生成器。
13、initDeployers():初始化部署者,具体代码如下:
protectedvoidinitDeployers(){
if(deployers==null){
deployers=newArrayList();
if(customPreDeployers!=null){
deployers.addAll(customPreDeployers);
}
deployers.addAll(getDefaultDeployers());
if(customPostDeployers!=null){
deployers.addAll(customPostDeployers);
}
}
}注意:从代码中可以看到部署者分为自定义的预部署者和后部署者,另外从代码里可以按到默认的部署者是BpmnDeployer。至于自定义部署者的用途还需要继续分析。
14、initJobExecutor():初始化定时任务执行器
15、initDataSource():初始化数据源
16、initTransactionFactory():初始化事务工厂
17、initSqlSessionFactory():初始化sqlSessionFactory,主要是初始化iBatis的配置等相关信息,代码如下:
protectedvoidinitSqlSessionFactory(){
if(sqlSessionFactory==null){
InputStreaminputStream=null;
try{
inputStream=ReflectUtil.getResourceAsStream("org/activiti/db/ibatis/activiti.ibatis.mem.conf.xml");
//updatethejdbcparameterstotheconfiguredones...
Environmentenvironment=newEnvironment("default",transactionFactory,dataSource);
Readerreader=newInputStreamReader(inputStream);
XMLConfigBuilderparser=newXMLConfigBuilder(reader);
Configurationconfiguration=parser.getConfiguration();
configuration.setEnvironment(environment);
configuration.getTypeHandlerRegistry().register(VariableType.class,JdbcType.VARCHAR,newIbatisVariableTypeHandler());
configuration=parser.parse();
sqlSessionFactory=newDefaultSqlSessionFactory(configuration);
}catch(Exceptione){
thrownewActivitiException("ErrorwhilebuildingibatisSqlSessionFactory:"+e.getMessage(),e);
}finally{
IoUtil.closeSilently(inputStream);
}
}
}

18、initSessionFactories():初始化各个主要Service的SessionFactory
19、initJpa():初始化JPA
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值