activiti工作流

activiti源码分析之流程引擎监听器

2019-03-26  本文已影响5人  我有一只喵喵

activiti源码分析之流程引擎监听器

一、流程引擎监听器是什么?何时触发?

在ProcessEngineImpl被实例化的同时调用以下一段代码

  if (processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
            processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineBuilt(this);
        }

并且ProcessEngineImpl的close方法中以下片段代码

    if (this.processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
        this.processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineClosed(this);
    }

通过以上源码可知关于流程引擎监听器以下几点

1、流程引擎监听器是由ProcessEngineConfiguration(流程引擎配置类)进行管理

2、查看getProcessEngineLifecycleListener()实现

public ProcessEngineLifecycleListener getProcessEngineLifecycleListener() {
         return this.processEngineLifecycleListener;
     }
public interface ProcessEngineLifecycleListener {
    void onProcessEngineBuilt(ProcessEngine var1);

    void onProcessEngineClosed(ProcessEngine var1);
}

从以上代码可知监听器只需要实现ProcessEngineLifecycleListener接口并且实现onProcessEngineBuilt,onProcessEngineClosed方法即可。

3、流程引擎监听器在ProcessEngineImpl被实例化时执行构造器时触发onProcessEngineBuilt,在close方法中触发onProcessEngineClosed

二、流程引擎监听器实战

1、实现ProcessEngineLifecycleListener接口
public class MyProcessEngineListener implements ProcessEngineLifecycleListener {
    @Override
    public void onProcessEngineBuilt(ProcessEngine processEngine) {
        System.out.println("流程引擎创建");
    }

    @Override
    public void onProcessEngineClosed(ProcessEngine processEngine) {
        System.out.println("流程引擎销毁");
    }
}
2、将流程引擎监听器交给ProcessEngineConfigurantion管理

activiti.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">

        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_activiti" />
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="1234" />
        <!--配置流程引擎监听器-->
        <property name="processEngineLifecycleListener">
            <bean class="cn.cf.activiti.shareniu.MyProcessEngineListener"/>
        </property>
        <property name="databaseSchemaUpdate" value="true" />
    </bean>

</beans>
上一篇 下一篇

猜你喜欢

热点阅读