activiti系列之ProcessEngines

ProcessEngines 是一个创建流程引擎与关闭流程引擎的工具类,所有创建的ProcessEngine实例均被注册到ProcessEngines中。

ProcessEngines

 protected static Map processEngines = new HashMap();

通过map集合,使用键值对的形式来保存ProcessEngine对象

初始化方法,用来实例化ProcessEngine对象

public static synchronized void init() {
    if (!isInitialized()) {
        if (processEngines == null) {
            processEngines = new HashMap();
        }
        ClassLoader classLoader = ReflectUtil.getClassLoader();
        Enumeration resources = null;
        try {
            resources = classLoader.getResources("activiti.cfg.xml");
        } catch (IOException var6) {
            throw new ActivitiIllegalArgumentException("problem retrieving activiti.cfg.xml resources on the classpath: " + System.getProperty("java.class.path"), var6);
        }
        HashSet configUrls = new HashSet();
        while(resources.hasMoreElements()) {
            configUrls.add(resources.nextElement());
        }
        Iterator iterator = configUrls.iterator();
        while(iterator.hasNext()) {
            URL resource = (URL)iterator.next();
            log.info("Initializing process engine using configuration '{}'", resource.toString());
            initProcessEngineFromResource(resource);
        }
        try {
            resources = classLoader.getResources("activiti-context.xml");
        } catch (IOException var5) {
            throw new ActivitiIllegalArgumentException("problem retrieving activiti-context.xml resources on the classpath: " + System.getProperty("java.class.path"), var5);
        }
        while(resources.hasMoreElements()) {
            URL resource = (URL)resources.nextElement();
            log.info("Initializing process engine using Spring configuration '{}'", resource.toString());
            initProcessEngineFromSpringResource(resource);
        }
        setInitialized(true);
    } else {
        log.info("Process engines already initialized");
    }
}

 

 

 把实例化好的ProcessEngine对象,加入到Map集合中

public static void registerProcessEngine(ProcessEngine processEngine) {
    processEngines.put(processEngine.getName(), processEngine);
}

 把实例化好的ProcessEngine对象,从map集合中去除

 public static void unregister(ProcessEngine processEngine) {
    processEngines.remove(processEngine.getName());
}

 

 

 public static ProcessEngine getDefaultProcessEngine();

 从map集合中取出默认的实例化好的ProcessEngine对象

public static ProcessEngine getDefaultProcessEngine() {
    return getProcessEngine("default");
}

public static ProcessEngine getProcessEngine(String processEngineName) {
    if (!isInitialized()) {
        init();
    }
    return (ProcessEngine)processEngines.get(processEngineName);
}

 

public static ProcessEngineInfo retry(String resourceUrl);

 如果Activiti在加载配置文件时出现异常的话,通过改方法可以重新加载配置文件

public static ProcessEngineInfo retry(String resourceUrl) {
    try {
        return initProcessEngineFromResource(new URL(resourceUrl));
    } catch (MalformedURLException var2) {
        throw new ActivitiIllegalArgumentException("invalid url: " + resourceUrl, var2);
    }
}

private static ProcessEngineInfo initProcessEngineFromResource(URL resourceUrl) {
    ProcessEngineInfo processEngineInfo = (ProcessEngineInfo)processEngineInfosByResourceUrl.get(resourceUrl.toString());
    String resourceUrlString;
    if (processEngineInfo != null) {
        processEngineInfos.remove(processEngineInfo);
        if (processEngineInfo.getException() == null) {
            resourceUrlString = processEngineInfo.getName();
            processEngines.remove(resourceUrlString);
            processEngineInfosByName.remove(resourceUrlString);
        }
   processEngineInfosByResourceUrl.remove(processEngineInfo.getResourceUrl());
    }
    resourceUrlString = resourceUrl.toString();
    ProcessEngineInfoImpl processEngineInfo;
    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, (String)null);
        processEngines.put(processEngineName, processEngine);
        processEngineInfosByName.put(processEngineName, processEngineInfo);
    } catch (Throwable var5) {
        log.error("Exception while initializing process engine: {}", var5.getMessage(), var5);
        processEngineInfo = new ProcessEngineInfoImpl((String)null, resourceUrlString, getExceptionString(var5));
    }
    processEngineInfosByResourceUrl.put(resourceUrlString, processEngineInfo);
    processEngineInfos.add(processEngineInfo);
    return processEngineInfo;
}

 

public synchronized static void destroy();

  对维护的所有的ProcessEngine实例进行销毁,并且在销毁的同时调用ProcessEngine的close方法

public static synchronized void destroy() {
    if (isInitialized()) {
        Map engines = new HashMap(processEngines);
        processEngines = new HashMap();
        Iterator var1 = engines.keySet().iterator();
        while(var1.hasNext()) {
            String processEngineName = (String)var1.next();
            ProcessEngine processEngine = (ProcessEngine)engines.get(processEngineName);
            try {
                processEngine.close();
            } catch (Exception var5) {
                log.error("exception while closing {}", processEngineName == null ? "the default process engine" : "process engine " + processEngineName, var5);
            }
        }
        processEngineInfosByName.clear();
        processEngineInfosByResourceUrl.clear();
        processEngineInfos.clear();
        setInitialized(false);
    }
}

 

 

ProcessEngine

一个ProcessEngine实例代表一个流程引擎,从ProcessEngine可以获取各种服务组件,依据这些服务组件可以操作流程实例、任务、系统角色等数据。

ProcessEngine processEngine;
//流程存储服务 提供一系列管理流程定义和流程部署的API
RepositoryService repositoryService = processEngine.getRepositoryService();
//任务 对流程任务进行管理 包括任务提醒 完成和创建
TaskService taskService = processEngine.getTaskService();
//运行服务组件 在流程运行时对流程实例进行管理与控制
RuntimeService runtimeService = processEngine.getRuntimeService();
//表单 
FormService formService = processEngine.getFormService();
//历史服务组件 对流程历史数据进行操作,包括查询 删除
HistoryService historyService = processEngine.getHistoryService();
//身份服务组件 提供对流程角色数据进行管理的API(用户 用户组 及它们之间的关系)
IdentityService identityService = processEngine.getIdentityService();
//管理服务组件  提供对流程引擎进行管理和维护服务
ManagementService managementService = processEngine.getManagementService();
//动态修改流程中的一些参数信息等,是引擎中的一个辅助的服务 ,使用该服务,可以不需要重新部署流程模型,就可以实现对流程模型的
//部分修改
DynamicBpmnService dynamicBpmnService = processEngine.getDynamicBpmnService();
设置流程引擎名称,ProcessEngine默认设置名称为"default"
 ProcessEngineConfiguration pconfig = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("test.xml");
//设置流程引擎名称
 pconfig.setProcessEngineName("ProcessEngineName");
 ProcessEngine processEngine = pconfig.buildProcessEngine();
 //依据名称查询流程引擎
 ProcessEngine processEngineName = ProcessEngines.getProcessEngine("ProcessEngineName");

  关闭流程引擎

processEngine.close();

具体实现方法:

public void close() {
    ProcessEngines.unregister(this);
    if (this.asyncExecutor != null && this.asyncExecutor.isActive()) {
        this.asyncExecutor.shutdown();
    }

    this.commandExecutor.execute(this.processEngineConfiguration.getSchemaCommandConfig(), new SchemaOperationProcessEngineClose());
    if (this.processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
        this.processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineClosed(this);
    }

    this.processEngineConfiguration.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createGlobalEvent(ActivitiEventType.ENGINE_CLOSED));
}

 

你可能感兴趣的:(Activiti)