Activiti的简单应用,使用外置表单的方式将业务页面绑定到工作流的结点上,当执行到当前结点时,打印出绑定表单的内容。
新建4个form页面,页面内容随便写些内容即可:
按照下图的方式依次绑定:
流程变量设置如图,其他的类似:
对应生成的xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
- <process id="myProcess" name="My process" isExecutable="true">
- <startEvent id="startevent1" name="Start"></startEvent>
- <userTask id="usertask1" name="显示用户详细信息" activiti:formKey="userinfo.form"></userTask>
- <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
- <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
- <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
- <userTask id="usertask3" name="耳机验证" activiti:formKey="ear.form"></userTask>
- <sequenceFlow id="flow4" name="耳机验证" sourceRef="exclusivegateway1" targetRef="usertask3">
- <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="ear"}]]></conditionExpression>
- </sequenceFlow>
- <userTask id="usertask4" name="二次登陆" activiti:formKey="secondtime.form"></userTask>
- <sequenceFlow id="flow5" name="存在登陆信息" sourceRef="exclusivegateway1" targetRef="usertask4">
- <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="exist"}]]></conditionExpression>
- </sequenceFlow>
- <exclusiveGateway id="exclusivegateway2" name="Exclusive Gateway"></exclusiveGateway>
- <sequenceFlow id="flow6" sourceRef="usertask4" targetRef="exclusivegateway2"></sequenceFlow>
- <sequenceFlow id="flow7" name="密码错误" sourceRef="exclusivegateway2" targetRef="usertask4">
- <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="no"}]]></conditionExpression>
- </sequenceFlow>
- <sequenceFlow id="flow8" name="密码正确" sourceRef="exclusivegateway2" targetRef="usertask3">
- <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="yes"}]]></conditionExpression>
- </sequenceFlow>
- <userTask id="usertask5" name="键盘验证" activiti:formKey="keyboard.form"></userTask>
- <sequenceFlow id="flow9" sourceRef="usertask3" targetRef="usertask5"></sequenceFlow>
- <endEvent id="endevent1" name="End"></endEvent>
- <sequenceFlow id="flow10" sourceRef="usertask5" targetRef="endevent1"></sequenceFlow>
- </process>
- <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
- <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
- <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
- <omgdc:Bounds height="35.0" width="35.0" x="75.0" y="79.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
- <omgdc:Bounds height="55.0" width="105.0" x="155.0" y="69.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
- <omgdc:Bounds height="40.0" width="40.0" x="305.0" y="77.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
- <omgdc:Bounds height="55.0" width="105.0" x="415.0" y="70.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4">
- <omgdc:Bounds height="55.0" width="105.0" x="273.0" y="178.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="exclusivegateway2" id="BPMNShape_exclusivegateway2">
- <omgdc:Bounds height="40.0" width="40.0" x="447.0" y="185.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="usertask5" id="BPMNShape_usertask5">
- <omgdc:Bounds height="55.0" width="105.0" x="600.0" y="70.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
- <omgdc:Bounds height="35.0" width="35.0" x="750.0" y="80.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
- <omgdi:waypoint x="110.0" y="96.0"></omgdi:waypoint>
- <omgdi:waypoint x="155.0" y="96.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
- <omgdi:waypoint x="260.0" y="96.0"></omgdi:waypoint>
- <omgdi:waypoint x="305.0" y="97.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
- <omgdi:waypoint x="345.0" y="97.0"></omgdi:waypoint>
- <omgdi:waypoint x="415.0" y="97.0"></omgdi:waypoint>
- <bpmndi:BPMNLabel>
- <omgdc:Bounds height="14.0" width="48.0" x="347.0" y="80.0"></omgdc:Bounds>
- </bpmndi:BPMNLabel>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
- <omgdi:waypoint x="325.0" y="117.0"></omgdi:waypoint>
- <omgdi:waypoint x="325.0" y="178.0"></omgdi:waypoint>
- <bpmndi:BPMNLabel>
- <omgdc:Bounds height="14.0" width="72.0" x="245.0" y="151.0"></omgdc:Bounds>
- </bpmndi:BPMNLabel>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
- <omgdi:waypoint x="378.0" y="205.0"></omgdi:waypoint>
- <omgdi:waypoint x="447.0" y="205.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
- <omgdi:waypoint x="467.0" y="225.0"></omgdi:waypoint>
- <omgdi:waypoint x="466.0" y="309.0"></omgdi:waypoint>
- <omgdi:waypoint x="389.0" y="309.0"></omgdi:waypoint>
- <omgdi:waypoint x="325.0" y="309.0"></omgdi:waypoint>
- <omgdi:waypoint x="325.0" y="233.0"></omgdi:waypoint>
- <bpmndi:BPMNLabel>
- <omgdc:Bounds height="14.0" width="48.0" x="469.0" y="267.0"></omgdc:Bounds>
- </bpmndi:BPMNLabel>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
- <omgdi:waypoint x="467.0" y="185.0"></omgdi:waypoint>
- <omgdi:waypoint x="467.0" y="125.0"></omgdi:waypoint>
- <bpmndi:BPMNLabel>
- <omgdc:Bounds height="14.0" width="48.0" x="468.0" y="166.0"></omgdc:Bounds>
- </bpmndi:BPMNLabel>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
- <omgdi:waypoint x="520.0" y="97.0"></omgdi:waypoint>
- <omgdi:waypoint x="600.0" y="97.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
- <omgdi:waypoint x="705.0" y="97.0"></omgdi:waypoint>
- <omgdi:waypoint x="750.0" y="97.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- </bpmndi:BPMNPlane>
- </bpmndi:BPMNDiagram>
- </definitions>
代码实现:
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.zip.ZipInputStream;
- import org.activiti.engine.FormService;
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngineConfiguration;
- import org.activiti.engine.RepositoryService;
- import org.activiti.engine.RuntimeService;
- import org.activiti.engine.TaskService;
- import org.activiti.engine.repository.Deployment;
- import org.activiti.engine.repository.ProcessDefinition;
- import org.activiti.engine.repository.ProcessDefinitionQuery;
- import org.activiti.engine.runtime.ProcessInstance;
- import org.activiti.engine.task.Task;
- import com.tgb.itoo.activiti.controller.CommonDelegate;
- import com.tgb.itoo.activiti.controller.milaoshi;
- import com.tgb.itoo.activiti.controller.tanghuan;
- public class formprocess {
- private static String readDataFromConsole(String prompt) {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String str = null;
- try {
- System.out.print(prompt);
- str = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return str;
- }
- public static void main(String[] args) {
- String str = readDataFromConsole("Please input string:");
- System.out.println("The information from console: " + str);
- ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
- .createStandaloneProcessEngineConfiguration();
- // 连接数据库的配置
- processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
- processEngineConfiguration
- .setJdbcUrl("jdbc:mysql://localhost:3306/activitiexam?useUnicode=true&characterEncoding=utf8");
- processEngineConfiguration.setJdbcUsername("root");
- processEngineConfiguration.setJdbcPassword("root");
- processEngineConfiguration.setDatabaseSchemaUpdate("create");
- ProcessEngine processEngine = processEngineConfiguration
- .buildProcessEngine();
- System.out.println(processEngine);
- // 获取流程存储服务组件
- RepositoryService repositoryService = processEngine
- .getRepositoryService();
- // 获取运行时服务组件
- RuntimeService runtimeService = processEngine.getRuntimeService();
- // 获取流程任务组件
- TaskService taskService = processEngine.getTaskService();
- //获取流程的表单组件
- FormService formService=processEngine.getFormService();
- // 1、部署流程文件
- InputStream in =repositoryService.createDeployment().name("MyProcess")
- .getClass().getClassLoader().getResourceAsStream("diagrams/"+ str +".zip");
- ZipInputStream zipInputStream = new ZipInputStream(in);
- Deployment deployment = processEngine.getRepositoryService()// 与流程定义和部署对象相关的Service
- .createDeployment()// 创建一个部署对象
- .name("流程定义")// 添加部署的名称
- .addZipInputStream(zipInputStream)// 指定zip格式的文件完成部署
- .deploy();// 完成部署
- // 2、启动流程--方式一
- /*ProcessInstance processInstance = runtimeService
- .startProcessInstanceByKey("myProcess");
- */
- /**
- * 2、启动流程--方式二
- * 此处使用这种方式,可能有问题,当所有的流程定义的key值均为myProcess时,查询出来的数据为多条
- * 但是真正使用时,我们是根据前台显示的多条中选择其中之一,就可以直接获取processDefinitionID故也没有问题
- */
- ProcessDefinitionQuery query = repositoryService
- .createProcessDefinitionQuery()
- .processDefinitionKey("myProcess").active()
- .orderByProcessDefinitionVersion().desc();
- ProcessDefinition processDefinition = query.list().get(0);
- Map<String, String> formProperties = new HashMap<String, String>();
- formProperties.put("numberOfDays", "20150601");
- //此处如果第二个参数为null值则报错,即使不放入任何数据也要使用map格式的对象
- ProcessInstance processInstance = formService
- .submitStartFormData(processDefinition.getId(), formProperties);
- String end="1";
- while (end.equalsIgnoreCase("1")) {
- // 3、查询任务
- Task task = taskService.createTaskQuery()
- .processInstanceId(processInstance.getId()).singleResult();
- ;
- if (task != null) {
- Object renderedTaskForm = formService.getRenderedTaskForm(task.getId());
- System.out.println("表单内容---"+renderedTaskForm.toString());
- System.out.println("任务名称---"+task.getName());
- /**
- * 如果整个流程都是顺序执行可以不暂停的,则去掉此段代码即可
- * 注:现实中存在的是页面流转过程中是需要点击下一步操作的,可以将此暂停理解为下一步的操作
- */
- str = readDataFromConsole("Please input next:");
- Map<String, Object> variables = new HashMap<String, Object>();
- variables.put("result", str);
- //方式一启动流程的完成任务,如果下一个任务需要参数使用map传递,此处的变量均是给下一个任务设置的
- taskService.complete(task.getId(),variables);
- //方式二启动流程完成任务
- //formService.submitTaskFormData(task.getId(), formProperties);
- //注:以上的两种方式可以混用
- }else {
- end="0";
- System.out.println("任务已经完成");
- }
- }
- }
- }
执行结果:
总结:
以上实现了结点绑定页面的操作,当执行到当前结点时,会打印出绑定页面的内容,这样页面的执行顺序就不需要我们去管理,而可以通过简单的配置实现,以上只是一个简单的main函数实现,下面会继续实现一个简单的demo。