1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Activiti6.0流程引擎学习——(11)使用IDEA编码实现的请假流程

Activiti6.0流程引擎学习——(11)使用IDEA编码实现的请假流程

时间:2022-08-06 18:53:13

相关推荐

Activiti6.0流程引擎学习——(11)使用IDEA编码实现的请假流程

本篇博客要使用IDEA来实现之前绘制好的请假流程图。流程图如下:

具体创建这个流程图请看这篇博客:/JJBOOM425/article/details/85015145

1、创建maven工程

我们在IDEA中new一个maven工程,这里我们不使用脚手架来创建maven工程。直接点击next进行下一步。

然后我们填写工程信息:

2、添加相关的配置

我们打开创建项目后的pom.xml文件,可以看到里面已经有了一些信息:

我们在这个XML文件中添加这个maven工程所需的配置:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jjf.activiti</groupId><artifactId>activiti6-leaveprocess</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- Activiti引擎依赖 --><dependency><groupId>org.activiti</groupId><artifactId>activiti-engine</artifactId><version>6.0.0</version></dependency><!-- 单元测试,注意scope,只有在测试时候使用 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- 较受欢迎的日志组建,类似于log4j --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.1.11</version></dependency><!-- 常用类 --><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>23.0</version></dependency><!-- h2内存级数据库 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>1.3.176</version></dependency></dependencies></project>

3、创建Activiti流程引擎的启动类

首先在src/main/java目录下创建一个 com.jjf.activiti.leaveprove文件夹,在里面我们创建DemoMain的启动类:

启动类中我们按照四步来创建一个流程的启动类:

一、创建流程引擎

ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration(); //创建默认的基于内存数据库的流程引擎配置对象ProcessEngine processEngine = cfg.buildProcessEngine(); //构造流程引擎String engineName = processEngine.getName(); //获取流程引擎的nameString version = ProcessEngine.VERSION; //获取流程引擎的版本信息LOGGER.info("流程引擎名称 [{}], 版本 [{}]", engineName, version);

二、部署流程定义文件

RepositoryService repositoryService = processEngine.getRepositoryService(); //创建一个对流程编译库操作的ServiceDeploymentBuilder deploymentBuilder = repositoryService.createDeployment(); //获取一个builderdeploymentBuilder.addClasspathResource("LeaveProcess.bpmn20.xml"); //这里写上流程编译路径Deployment deployment = deploymentBuilder.deploy(); //部署String deploymentId = deployment.getId(); //获取deployment的IDProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();//根据deploymentId来获取流程定义对象LOGGER.info("流程定义文件 [{}] , 流程ID [{}]", processDefinition.getName(),

三、启动运行流程

RuntimeService runtimeService = processEngine.getRuntimeService(); //启动流程要有一个运行时对象ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId()); //这里我们根据processDefinition的ID来启动LOGGER.info("启动流程 [{}]", processInstance.getProcessDefinitionKey());

四、处理流程任务

Scanner scanner = new Scanner(System.in);while (processInstance != null && !processInstance.isEnded()) { //判断流程不为空,且流程没有结束TaskService taskService = processEngine.getTaskService();List<Task> list = taskService.createTaskQuery().list(); //列出当前需要处理的任务LOGGER.info("待处理任务数量 [{}]", list.size());for (Task task : list) {LOGGER.info("待处理任务 [{}]", task.getName());FormService formService = processEngine.getFormService(); //通过formService来获取form表单输入TaskFormData taskFormData = formService.getTaskFormData (task.getId());List<FormProperty> formProperties = taskFormData.getFormProperties(); //获取taskFormData的表单内容Map<String,Object> variables = Maps.newHashMap(); //这个Map键值对来存对应表单用户输入的内容for (FormProperty property : formProperties){ //property为表单中的内容String line = null; //这里获取输入的信息if(StringFormType.class.isInstance(property.getType())){ //如果是String类型的话LOGGER.info("请输入 [{}] ?" , property.getName()); //输入form表单的某一项内容line = scanner.nextLine();variables.put(property.getId(),line);}else if(DateFormType.class.isInstance(property.getType())){ //如果是日期类型的话LOGGER.info("请输入 [{}] ? 格式为(yyyy-MM-dd)" , property.getName()); //输入form表单的某一项内容line = scanner.nextLine();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //设置输入的日期格式Date date = dateFormat.parse(line);variables.put(property.getId(),date);}else{LOGGER.info("类型不支持 [{}]",property.getType());}LOGGER.info("您输入的内容是 [{}] " , line);}plete(task.getId(),variables);processInstance = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();}}scanner.close();

4、导入logback文件

将logback.xml导入到resources目录下,为了筛选掉一些对我们来说并没有用到日志信息,我们要在日志中打印输出信息,避免别的信息造成干扰。

其中内容如下:

<?xml version="1.0" encoding="UTF-8"?><configuration><!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径--><property name="LOG_HOME" value="/home/tomcatlog/logs/agent" /><property name="plain" value="%msg%n" /><!-- 控制台输出 --><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${plain}</pattern></encoder></appender><!-- 按照每天生成日志文件 --><appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_HOME}/agent.log</file><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!--日志文件输出的文件名 --><fileNamePattern>${LOG_HOME}/agent.%d{yyyy-MM-dd}.log</fileNamePattern><!--日志文件保留天数 --><maxHistory>30</maxHistory></rollingPolicy><encoder><!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --><pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</pattern></encoder></appender><logger name="jdbc.sqlonly" level="OFF" /><logger name="jdbc.resultsettable" level="OFF" /><logger name="jdbc.sqltiming" level="erroe" /><logger name="jdbc.audit" level="error" /><logger name="jdbc.connection" level="error" /><logger name="jdbc.resultset" level="error" /><logger name="root"><level value="ERROR"/></logger><logger name="com.jjf"><level value="DEBUG"/></logger><!-- 日志输出级别 --><root level="info"><!-- ERROR、WARN、INFO、DEBUG --><appender-ref ref="STDOUT" /><appender-ref ref="FILE" /></root></configuration>

