以获取默认的流程实例为例,来解释整个流程实例的获取过程
文件位置:/org/activiti/engine/ProcessEngines.java
第一步:通过getDefaultProcessEngine来获取,可以看出其调用的当前类的getProcessEngine方法。
//获取默认的流程引擎实例 NAME_DEFAULT="default"
public static ProcessEngine getDefaultProcessEngine() {
return getProcessEngine(NAME_DEFAULT);
}
第二步: 根据ProcessEngine的name获取指定的ProcessEngine
/**
* @author 郑小康
* 根据ProcessEngine的name获取指定的ProcessEngine
* */
public static ProcessEngine getProcessEngine(String processEngineName) {
if (!isInitialized()) {
init();
}
return processEngines.get(processEngineName);
}
第三步:调用init方法,来对流程引擎进行初始化
/**
* @author 郑小康
*
* 该方法使用了synchronized 再次检测其是否被初始化,避免其它客户端进行了初始化
*
* 1.检验是否初始化,默认值为false,第一次进来肯定是false,然后向下进行执行
*
* 2.检验processEngines是否存在,如果不存在则构建一个HashMap实例
*
* 3.获取当前类加载器,根据类加载器获取activiti.cfg.xml资源文件,如果不存在则扔出异常,要求必须命名为activiti.cfg.xml
*
* 4.遍历所有的资源文件,通过initProcessEngineFromSpringResource这个方法构建其processEngine实例并添加其对应的属性
*
* 5.将初始化属性isInitialized的值设为true
* */
public synchronized static void init() {
if (!isInitialized()) {
if (processEngines == null) {
processEngines = new HashMap<String, ProcessEngine>();
}
ClassLoader classLoader = ReflectUtil.getClassLoader();
Enumeration<URL> resources = null;
try {
resources = classLoader.getResources("activiti.cfg.xml");
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("problem retrieving activiti.cfg.xml resources on the classpath: " + System.getProperty("java.class.path"), e);
}
Set<URL> configUrls = new HashSet<URL>();
while (resources.hasMoreElements()) {
configUrls.add(resources.nextElement());
}
for (Iterator<URL> iterator = configUrls.iterator(); iterator.hasNext();) {
URL resource = iterator.next();
log.info("Initializing process engine using configuration '{}'", resource.toString());
initProcessEngineFromResource(resource);
}
try {
resources = classLoader.getResources("activiti-context.xml");
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("problem retrieving activiti-context.xml resources on the classpath: " + System.getProperty("java.class.path"), e);
}
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
log.info("Initializing process engine using Spring configuration '{}'", resource.toString());
initProcessEngineFromSpringResource(resource);
}
setInitialized(true);
} else {
log.info("Process engines already initialized");
}
}
第四步:通过initProcessEngineFromResource方法来进行ProcessEngine实例以及其一系列相关属性的创建,核心是调用了buildProcessEngine来构建其实例
/**
* @author 郑小康
*
* 1.从processEngineInfosByResourceUrl中获取流程配置信息
*
* 2.如果processEngineInfo不为空,移除所有与ProcessEngine相关的信息
*
* 3.构建ProcessEngine实例,将其添加到processEngines
*
* 4.构建processEngineInfo实例,将其加入到processEngineInfosByName processEngineInfosByResourceUrl processEngineInfos.add(processEngineInfo);
*
* */
private static ProcessEngineInfo initProcessEngineFromResource(URL resourceUrl) {
ProcessEngineInfo processEngineInfo = processEngineInfosByResourceUrl.get(resourceUrl.toString());
// if there is an existing process engine info
if (processEngineInfo != null) {
// remove that process engine from the member fields
processEngineInfos.remove(processEngineInfo);
if (processEngineInfo.getException() == null) {
String processEngineName = processEngineInfo.getName();
processEngines.remove(processEngineName);
processEngineInfosByName.remove(processEngineName);
}
processEngineInfosByResourceUrl.remove(processEngineInfo.getResourceUrl());
}
String resourceUrlString = resourceUrl.toString();
try {
log.info("initializing process engine for resource {}", resourceUrl);
ProcessEngine processEngine = buildProcessEngine(resourceUrl);
String processEngineName = processEngine.getName();
log.info("initialised process engine {}", processEngineName);
processEngineInfo = new ProcessEngineInfoImpl(processEngineName, resourceUrlString, null);
processEngines.put(processEngineName, processEngine);
processEngineInfosByName.put(processEngineName, processEngineInfo);
} catch (Throwable e) {
log.error("Exception while initializing process engine: {}", e.getMessage(), e);
processEngineInfo = new ProcessEngineInfoImpl(null, resourceUrlString, getExceptionString(e));
}
processEngineInfosByResourceUrl.put(resourceUrlString, processEngineInfo);
processEngineInfos.add(processEngineInfo);
return processEngineInfo;
}
第五步:buildProcessEngine方法来构建实例,先获取配置实例,在调用其的buildProcessEngine方法来实例化,具体实现类是ProcessEngineConfigurationImpl
/**
* @author 郑小康
* 根据文件路径,构建其ProcessEngineConfiguration实例,调用buildProcessEngine方法创建ProcessEngine实例
* */
private static ProcessEngine buildProcessEngine(URL resource) {
InputStream inputStream = null;
try {
inputStream = resource.openStream();
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream);
return processEngineConfiguration.buildProcessEngine();
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("couldn't open resource stream: " + e.getMessage(), e);
} finally {
IoUtil.closeSilently(inputStream);
}
}
以上就是一个流程实例获取的过程,其实不难发现,我们不一定要获取默认的,我们也可以根据指定的processEngineName利用getProcessEngine来获取对应的流程实例,结合上文我们还能自己构建流程配置实例,直接进行获取
配置文件
<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-study?useUnicode=true&characterEncoding=UTF-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password" value="fadp" />
</bean>
<bean id="processEngine1"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>
获取的方法
@Test
public void getDefaultProcessEngine() {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println(processEngine);
}
但是这样就必须按照其要求的文件目录结构,我很不喜欢这样,我想自己定制,所以我实现的方式是,构建流程配置实例再创建流程实例
private final String cfgXml = System.getProperty("user.dir") + "/resource/activiti.cfg.xml";
@Test
public void createProcessEngineConfigurationFromResource() throws IOException {
InputStream in = new FileInputStream(cfgXml);
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(in,"processEngine1");
System.out.println(processEngineConfiguration);
ProcessEngine processEngine=processEngineConfiguration.buildProcessEngine();
System.out.println(processEngine);
}