Activiti学习系列一

工作流的概念

解决“使在多个参与者之间按照某种预定义的规则传递文档、信息或人物的过程自动进行,从而实现某个预期的业务目标,或者促使此目标的实现”。

核心API

ProcessEngine

1、在Activiti中最核心的类,其他的类都是由他而来
2、产生方式

  • ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();//配置引擎通过配置文件获取对象
  • ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

3、提供的服务

//流程入的部署 修改 删除的服务 act_ge_bytearray act_re_deployment act_re_model act_re_procdef
RepositoryService repositoryService1 = processEngine.getRepositoryService();
//流程的运行
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
//查询历史纪录
HistoryService historyService = processEngine.getHistoryService();
//页面表单的服务(不常用)
FormService formService = processEngine.getFormService();
//对工作流的用户管理的表的操作
IdentityService identityService = processEngine.getIdentityService();
//管理器
ManagementService managementService = processEngine.getManagementService();
RepositoryService

是Activiti的仓库类。所谓的仓库指流程定义文档的两个文件,bpmn文件和流程图片。

作用
  • 管理流程定义文件xml及静态资源的服务
  • 对特定流程的暂停和激活
  • 流程定义启动权限管理
基础API
  • 部署文件构造器DeploymentBuilder(对资源的封装)
  • 部署文件查询器DeploymentQuery(查询部署的一些操作)
  • 流程定义文件查询对象ProcessDefinitionQuery
  • 流程部署文件对象Deployment
  • 流程定义文件对象ProcessDefinition
  • 流程定义的java格式BpmnModel

1、产生方式

RepositoryService repositoryService1 = processEngine.getRepositoryService();

2、可以产生DeploymentBuilder,用来定义流程部署的相关参数

DeploymentBuilder builder = repositoryService.createDeployment();

3、删除流程定义

repositoryService.deleteDeployment(deploymentId);
RuntimeService

是activiti的流程执行服务类,可以从这个服务类中获取很多关于流程执行相关的信息

ProcessDefinition

流程定义文件类,可以从这里获得资源文件等。

ProcessInstance

流程实例,某一个具体的流程,通过RepositoryService部署某个流程,再通过部署的流程的id获取流程定义文件,最后通过ProcessDefinition的实例的id,使用runtimeService得到ProcessInstance的具体实例。

demo
public class DemoMain {
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoMain.class);

    public static void main(String[] args) throws ParseException {
        System.out.println("启动程序");
        System.out.println();
        //创建流程引擎
//        ProcessEngine processEngine = getProcessEngine();
        ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
        ProcessEngine processEngine = cfg.buildProcessEngine();
        String name = processEngine.getName();
        String version = processEngine.VERSION;

        System.out.printf("流程引擎[%s],版本[%s]",name,version);
        System.out.println();
        //部署流程定义文件
//        ProcessDefinition processDefinition = getProcessDefinition(processEngine);
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder builder = repositoryService.createDeployment();
        builder.addClasspathResource("diagrams/check.bpmn");
        Deployment deploy = builder.deploy();
        String id = deploy.getId();
        //获取流程定义文件
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(id).singleResult();
        System.out.printf("流程定义文件[%s],流程id[%s]",processDefinition.getName(),processDefinition.getId());
        System.out.println();

        //启动运行流程
//        ProcessInstance processInstance = startRuntimeService(processEngine, processDefinition);
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        System.out.printf("启动流程:[%s]",processInstance.getProcessDefinitionKey());
        System.out.println();

        //处理流程任务
        Scanner scanner = new Scanner(System.in);
        while (processInstance!=null && !processInstance.isEnded()) {
            TaskService taskService = processEngine.getTaskService();
            List<Task> list = taskService.createTaskQuery().list();
            System.out.printf("待处理任务数量:[%s]",list.size());
            System.out.println();
            for(Task task : list){
                System.out.printf("待处理任务:[%s]",task.getName());
                System.out.println();
                FormService formService = processEngine.getFormService();
                TaskFormData taskFormData = formService.getTaskFormData(task.getId());
                List<FormProperty> formProperties = taskFormData.getFormProperties();
                Map<String,Object> map = new HashMap<String,Object>();
                for(FormProperty property : formProperties){
                    String line = null;
                    if(StringFormType.class.isInstance(property.getType())){
                        System.out.printf("请输入{%s}:",property.getName());
                        System.out.println();
                        line = scanner.nextLine();
                        map.put(property.getId(),line);
                    }else if(DateFormType.class.isInstance(property.getType())){
                        System.out.printf("请输入%s格式 (yyyy-MM-dd): ",property.getName());
                        System.out.println();
                        line = scanner.nextLine();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date parse = simpleDateFormat.parse(line);
                        map.put(property.getId(),parse);
                    }else {
                        System.out.printf("类型暂不支持%s",property.getType());
                        System.out.println();
                    }
                    System.out.printf("您输入的内容是: [%s]",line);
                    System.out.println();
                }
                taskService.complete(task.getId(),map);
                processInstance = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
            }
        }

        System.out.println("结束程序");

    }

}

各个服务启用时会操作的数据库

Activiti学习系列一_第1张图片

你可能感兴趣的:(java)