一個Activiti的小demo

創建一個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);
}
  1. 創建流程引擎
private ProcessEngine getProcessEngine() {
	ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
	ProcessEngine processEngine = cfg.buildProcessEngine();
	return processEngine;
}
  1. 部署流程定義文件
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;
}
  1. 啓動運行程序
private ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {
	RuntimeService runtimeService = processEngine.getRuntimeService();
	ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
	return processInstance;
}
  1. 處理流程任務
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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章