前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >activiti学习笔记(三) 监听生命周期

activiti学习笔记(三) 监听生命周期

作者头像
cfs
发布2018-03-08 15:44:48
1.2K0
发布2018-03-08 15:44:48
举报
文章被收录于专栏:编码小白

activiti监听生命周期

activiti生命周期的作用

生命周期的作用是在构建processEngine实例时候或者关闭的时候调用对应处理方法,比如我在启动实例的时候,可以对实例做一些预处理

activiti的接口类

代码语言:javascript
复制
public interface ProcessEngineLifecycleListener {

  /**实体引擎实例化的时候调用该方法*/
  void onProcessEngineBuilt(ProcessEngine processEngine);

  /**实体引擎关闭的时候调用该方法*/
  void onProcessEngineClosed(ProcessEngine processEngine);
}

activiti的生命周期的实现

       首先通过bean进行实例化,再注入到当前bean,具体监听则是在构建实例和关闭实例的时候

   构建实例:会调用监听类的onProcessEngineBuilt方法

代码语言:javascript
复制
  /**
   * @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方法

代码语言:javascript
复制
 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));
  }

activiti生命周期的使用

上面是生命周期的源码实现介绍,下面就讲述一下怎样使用

第一步:实现ProcessEngineLifecycleListener这个接口

代码语言:javascript
复制
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里面

代码语言:javascript
复制
<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>

第三步:具体实现

代码语言:javascript
复制
        @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+"========");
	}
代码语言:javascript
复制
流程引擎生命周期监听器开始了....
Arial
org.activiti.engine.impl.ProcessEngineImpl@4b29d1d2========

      从显示结果可以看出在调用buildProcessEngine方法的时候调用了ProcessEngineLifecycleListener继承类的onProcessEngineBuilt方法

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年08月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • activiti监听生命周期
    • activiti生命周期的作用
      • activiti的接口类
        • activiti的生命周期的实现
          • activiti生命周期的使用
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档