在Activiti中编辑当前流程定义

时间:2013-09-30 18:45:04

标签: activiti

每当我需要对流程定义进行更改时,我必须重新部署流程定义。它似乎创建了流程定义的新版本。

有没有办法可以更新现有的流程定义,而不是一直创建新版本。

如果有新版本的流程定义,旧流程实例会发生什么。

对上述查询的任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:5)

我不认为Activiti API提供了一种在不部署新版本的情况下替换现有流程定义的方法。

部署新版本的流程时,旧版本上的所有现有流程实例都会继续在旧版本上运行。

但是,您可以使用SetProcessDefinitionVersionCmd类来更改流程实例上的流程版本。但它并不“聪明”;它只是更改版本号,并且不会更改任何其他运行时数据,因此如果您在流程定义中进行了不兼容的更改,它可能会破坏流程实例。

答案 1 :(得分:0)

如哑光所说,您可以使用org.activiti.engine.impl.cmd.SetProcessDefinitionVersionCmd

以下代码用于更新最新的流程定义ID。

String PROCESS_DEFINITION_NAME = processdefID; 
DesEncrypter de = new DesEncrypter();
String password = "yourpwd";

ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
 .setJdbcUrl("database.url")
 .setJdbcUsername("database.username").setJdbcPassword("database.password")
 .setJdbcDriver("database.driver")
 .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE);

ProcessEngine processEngine = cfg.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();

ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().latestVersion()
 .processDefinitionKey(PROCESS_DEFINITION_NAME).singleResult();

if (processDefinition == null) {
 System.out.println("Cannot find Process Def ID " + PROCESS_DEFINITION_NAME);
} else {
 System.out.println("Latest Process Definition Version: " + processDefinition.getVersion());
 long counter = runtimeService.createProcessInstanceQuery().active()
  .processDefinitionKey(PROCESS_DEFINITION_NAME).count();
 List < ProcessInstance > processInstanceList = runtimeService.createProcessInstanceQuery().active()
  .processDefinitionKey(PROCESS_DEFINITION_NAME).list();

 System.out.println(counter);

 System.out.println("Total process instances: " + counter);
 System.out.println("Total process instances retrieved: " + processInstanceList.size());

 int index = 1;
 for (ProcessInstance oneInstance: processInstanceList) {
  try {
   CommandConfig commandConfig = new CommandConfig();
   // Execute the command asynchronously to prevent that
   // one error updating a process instance stops the
   // entire batch
   cfg.getAsyncExecutor().getProcessEngineConfiguration().getCommandExecutor()
    .execute(commandConfig.transactionNotSupported(), new SetProcessDefinitionVersionCmd(
     oneInstance.getId(), processDefinition.getVersion()));
   System.out.println(index++ + " - Process instance UPDATED: " + oneInstance.getId() +
    ", previous definition ID: " + oneInstance.getProcessDefinitionVersion());

  } catch (Exception e) {
   System.out.println(index++ + " - Process instance FAILED: " + oneInstance.getId() +
    ", previous definition ID: " + oneInstance.getProcessDefinitionVersion() +
    ",  Cause: " + e.getMessage());
  }
 }

 System.out.println("********* Completed Successfully. *********");
 System.in.read();

}