zoukankan      html  css  js  c++  java
  • Activiti入门

    1、核心组件

     

    Activiti引擎提供了7大Service接口,均通过processEngine获取,支持链式API风格。

    Service接口 作用
    RepositoryService 流程仓库服务,管理流程仓库,比如部署、删除、读取流程资源
    IdentityService 身份服务,管理用户、组及其关系
    RuntimeService 运行服务,处理所有正在运行态的流程实例、任务等。
    TaskService 任务服务,管理(签收、办理、指派等)、查询任务。
    FormService 表单服务,读取和流程、任务相关的表单数据
    HistoryService 历史服务,管理所有历史数据
    ManagementService 引擎管理服务,和具体业务无关,管理引擎。
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    
    RuntimeService runtimeService = processEngine.getRuntimeService();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    TaskService taskService = processEngine.getTaskService();
    ManagementService managementService = processEngine.getManagementService();
    IdentityService identityService = processEngine.getIdentityService();
    HistoryService historyService = processEngine.getHistoryService();
    FormService formService = processEngine.getFormService();

    2、流程设计器

    Eclipse插件:Eclipse Designer

    web:Activiti Modeler

    3、设计流程

    在activiti部署的时候,必须同时存在leave.bpmn及leave.png。因此在eclipse设置:window—>preferences—>activiti->save->选中create process definition image when saving the diagram。绘制完leave.bpmn后,就会自动生成对应的png文件。

    4、测试

    public class SayHelloToLeaveTest {
    
        @Test
        public void testStartProcess() throws Exception {
            ProcessEngine processEngine = ProcessEngineConfiguration
                    .createStandaloneInMemProcessEngineConfiguration()
                    .buildProcessEngine();
    
            RepositoryService repositoryService = processEngine.getRepositoryService();
            String bpmnFileName = "me/kafeitu/activiti/helloworld/SayHelloToLeave.bpmn";
            repositoryServic.createDeployment()
                     .addInputStream("SayHelloToLeave.bpmn",this.getClass().getClassLoader().getResourceAsStream(bpmnFileName))
                     .deploy();
    
            ProcessDefinition processDefinition = repositoryService
                    .createProcessDefinitionQuery().singleResult();
            assertEquals("SayHelloToLeave", processDefinition.getKey());
    
            RuntimeService runtimeService = processEngine.getRuntimeService();
    
            Map<String, Object> variables = new HashMap<String, Object>();
            variables.put("applyUser", "employee1");
            variables.put("days", 3);
    
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("SayHelloToLeave", variables);
            assertNotNull(processInstance);
            System.out.println("pid=" + processInstance.getId() + ", pdid="
                    + processInstance.getProcessDefinitionId());
    
            TaskService taskService = processEngine.getTaskService();
            Task taskOfDeptLeader = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult();
            assertNotNull(taskOfDeptLeader);
            assertEquals("领导审批", taskOfDeptLeader.getName());
    
            taskService.claim(taskOfDeptLeader.getId(), "leaderUser");
            variables = new HashMap<String, Object>();
            variables.put("approved", true);
            taskService.complete(taskOfDeptLeader.getId(), variables);
    
            taskOfDeptLeader = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult();
            assertNull(taskOfDeptLeader);
    
            HistoryService historyService = processEngine.getHistoryService();
            long count = historyService.createHistoricProcessInstanceQuery().finished().count();
            assertEquals(1, count);
        }
    }

    5、自动部署

    通过Spring创建 ProcessEngine 时一个独有的功能。可以在初始化 ProcessEngine 时自动将定义的资源部署到 ProcessEngine 中。对新系统的上线或者开发过程中加入新的流程非常有用,可以自动部署引擎数据库中不存在的,或者修改过的流程定义。

    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
            <property name="dataSource" ref="dataSource" />
            <property name="transactionManager" ref="transactionManager" />
            <property name="databaseSchemaUpdate" value="true" />
            <property name="jobExecutorActivate" value="false" />
            <property name="deploymentResource" value="classpath*:/chapter6/**/*.bpmn"/>
        </bean>

    6、集成普通表单

    业务数据和流程数据分离

    <form action="${ctx }/chapter7/leave/start" class="form-horizontal" method="post" onsubmit="beforeSend()">
            <input type="hidden" name="startTime" />
            <input type="hidden" name="endTime" />
            <fieldset>
                <legend><small>请假申请</small></legend>
                <div id="messageBox" class="alert alert-error input-large controls" style="display:none">输入有误,请先更正。</div>
                <div class="control-group">
                    <label for="loginName" class="control-label">请假类型:</label>
                    <div class="controls">
                        <select id="leaveType" name="leaveType" class="required">
                            <option>公休</option>
                            <option>病假</option>
                            <option>调休</option>
                            <option>事假</option>
                            <option>婚假</option>
                        </select>
                    </div>
                </div>
                <div class="control-group">
                    <label for="name" class="control-label">开始时间:</label>
                    <div class="controls">
                        <input type="text" id="startDate" class="datepicker input-small" data-date-format="yyyy-mm-dd" />
                        <input type="text" id="startTime" class="time input-small" />
                    </div>
                </div>
                <div class="control-group">
                    <label for="plainPassword" class="control-label">结束时间:</label>
                    <div class="controls">
                        <input type="text" id="endDate" class="datepicker input-small" data-date-format="yyyy-mm-dd" />
                        <input type="text" id="endTime" class="time input-small" />
                    </div>
                </div>
                <div class="control-group">
                    <label for="groupList" class="control-label">请假原因:</label>
                    <div class="controls">
                        <textarea name="reason"></textarea>
                    </div>
                </div>
                <div class="form-actions">
                    <button type="submit" class="btn"><i class="icon-play"></i>启动流程</button>
                </div>
            </fieldset>
        </form>
    
    form
    @Service
    @Transactional
    public class LeaveWorkflowService {
    
        private Logger logger = LoggerFactory.getLogger(getClass());
    
        @Autowired
        LeaveManager leaveManager;
    
        @Autowired
        private IdentityService identityService;
    
        @Autowired
        private RuntimeService runtimeService;
    
        @Autowired
        private TaskService taskService;
    
        @Autowired
        private RepositoryService repositoryService;
    
        /**
         * 保存请假实体并启动流程
         */
        public ProcessInstance startWorkflow(Leave entity, String userId, Map<String, Object> variables) {
            if (entity.getId() == null) {
                entity.setApplyTime(new Date());
                entity.setUserId(userId);
            }
            leaveManager.save(entity);//持久化请假实体
            String businessKey = entity.getId().toString();//实体保存后的ID,作为流程中的业务key
    
            // 用来设置启动流程的人员ID,引擎会自动把用户ID保存到activiti:initiator中
            identityService.setAuthenticatedUserId(userId); // 设置当前用户
    
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave", businessKey, variables);//将业务主ID设置为流程实例的key
            String processInstanceId = processInstance.getId();
            entity.setProcessInstanceId(processInstanceId);// 将流程实例的ID保存至业务表
            logger.debug("start process of {key={}, bkey={}, pid={}, variables={}}", new Object[]{"leave", businessKey, processInstanceId, variables});
            leaveManager.save(entity);
            return processInstance;
        }
    
        ...
    }

     7、解决流程图乱码问题

    /**
     * 解决乱码问题
     * @author Administrator
     *
     */
    @Component
    public class ShareniuProcessEngineConfigurationConfigurer implements ProcessEngineConfigurationConfigurer  {
        public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
            processEngineConfiguration.setActivityFontName("宋体");
            processEngineConfiguration.setLabelFontName("宋体");
            processEngineConfiguration.setAnnotationFontName("宋体");
            System.out.println("ShareniuProcessEngineConfigurationConfigurer#############");
            System.out.println(processEngineConfiguration.getActivityFontName());
        }
  • 相关阅读:
    js实现弹窗后选择信息填入text标签中以及需要注意的问题
    JDBC链接mysql之后出现read-only
    面试题之心理测试题及答案
    互联网经济思维故事
    模板
    工具包、类
    开源项目
    项目开发工具
    Java API 常用 详解
    spring文章
  • 原文地址:https://www.cnblogs.com/ijavanese/p/9442486.html
Copyright © 2011-2022 走看看