生命周期的作用是在构建processEngine实例时候或者关闭的时候调用对应处理方法,比如我在启动实例的时候,可以对实例做一些预处理
public interface ProcessEngineLifecycleListener {
/**实体引擎实例化的时候调用该方法*/
void onProcessEngineBuilt(ProcessEngine processEngine);
/**实体引擎关闭的时候调用该方法*/
void onProcessEngineClosed(ProcessEngine processEngine);
}
首先通过bean进行实例化,再注入到当前bean,具体监听则是在构建实例和关闭实例的时候
构建实例:会调用监听类的onProcessEngineBuilt方法
/**
* @author 郑小康
*
* 1.注入流程配置实例的一些变量到当前实例
*
* 2.FIXME:未知
*
* 3.注册当前流程实例
*
* 4.FIXME:未知
*
* 5.调用监听方法,这也是这里需要描述的核心(不是本方法的核心)
* */
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.dynamicBpmnService = processEngineConfiguration.getDynamicBpmnService();
this.asyncExecutor = processEngineConfiguration.getAsyncExecutor();
this.commandExecutor = processEngineConfiguration.getCommandExecutor();
this.sessionFactories = processEngineConfiguration.getSessionFactories();
this.transactionContextFactory = processEngineConfiguration.getTransactionContextFactory();
this.formEngineRepositoryService = processEngineConfiguration.getFormEngineRepositoryService();
this.formEngineFormService = processEngineConfiguration.getFormEngineFormService();
if (processEngineConfiguration.isUsingRelationalDatabase() && processEngineConfiguration.getDatabaseSchemaUpdate() != null) {
commandExecutor.execute(processEngineConfiguration.getSchemaCommandConfig(), new SchemaOperationsProcessEngineBuild());
}
if (name == null) {
log.info("default activiti ProcessEngine created");
} else {
log.info("ProcessEngine {} created", name);
}
ProcessEngines.registerProcessEngine(this);
if (asyncExecutor != null && asyncExecutor.isAutoActivate()) {
asyncExecutor.start();
}
//流程引擎实例化监听方法
if (processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineBuilt(this);
}
processEngineConfiguration.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createGlobalEvent(ActivitiEventType.ENGINE_CREATED));
}
关闭实例会调用onProcessEngineClosed方法
public void close() {
ProcessEngines.unregister(this);
if (asyncExecutor != null && asyncExecutor.isActive()) {
asyncExecutor.shutdown();
}
commandExecutor.execute(processEngineConfiguration.getSchemaCommandConfig(), new SchemaOperationProcessEngineClose());
//流程引擎关闭触发监听方法
if (processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineClosed(this);
}
processEngineConfiguration.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createGlobalEvent(ActivitiEventType.ENGINE_CLOSED));
}
上面是生命周期的源码实现介绍,下面就讲述一下怎样使用
第一步:实现ProcessEngineLifecycleListener这个接口
public class processEngineLifecycleListener implements ProcessEngineLifecycleListener{
public void onProcessEngineBuilt(ProcessEngine processEngine) {
System.out.println("流程引擎生命周期监听器开始了....");
ProcessEngineConfiguration pec = processEngine.getProcessEngineConfiguration();
String activityFontName = pec.getActivityFontName();
System.out.println(activityFontName);
}
public void onProcessEngineClosed(ProcessEngine processEngine) {
}
}
第二步:配置当前processEngineLifecycleListener这个bean并将其注入processEngineConfiguration里面
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&characterEncoding=UTF-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password" value="fadp" />
</bean>
<bean id="processEngineLifecycleListener"
class="com.activiti_study.ch6.processEngineLifecycleListener"></bean>
<bean id="processEngine1" name="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="databaseSchemaUpdate" value="true" />
<property name="processEngineLifecycleListener" ref="processEngineLifecycleListener"></property>
</bean>
</beans>
第三步:具体实现
@Test
public void getDefaultProcessEngine() throws FileNotFoundException {
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("com/activiti_study/ch6/activiti1.cfg.xml","processEngineConfiguration");
ProcessEngine pe = processEngineConfiguration.buildProcessEngine();
System.out.println(pe+"========");
}
流程引擎生命周期监听器开始了....
Arial
org.activiti.engine.impl.ProcessEngineImpl@4b29d1d2========
从显示结果可以看出在调用buildProcessEngine方法的时候调用了ProcessEngineLifecycleListener继承类的onProcessEngineBuilt方法