创建一个activiti的小应用,大概分为4步。
public void testProcess throws ParseException {
// 1. 创建流程引擎
ProcessEngine processEngine = getProcessEngine();
// 2. 部署流程定义文件
ProcessDefinition processDefinition = getProcessDefinition(processEngine);
// 3. 启动运行程序
ProcessInstance processInstance = getProcessInstance(processEngine, processDefinition);
// 4. 处理流程任务
processTask(processEngine, processInstance);
}
- 创建流程引擎
private ProcessEngine getProcessEngine() {
ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
ProcessEngine processEngine = cfg.buildProcessEngine();
return processEngine;
}
- 部署流程定义文件
private ProcessDefinition getProcessDefinition(ProcessEngine processEngine) {
RepositoryService repositoryService = processEngine.getRepositoryService();
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
deploymentBuilder.addClasspathResource("process.bpmn20.xml");
Deployment deployment = deploymentBuilder.deploy();
String deploymentId = deployment.getId();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().
deploymentId(deploymentId).
singleResult();
return processDefinition;
}
- 启动运行程序
private ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
return processInstance;
}
- 处理流程任务
private void processTask(ProcessEngine processEngine, ProcessInstance processInstance) throws ParseException {
Scanner scanner = new Scanner(System.in);
while (processInstance != null && !processInstance.isEnded()) {
TaskService taskService = processEngine.getTaskService();
List<Task> tasks = taskService.createTaskQuery().list();
for (Task task : tasks) {
Map<String, Object> variables = getVariables(processEngine, scanner, task);
taskService.complete(task.getId(), variables);
processInstance = processEngine.getRuntimeService()
.createProcessInstanceQuery()
.processInstanceId(processInstance.getId())
.singleResult();
}
}
scanner.close();
}
生产中参数一般从数据库或配置文件中读取,这里是简单的demo,所以直接从控制台输入。
private Map<String, Object> getVariables(ProcessEngine processEngine, Scanner scanner, Task task) throws ParseException {
FormService formService = processEngine.getFormService();
TaskFormData taskFormData = formService.getTaskFormData(task.getId());
List<FormProperty> formProperties = taskFormData.getFormProperties();
Map<String, Object> variables = Maps.newHashMap();
for (FormProperty formProperty: formProperties) {
String line = null;
if (StringFormType.class.isInstance(formProperty.getType())) {
LOGGER.info("please enter {}", formProperty.getName());
line = scanner.nextLine();
variables.put(formProperty.getId(), line);
} else if (DateFormType.class.isInstance(formProperty.getType())) {
LOGGER.info("please enter {},format(yyyy-MM-dd)", formProperty.getName());
line = scanner.nextLine();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(line);
variables.put(formProperty.getId(), date);
} else {
LOGGER.info("This type: {} is not supportted.", formProperty.getType());
}
}
return variables;
}