5、复制bpmn流程图文件

这里将我们上一篇博客绘制好的请假流程图复制到resources目录中。

将这个bpmn文件重新拷贝一份,命名为LeaveProcess.bpmn20.xml

最后的文件格式如下:

其中LeaveProcess.bpmn20.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?><definitions xmlns="/spec/BPMN/0524/MODEL" xmlns:xsi="/2001/XMLSchema-instance" xmlns:xsd="/2001/XMLSchema" xmlns:activiti="/bpmn" xmlns:bpmndi="/spec/BPMN/0524/DI" xmlns:omgdc="/spec/DD/0524/DC" xmlns:omgdi="/spec/DD/0524/DI" typeLanguage="/2001/XMLSchema" expressionLanguage="/1999/XPath" targetNamespace="/test"><process id="LeaveProcess" name="请假流程" isExecutable="true"><startEvent id="startevent" name="开始"></startEvent><userTask id="submitform" name="填写请假申请"><extensionElements><activiti:formProperty id="message" name="申请信息" type="string" required="true"></activiti:formProperty><activiti:formProperty id="name" name="申请人姓名" type="string" required="true"></activiti:formProperty><activiti:formProperty id="submitTime" name="提交时间" type="date" datePattern="yyyy-MM-dd" required="true"></activiti:formProperty><activiti:formProperty id="submitType" name="确认申请" type="string" required="true"></activiti:formProperty></extensionElements></userTask><sequenceFlow id="flow1" sourceRef="startevent" targetRef="submitform"></sequenceFlow><exclusiveGateway id="decideSubmit" name="提交或取消"></exclusiveGateway><sequenceFlow id="flow2" sourceRef="submitform" targetRef="decideSubmit"></sequenceFlow><userTask id="ZG_approve" name="部门主管审批"><extensionElements><activiti:formProperty id="ZGapprove" name="主管审批结果" type="string" required="true"></activiti:formProperty><activiti:formProperty id="ZGmessage" name="主管备注" type="string" required="true"></activiti:formProperty></extensionElements></userTask><sequenceFlow id="flow3" sourceRef="decideSubmit" targetRef="ZG_approve"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="y" || submitType=="Y"}]]></conditionExpression></sequenceFlow><exclusiveGateway id="decideZGapprove" name="主管审批校验"></exclusiveGateway><sequenceFlow id="flow4" sourceRef="ZG_approve" targetRef="decideZGapprove"></sequenceFlow><userTask id="ZJL_approve" name="总经理审批"><extensionElements><activiti:formProperty id="ZJLapprove" name="总经理审批结果" type="string" required="true"></activiti:formProperty><activiti:formProperty id="ZJLmessage" name="总经理备注" type="string" required="true"></activiti:formProperty></extensionElements></userTask><sequenceFlow id="flow5" sourceRef="decideZGapprove" targetRef="ZJL_approve"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${ZGapprove=="y" || ZGapprove=="Y"}]]></conditionExpression></sequenceFlow><exclusiveGateway id="decideZJLapprove" name="总经理审批校验"></exclusiveGateway><sequenceFlow id="flow6" sourceRef="ZJL_approve" targetRef="decideZJLapprove"></sequenceFlow><endEvent id="endevent" name="结束"></endEvent><sequenceFlow id="flow7" sourceRef="decideZJLapprove" targetRef="endevent"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${ZJLapprove=="y" || ZJLapprove=="Y"}]]></conditionExpression></sequenceFlow><endEvent id="endeventCancel" name="取消"></endEvent><sequenceFlow id="flow8" sourceRef="decideSubmit" targetRef="endeventCancel"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="n" || submitType=="N"}]]></conditionExpression></sequenceFlow><sequenceFlow id="flow9" sourceRef="decideZGapprove" targetRef="submitform"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${ZGapprove=="n" || ZGapprove=="N"}]]></conditionExpression></sequenceFlow><sequenceFlow id="flow10" sourceRef="decideZJLapprove" targetRef="submitform"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${ZJLapprove=="n" || ZJLapprove=="N"}]]></conditionExpression></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_LeaveProcess"><bpmndi:BPMNPlane bpmnElement="LeaveProcess" id="BPMNPlane_LeaveProcess"><bpmndi:BPMNShape bpmnElement="startevent" id="BPMNShape_startevent"><omgdc:Bounds height="35.0" width="35.0" x="65.0" y="279.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="submitform" id="BPMNShape_submitform"><omgdc:Bounds height="55.0" width="105.0" x="145.0" y="269.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="decideSubmit" id="BPMNShape_decideSubmit"><omgdc:Bounds height="40.0" width="40.0" x="295.0" y="277.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="ZG_approve" id="BPMNShape_ZG_approve"><omgdc:Bounds height="55.0" width="105.0" x="380.0" y="270.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="decideZGapprove" id="BPMNShape_decideZGapprove"><omgdc:Bounds height="40.0" width="40.0" x="530.0" y="278.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="ZJL_approve" id="BPMNShape_ZJL_approve"><omgdc:Bounds height="55.0" width="105.0" x="615.0" y="271.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="decideZJLapprove" id="BPMNShape_decideZJLapprove"><omgdc:Bounds height="40.0" width="40.0" x="765.0" y="279.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endevent" id="BPMNShape_endevent"><omgdc:Bounds height="35.0" width="35.0" x="850.0" y="282.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endeventCancel" id="BPMNShape_endeventCancel"><omgdc:Bounds height="35.0" width="35.0" x="395.0" y="329.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"><omgdi:waypoint x="100.0" y="296.0"></omgdi:waypoint><omgdi:waypoint x="145.0" y="296.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"><omgdi:waypoint x="250.0" y="296.0"></omgdi:waypoint><omgdi:waypoint x="295.0" y="297.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"><omgdi:waypoint x="335.0" y="297.0"></omgdi:waypoint><omgdi:waypoint x="380.0" y="297.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"><omgdi:waypoint x="485.0" y="297.0"></omgdi:waypoint><omgdi:waypoint x="530.0" y="298.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"><omgdi:waypoint x="570.0" y="298.0"></omgdi:waypoint><omgdi:waypoint x="615.0" y="298.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6"><omgdi:waypoint x="720.0" y="298.0"></omgdi:waypoint><omgdi:waypoint x="765.0" y="299.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7"><omgdi:waypoint x="805.0" y="299.0"></omgdi:waypoint><omgdi:waypoint x="850.0" y="299.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8"><omgdi:waypoint x="315.0" y="317.0"></omgdi:waypoint><omgdi:waypoint x="315.0" y="346.0"></omgdi:waypoint><omgdi:waypoint x="395.0" y="346.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9"><omgdi:waypoint x="550.0" y="318.0"></omgdi:waypoint><omgdi:waypoint x="549.0" y="382.0"></omgdi:waypoint><omgdi:waypoint x="397.0" y="382.0"></omgdi:waypoint><omgdi:waypoint x="197.0" y="382.0"></omgdi:waypoint><omgdi:waypoint x="197.0" y="324.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10"><omgdi:waypoint x="785.0" y="279.0"></omgdi:waypoint><omgdi:waypoint x="784.0" y="222.0"></omgdi:waypoint><omgdi:waypoint x="505.0" y="222.0"></omgdi:waypoint><omgdi:waypoint x="197.0" y="222.0"></omgdi:waypoint><omgdi:waypoint x="197.0" y="269.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram></definitions>

