activiti学习笔记(四) 配置器

activiti配置器

activiti配置器的作用

       一般在构建流程实例的时候, 会进行初始化,配置器就是可以在获取了流程配置实例,以及初始化之后可以进行一些额外的配置

activiti配置器的机制

allConfigurators集合添加

       在ProcessEngineConfigurationImpl这个类中调用init方法中initConfigurators方法,将所有流程配置器添加到allConfigurators这个集合,流程配置器有两种获取形式,第一种是直接通过bean方式注入到configurators遍历添加到allConfigurators集合,第二种是通过调用spi模式,加载到allConfigurators集合。最后将所有的按照照priority的大小进行排序

public void initConfigurators() {


    //一个list集合,存放所有ProcessEngineConfigurator
    allConfigurators = new ArrayList<ProcessEngineConfigurator>();

    //遍历Configurators将其添加到allConfigurators这个集合
    if (configurators != null) {
      for (ProcessEngineConfigurator configurator : configurators) {
        allConfigurators.add(configurator);
      }
    }

    //获取当前类加载器
    if (enableConfiguratorServiceLoader) {
      ClassLoader classLoader = getClassLoader();
      if (classLoader == null) {
        classLoader = ReflectUtil.getClassLoader();
      }

      //利用spi模式加载ProcessEngineConfigurator所有实现类 将其添加到allConfigurators集合
      ServiceLoader<ProcessEngineConfigurator> configuratorServiceLoader = ServiceLoader.load(ProcessEngineConfigurator.class, classLoader);
      int nrOfServiceLoadedConfigurators = 0;
      for (ProcessEngineConfigurator configurator : configuratorServiceLoader) {
        allConfigurators.add(configurator);
        nrOfServiceLoadedConfigurators++;
      }

      if (nrOfServiceLoadedConfigurators > 0) {
        log.info("Found {} auto-discoverable Process Engine Configurator{}", nrOfServiceLoadedConfigurators++, nrOfServiceLoadedConfigurators > 1 ? "s" : "");
      }

      //将allConfigurators进行排序,按照priority1的从大到小
      if (!allConfigurators.isEmpty()) {
        Collections.sort(allConfigurators, new Comparator<ProcessEngineConfigurator>() {
          @Override
          public int compare(ProcessEngineConfigurator configurator1, ProcessEngineConfigurator configurator2) {
            int priority1 = configurator1.getPriority();
            int priority2 = configurator2.getPriority();

            if (priority1 < priority2) {
              return -1;
            } else if (priority1 > priority2) {
              return 1;
            }
            return 0;
          }
        });

        // Execute the configurators
        log.info("Found {} Process Engine Configurators in total:", allConfigurators.size());
        for (ProcessEngineConfigurator configurator : allConfigurators) {
          log.info("{} (priority:{})", configurator.getClass(), configurator.getPriority());
        }

      }

    }
  }

初始化调用方法

在ProcessEngineConfigurationImpl调用init方法中configuratorsBeforeInit方法,它会遍历所有的allConfigurators,并调用其中的beforeInit

  /**调用所有配置器的beforeInit方法*/
  public void configuratorsBeforeInit() {
    for (ProcessEngineConfigurator configurator : allConfigurators) {
      log.info("Executing beforeInit() of {} (priority:{})", configurator.getClass(), configurator.getPriority());
      configurator.beforeInit(this);
    }
  }

初始化后配置方法调用

在ProcessEngineConfigurationImpl调用init方法中configuratorsAfterInit方法,它会遍历所有的allConfigurators,并调用其中的configure

 /**调用所有配置的configure方法*/
  public void configuratorsAfterInit() {
    for (ProcessEngineConfigurator configurator : allConfigurators) {
      log.info("Executing configure() of {} (priority:{})", configurator.getClass(), configurator.getPriority());
      configurator.configure(this);
    }
  }

activiti配置器的接口类

public interface ProcessEngineConfigurator {

  /**init方法的第二个方法调用,第一个是获取所有ProcessEngineConfigurator(当前接口的实例) 在初始化前做一些操作*/
  void beforeInit(ProcessEngineConfigurationImpl processEngineConfiguration);

  /**init方法的最后一个调用,在初始化后可以做一些配置*/
  void configure(ProcessEngineConfigurationImpl processEngineConfiguration);

  /**使用实例时,首先按照priority进行裴谞,如果你有实例之间的依赖性,自己定义优先级序*/
  int getPriority();

}

activiti的bean方式配置文件

<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="processEngineConfigurator1" class="com.activiti_study.ch7.processEngineConfigurator1"></bean>
	<bean id="processEngineConfigurator2"  class="com.activiti_study.ch7.processEngineConfigurator2"></bean>

	<bean id="processEngineConfiguration"
		class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
		<property name="dataSource" ref="dataSource" />
		<property name="processEngineName" value="shareniu" />
		<property name="configurators">
		
		<list>
		  <ref bean="processEngineConfigurator1"/>
		  <ref bean="processEngineConfigurator2"/>
		</list>
		</property>
	</bean>
</beans>

activiti的实现类

public class processEngineConfigurator1 extends AbstractProcessEngineConfigurator{
	@Override
	public void beforeInit(
			ProcessEngineConfigurationImpl processEngineConfiguration) {
		System.out.println("1:beforeInit ");
		processEngineConfiguration.setDatabaseSchemaUpdate("true");
	}
	@Override
	public void configure(
			ProcessEngineConfigurationImpl processEngineConfiguration) {
		System.out.println("1:configure ");
		String databaseSchemaUpdate = processEngineConfiguration.getDatabaseSchemaUpdate();
		System.out.println(databaseSchemaUpdate+"-----------------");
	}
}
public class processEngineConfigurator2 extends AbstractProcessEngineConfigurator{
	@Override
	public void beforeInit(
			ProcessEngineConfigurationImpl processEngineConfiguration) {
		System.out.println("2:beforeInit ");
		processEngineConfiguration.setDatabaseSchemaUpdate("true");
	}
	@Override
	public void configure(
			ProcessEngineConfigurationImpl processEngineConfiguration) {
		System.out.println("2:configure ");
		String databaseSchemaUpdate = processEngineConfiguration.getDatabaseSchemaUpdate();
		System.out.println(databaseSchemaUpdate+"-----------------");
	}
}

activit配置器的测试代码

public class App {
	@Test
	public void getProcessEngine() {
		ProcessEngineConfiguration pec = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("com/activiti_study/ch7/activiti1.cfg.xml");
		ProcessEngine pe1 = pec.buildProcessEngine();
	}
}

activiti配置器的测试结果

1:beforeInit 
2:beforeInit 
1:configure 
true-----------------
2:configure 
true-----------------

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

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

3195
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

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

cocos2dx 打灰机

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

5566
来自专栏陈仁松博客

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

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

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

Spring Reactor 项目核心库Reactor Core

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

2192
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

2717
来自专栏跟着阿笨一起玩NET

c#实现打印功能

2812
来自专栏魂祭心

原 canvas绘制clock

4164
来自专栏张善友的专栏

LINQ via C# 系列文章

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

2665
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

3879

扫码关注云+社区