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

activiti监听生命周期

activiti生命周期的作用

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

activiti的接口类

public interface ProcessEngineLifecycleListener {

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

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

activiti的生命周期的实现

       首先通过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));
  }

activiti生命周期的使用

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

第一步:实现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方法

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

32710
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3265
来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

4878
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2675
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

5676
来自专栏魂祭心

原 canvas绘制clock

4294
来自专栏落花落雨不落叶

canvas画简单电路图

65611
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2627
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2232
来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

3038

扫码关注云+社区