6、对代码进行重构

这里我将可以写成函数的代码选取,然后右键Refactor->Extract->Method... 。

这里写重构函数的函数名:

最后得到我的DemoMain.class如下:

package com.jjf.activiti.leaveprovess;import mon.collect.Maps;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.Date;import java.util.List;import java.util.Map;import java.util.Scanner;public class DemoMain{private static final Logger LOGGER = LoggerFactory.getLogger(DemoMain.class);public static void main(String[] args) throws ParseException {LOGGER.info("开始请假流程 . . .");//创建流程引擎ProcessEngine processEngine = getProcessEngine();//部署流程定义文件ProcessDefinition processDefinition = getProcessDefinition(processEngine);//启动运行流程ProcessInstance processInstance = getProcessInstance(processEngine, processDefinition);//处理流程任务processTask(processEngine, processInstance);LOGGER.info("结束请假流程 . . ");}/*** 处理流程任务* @param processEngine* @param processInstance* @throws ParseException*/private static 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> list = taskService.createTaskQuery().list(); //列出当前需要处理的任务LOGGER.info("待处理任务数量 [{}]", list.size());for (Task task : list) {LOGGER.info("待处理任务 [{}]", task.getName());Map<String, Object> variables = getMap(processEngine, scanner, task);//获取用户的输入信息plete(task.getId(),variables);processInstance = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();}}scanner.close();}/*** 获取用户的输入信息* @param processEngine* @param scanner* @param task* @return* @throws ParseException*/private static Map<String, Object> getMap(ProcessEngine processEngine, Scanner scanner, Task task) throws ParseException {FormService formService = processEngine.getFormService(); //通过formService来获取form表单输入TaskFormData taskFormData = formService.getTaskFormData (task.getId());List<FormProperty> formProperties = taskFormData.getFormProperties(); //获取taskFormData的表单内容Map<String,Object> variables = Maps.newHashMap(); //这个Map键值对来存对应表单用户输入的内容for (FormProperty property : formProperties){ //property为表单中的内容String line = null; //这里获取输入的信息if(StringFormType.class.isInstance(property.getType())){ //如果是String类型的话LOGGER.info("请输入 [{}] ?" , property.getName()); //输入form表单的某一项内容line = scanner.nextLine();variables.put(property.getId(),line);}else if(DateFormType.class.isInstance(property.getType())){ //如果是日期类型的话LOGGER.info("请输入 [{}] ? 格式为(yyyy-MM-dd)" , property.getName()); //输入form表单的某一项内容line = scanner.nextLine();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //设置输入的日期格式Date date = dateFormat.parse(line);variables.put(property.getId(),date);}else{LOGGER.info("类型不支持 [{}]",property.getType());}LOGGER.info("您输入的内容是 [{}] " , line);}return variables;}/*** 启动运行流程** @param processEngine* @param processDefinition*/private static ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {RuntimeService runtimeService = processEngine.getRuntimeService(); //启动流程要有一个运行时对象ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId()); //这里我们根据processDefinition的ID来启动LOGGER.info("启动流程 [{}]", processInstance.getProcessDefinitionKey());return processInstance;}/*** 部署流程定义文件** @param processEngine* @return*/private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine) {RepositoryService repositoryService = processEngine.getRepositoryService(); //创建一个对流程编译库操作的ServiceDeploymentBuilder deploymentBuilder = repositoryService.createDeployment(); //获取一个builderdeploymentBuilder.addClasspathResource("LeaveProcess.bpmn20.xml"); //这里写上流程编译路径Deployment deployment = deploymentBuilder.deploy(); //部署String deploymentId = deployment.getId(); //获取deployment的IDProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();//根据deploymentId来获取流程定义对象LOGGER.info("流程定义文件 [{}] , 流程ID [{}]", processDefinition.getName(), processDefinition.getId());return processDefinition;}/*** 创建流程引擎** @return*/private static ProcessEngine getProcessEngine() {ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration(); //创建默认的基于内存数据库的流程引擎配置对象ProcessEngine processEngine = cfg.buildProcessEngine(); //构造流程引擎String engineName = processEngine.getName(); //获取流程引擎的nameString version = ProcessEngine.VERSION; //获取流程引擎的版本信息LOGGER.info("流程引擎名称 [{}], 版本 [{}]", engineName, version);return processEngine;}}

7、执行请假流程

这里我们的输入是有一定的格式的,比如提交时间一定要按照格式输入,并且确认申请与审批结果都只能输入y/Y或n/N。因为这些我们在流程图中定义好的,如果有什么问题可以先看流程图绘制的博客。

以上就是我们对请假流程的编码实现。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。