二、Activiti 6.0 简单样例

一、Eclipse 安装Activiti 插件

Eclipse -> Help -> Install New SoftWare-> Add
http://www.activiti.org/designer/update/
二、Activiti 6.0 简单样例_第1张图片

二、创建工作流

二、Activiti 6.0 简单样例_第2张图片
二、Activiti 6.0 简单样例_第3张图片
二、Activiti 6.0 简单样例_第4张图片
二、Activiti 6.0 简单样例_第5张图片

二、创建Maven工程

1、修改pom.xml文件


    org.activiti
    activiti-engine
    6.0.0


    junit
    junit
    4.11
    test


    ch.qos.logback
    logback-classic
    1.1.11


    com.h2database
    h2
    1.3.176

2、启动函数

package com.quincy.activiti;

import org.activiti.engine.*;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.impl.form.DateFormType;
import org.activiti.engine.impl.form.StringFormType;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class MainSystemApplication {

    private static final Logger logger = LoggerFactory.getLogger(MainSystemApplication.class);

    public  static void  main(String [] args) throws ParseException {
        logger.info("开始");
        //创建流程引擎
        ProcessEngine processEngine = getProcessEngine();
        //部署流程引擎
        ProcessDefinition processDefinition = getProcessDefinition(processEngine, "bpmn/hello_word_approve.bpmn20.xml");
        //启动运行流程
        ProcessInstance processInstance = getProcessInstance(processEngine, processDefinition);
        //处理流程任务
        doTask(processEngine, processInstance);
        logger.info("结束");
    }

    private static void doTask(ProcessEngine processEngine, ProcessInstance processInstance) throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (processInstance != null && !processInstance.isEnded()) {
            TaskService taskService = processEngine.getTaskService();
            List list = taskService.createTaskQuery().list();
            for (Task task : list) {
                logger.info("处理任务 [{}]", task.getName());
                Map variables = inputInfo(processEngine, scanner, task);
                taskService.complete(task.getId(),variables);
                processInstance = processEngine.getRuntimeService()
                        .createProcessInstanceQuery()
                        .processInstanceId(processInstance.getId())
                        .singleResult();
            }
        }
        scanner.close();
    }

    private static Map inputInfo(ProcessEngine processEngine, Scanner scanner, Task task) throws ParseException {
        FormService formService = processEngine.getFormService();
        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        List formProperties = taskFormData.getFormProperties();
        Map map = new HashMap();
        for (FormProperty property : formProperties) {
            String input = null;
            if(StringFormType.class.isInstance(property.getType())){
                logger.info("输入【{}】?",property.getName());
                input = scanner.nextLine();
                map.put(property.getId(),input);
            }else if(DateFormType.class.isInstance(property.getType())){
                logger.info("输入【{}】? 格式 (yyyy-MM-dd)",property.getName());
                input = scanner.nextLine();
                SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
                Date date = dateF.parse(input);
                map.put(property.getId(),date);
            }else{
                logger.info("类型错误 {}",property.getType());
            }

        }
        return map;
    }

    private static ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        logger.info("启动流程 [{}]", processInstance.getProcessDefinitionKey());
        return processInstance;
    }


    private static ProcessEngine getProcessEngine() {
        ProcessEngineConfiguration cfg = ProcessEngineConfiguration
                .createStandaloneInMemProcessEngineConfiguration();
        return cfg.buildProcessEngine();
    }

    private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine, String bpmnResource) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        deploymentBuilder.addClasspathResource(bpmnResource);
        Deployment deployment = deploymentBuilder.deploy();
        String deploymentId = deployment.getId();
        return repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();
    }
}

你可能感兴趣的:(Acticiti)