springboot整合flowable框架入门步骤-mile米乐体育

最近工作中有用到工作流的开发,引入了flowable工作流框架,在此记录一下springboot整合flowable工作流框架的过程,以便后续再次使用到时可以做一些参考使用,如果项目中有涉及到流程审批的话,可以使用该框架帮我们实现流程图示化展示的功能,为了快速了解flowable工作流框架的一个使用过程,我们直接步入主题,springboot整合flowable工作流框架的步骤如下:

1、首先创建一个springboot工程,然后引入flowable pom依赖,代码如下:

org.flowable flowable-spring-boot-starter 6.7.0

2、创建流程图定义文档

这里有一个使用flowable-ui可视化的工程来创建流程图定义文档,https://www.wandouip.com/t5i212543/,具体实施过程如下:

先从 https://github.com/flowable/flowable-engine/releases 上下载一个发布文档,这里选择flowable 6.7.2 release;然后解压缩文件,将里面的wars文档下的两个jar包(flowable-rest.war、flowable-ui.war)部署到tomcat下,放到webapps文件加下,点击运行,运行存在一个解压缩文件的过程,会产生flowable-rest、flowable-ui文件夹,浏览器输入 http://localhost:8080/flowable-ui,如下图:

用户名密码输入admin/test,如下图:

点击建模器应用程序,点击右上角”创建流程“,填写相关信息,进去后就可以可视化地创建流程,如下图:

上面初步展示了使用可视化制作工具制作流程图的过程,然后导出bpmn2文件,文件内容大概如下:

以上初步实现怎么使用可视化工具来创建流程图定义文件,里面更为具体的使用方法还有待探索,上面的步骤执行完毕后,我们就可以运行我们的springboot工程了,在配置文件中配置好数据库链接相关的信息后,然后配置flowable相关信息:

flowable:

aysnc-executor-activate: false

database-schema-update: true

process-definition-location-prefix: classpath*:/processes/

process-definition-location-suffixes: "**.bpmn20.xml, **.bpmn"

上面的信息配置完毕后,就可以运行我们的工程了,在运行工程之前,在工程的resources文件夹下创建一个processes文件夹,我们的流程定义文档就放在这个文件夹下,创建一个process-test.bpmn20.xml放到processes文件夹下,具体内容定义如下:

www.cppcns.comndi:bpmnshape bpmnelement="sendjuniorrejectemail" id="bpmnshape_sendjuniorrejectemail">

上面文件中的process标签的id值是该流程定义的唯一标识,在创建流程实例时需要传入processid标识,上面的assignee变量后面定义了一个变量${senioradmin},这个是由接口调用时传入的,activiti:class后面的值是一个类,表示执行到这个步骤时会触发执行某个动作,比如id为sendjuniorrectemail的servicetask中的class定义如下:

@slf4j public class sendjuniorrejectionmaildelegate implements javadelegate { @override public void execute(delegateexecution execution) { string requestuser = (string) execution.getvariable("requestuser"); string resourceid = (string) execution.getvariable("resourceid"); system.out.println("sendjuniorrejectionmaildelegate"); log.info("send approval success mail for user [" requestuser "] with apply resource [" resourceid "]"); } }

上面的类需要实现javadelegate这个接口,上面的内容定义完毕后,就可以定义我们的实现方法了,先创建一个接口,定义一些方法:

public interface iprocess { /** * 创建一个流程实例,创建实例时会登记一些信息,这些信息可以通过调用 * queryprocessvariables方法获取到,调用时需要传递processinstanceid * @param paramobj * @return */ processinstanceentity startprocess(paramobj paramobj); /** * 获取指定工作人的代办任务 * @param assignee * @return */ list taskinstance(string assignee); /** * 处理工作 * @param paramobj */ void handletask(paramobj paramobj); /** * 获取某个流程实体的状态,各个审批环节所处的状态信息 * @param processinstanceid * @return */ list queryprocessstatus(string processinstanceid); /** * 查看创建流程实例时登记的变量信息 * @param processinstanceid * @return */ map queryprocessvariables(string processinstanceid); /** * 获取某人的历史审批数据 * @param assignee * @return */ list queryhistoryprocess(string assignee); /** * 生成流程的图谱 * @param httpservletresponse * @param processinstanceid */ void genprocessdiagram(httpservletresponse httpservletresponse, string processinstanceid) throws exception; /** * 查询是否存在历史数据的流程实例 * @param processinstanceid * @return */ boolean isexisthistoricprocessinstance(string processinstanceid); /** * 查询指定的流程是否是运行中的流程 * @param processinstanceid * @return */ boolean isexistrunningprocessinstance(string processinstanceid); /** * 将指定的流程挂起 * @param processinstanceid */ void suspendprocessinstance(string processinstanceid); /** * 终止项目流程 * @param paramobj */ void terminateprocessinstance(paramobj paramobj); /** * 将指定的流程激活 * @param processinstanceid */ void activateprocessinstance(string processinstanceid); /** * 删除流程实例 * @param paramobj */ void deleteprocessinstance(paramobj paramobj); /** * 将任务返回到某一步骤 * @param taskid * @param targettaskkey 返回到的目标任务id */ void rollbacktask(string taskid, string targettaskkey); boolean isprocessfinished(string processinstanceid); }

定义好接口后,再定义一个实现类:

@service public class iprocessimpl implements iprocess { @autowired private repositoryservice repositoryservice; private runtimeservice runtimeservice; private taskservice taskservice; private historyservice historyservice; private processengine processengine; managementservice managementservice; @override public processinstanceentity startprocess(paramobj paramobj) { map variables = new hashmap<>(); // 请求的资源id variables.put("resourceid", paramobj.getresourceid()); // 请求发起用户 variables.put("requestuser", paramobj.getrequestuser()); // 初级审批用户 variables.put("junioradmin", paramobj.getjunioradmin()); // 高级审批用户 variables.put("senioradmin", paramobj.getsenioradmin()); processinstance processinstance=runtimeservice. startprocessinstancebykey(constantvalues.flowable_process_test, variables); processinstanceentity entity=new processinstanceentity(); entity.setprocessdeploymentid(processinstance.getdeploymentid()); entity.setprocessinstanceid(processinstance.getprocessinstanceid()); entity.setactivityid(processinstance.getactivityid()); return entity; } public list taskinstance(string assignee) { list entities=new arraylist<>(); list tasks= taskservice.createtaskquery().taskassignee(assignee).orderbytaskcreatetime().desc().list(); if(!collectionutils.isempty(tasks)){ tasks.stream().foreach(task -> { taskinstanceentity entity=new taskinstanceentity(); string id=task.getid(); entity.setcreatetime(task.getcreatetime()); entity.settaskname(task.getname()); entity.setprocessinstanceid(task.getprocessinstanceid()); entity.settaskid(id); map processvariables = taskservice.getvariables(id); entity.setrequestuser(processvariables.get("requestuser").tostring()); entity.setresourceid(processvariables.get("resourceid").tostring()); entities.add(entity); }); } return entities; public void handletask(paramobj paramobj) { map taskvariables = new hashmap<>(); string approved=paramobj.isapproved()?"y":"n"; taskvariables.put("approved", approved); //审核结果和审核意见都封装为json然后放在评论里,后续需要进行逆操作。 objectmapper objectmapper = new objectmapper(); map map= new hashmap<>(); map.put("approved", approved); map.put("comment", paramobj.getcomment()); try { string json = objectmapper.writevalueasstring(map); taskservice.addcomment(paramobj.gettaskid(), null, json); taskservice.complete(paramobj.gettaskid(), taskvariables); } catch (exception e) { throw new runtimeexception(e); public list queryprocessstatus(string processinstanceid) { list result = new arraylist<>(); list historictaskinstances = historyservice.createhistorictaskinstancequery() .processinstanceid(processinstanceid).list(); if(collectionutils.isempty(historictaskinstances)) { throw new runtimeexception("process instance [" processinstanceid "] not exist"); for (historictaskinstance hti : historictaskinstances) { string taskid = hti.getid(); string taskname = hti.getname(); string assignee = hti.getassignee(); date createtime = hti.getcreatetime(); string comment = null; string approved=null; list comments = taskservice.gettaskcomments(taskid); if (!collectionutils.isempty(comments)) { comment = comments.get(0).getfullmessage(); if(null!=comment) { //这里进行评论的json数据的逆操作提取数据 objectmapper mapper = new objectmapper(); try { map data = mapper.readvalue(comment, map.class); approved=data.get("approved").tostring(); comment=data.get("comment").tostring(); } catch (exception e) { system.out.println(e.tostring()); } } } processstatusentity pd=new processstatusentity(); pd.settaskname(taskname); pd.setassignee(assignee); pd.setcreatetime(createtime); pd.setapproved(approved); pd.setcomment(comment); pd.settaskid(hti.getid()); pd.setprocessinstanceid(hti.getprocessinstanceid()); result.add(pd); return result; public map queryprocessvariables(string processinstanceid) { list historicvariableinstances = historyservice.createhistoricvariableinstancequery() if (historicvariableinstances == null) { map ret= new hashmap<>(); for(historicvariableinstance var: historicvariableinstances) { ret.put(var.getvariablename(), var.getvalue()); return ret; public list queryhistoryprocess(string assignee) { list result=new arraylist<>(); list activities = historyservice.createhistoricactivityinstancequery() .taskassignee(assignee).finished().orderbyhistoricactivityinstanceendtime().desc().list(); for(historicactivityinstance h : activities) { histanceinstanceentity d=new histanceinstanceentity(); d.setprocessinstanceid(h.getprocessinstanceid()); d.settaskid(h.gettaskid()); d.setstarttime(h.getstarttime()); d.setendtime(h.getendtime()); result.add(d); public void genprocessdiagram(httpservletresponse httpservletresponse, string processinstanceid) throws exception{ processinstance pi = runtimeservice.createprocessinstancequery(). processinstanceid(processinstanceid).singleresult(); //流程走完的不显示图 if (pi == null) { // system.out.println("不存在该流程或则流程已经走完"); throw new runtimeexception("不存在该流程或则流程已经走完"); // return; task task = taskservice.createtaskquery().processinstanceid(pi.getid()).singleresult(); //使用流程实例id,查询正在执行的执行对象表,返回流程实例对象 string instanceid = task.getprocessinstanceid(); list executions = runtimeservice .createexecutionquery() .processinstanceid(instanceid) .list(); //得到正在执行的activity的id list activityids = new arraylist<>(); list flows = new arraylist<>(); for (execution exe : executions) { list ids = runtimeservice.getactiveactivityids(exe.getid()); activityids.addall(ids); //获取流程图 bpmnmodel bpmnmodel = repositoryservice.getbpmnmodel(pi.getprocessdefinitionid()); processengineconfiguration engineconf = processengine.getprocessengineconfiguration(); processdiagramgenerator diagramgenerator = engineconf.getprocessdiagramgenerator(); inputstream in = diagramgenerator.generatediagram(bpmnmodel, "png", activityids, flows, engineconf.getactivityfontname(), engineconf.getlabelfontname(), engineconf.getannotationfontname(), engineconf.getclassloader(), 1.0,true); outputstream out = null; byte[] buf = new byte[1024]; int length = 0; out = httpservletresponse.getoutputstream(); while ((length =in.read(buf)) != -1) { out.write(buf, 0, length); } finally { if (in != null) { in.close(); if (out != null) { out.close(); public boolean isexisthistoricprocessinstance(string processinstanceid) { historicprocessinstance historicprocessinstance = historyservice.createhistoricprocessinstancequery(). processinstanceid(processinstanceid).singleresult(); if (historicprocessinstance == null) { return false; return true; public boolean isexistrunningprocessinstance(string processinstanceid) { processinstance processinstance = runtimeservice.createprocessinstancequery(). if (processinstance == null) { public void suspendprocessinstance(string processinstanceid) { runtimeservice.suspendprocessinstancebyid(processinstanceid); public void terminateprocessinstance(paramobj paramobj) { runtimeservice.deleteprocessinstance(paramobj.getprocessinstanceid(),paramobj.getdeletereason()); public void activateprocessinstance(string processinstanceid) { runtimeservice.activateprocessinstancebyid(processinstanceid); public void deleteprocessinstance(paramobj paramobj) { //查询是否操作 long count = runtimeservice.createexecutionquery().processinstanceid(paramobj.getprocessinstanceid()).count(); if(count>0){ deleteflowableprocessinstancecmd cmd= new deleteflowableprocessinstancecmd(paramobj.getprocessinstanceid(), paramobj.getdeletereason(),true); managementservice.executecommand(cmd); //runtimeservice.deleteprocessinstance(processinstanceid,deletereason); }else{ //删除历史数据的流程实体 historyservice.deletehistoricprocessinstance(paramobj.getprocessinstanceid()); public void rollbacktask(string taskid, string targettaskkey) { task currenttask = taskservice.createtaskquery().taskid(taskid).singleresult(); if (currenttask == null) { return ; list key = new arraylist<>(); key.add(currenttask.gettaskdefinitionkey()); runtimeservice.createchangeactivitystatebuilder() .processinstanceid(currenttask.getprocessinstanceid()) .moveactivityidstosingleactivityid(key, targettaskkey) .changestate(); public boolean isprocessfinished(string processinstanceid) { return historyservice.createhistoricprocessinstancequery().finished() .processinstanceid(processinstanceid).count()>0; }

实现类中注入的变量都是flowable框架中的变量,实现类中的方法的作用在接口中都有向相关注释,其中deleteprocessinstance方法中会引用一个类来删除流程实例,deleteflowableprocessinstancecmd类的定义如下:

@data public class deleteprocessinstancecmd implements command, serializable { string processinstanceid; string deletereason; //是否删除历史 boolean cascade=true; public deleteprocessinstancecmd(){ } public deleteprocessinstancecmd(string processinstanceid,string deletereason){ this.deletereason=deletereason; this.processinstanceid=processinstanceid; } public deleteprocessinstancecmd(string processinstanceid, string deletereason, boolean cascade){ this.deletereason=deletereason; this.processinstanceid=processinstanceid; this.cascade=cascade; } @override public void execute(commandcontext commandcontext) { executionentity entity= commandcontextutil.getexecutionentitymanager(commandcontext) .findbyid(processinstanceid); if(entity!=null){ if(entity.isdeleted()){ return null; } if(flowable5util.isflowable5processdefinitionid(commandcontext,entity.getprocessdefinitionid())){ flowable5compatibilityhandler handler=flowable5util.getflowable5compatibilityhandler(); handler.deleteprocessinstance(processinstanceid,deletereason); }else{ commandcontextutil.getexecutionentitymanager(commandcontext).deleteprocessinstance(entity.getprocessinstanceid(),deletereason,cascade); } } return null; } }

上述功能了类定义完毕后,就可以创建我们的controller类了,我们的controller类的定义如下:

@restcontroller @requestmapping("/flowabletest") public class flowablecontroller { @autowired iprocess process; @postmapping("/startprocess") public processinstanceentity startprocess(@requestbody paramobj paramobj){ return process.startprocess(paramobj); } @getmapping("/gettaskinstance/{assignee}") public list taskinstance(@pathvariable("assignee") string assignee){ return process.taskinstance(assignee); } @putmapping("/handletask") public string handletask(@requestbody paramobj paramobj){ process.handletask(paramobj); return "success"; } @getmapping("/queryprocessstatus") public list queryprocessstatus(string processinstanceid){ return process.queryprocessstatus(processinstanceid); } @getmapping("/queryprocessvariables") public map queryprocessvariables(string processinstanceid){ return process.queryprocessvariables(processinstanceid); } @getmapping("/queryhistoryprocess") public list queryhistoryprocess(string assignee){ return process.queryhistoryprocess(assignee); } @getmapping("/genprocessdiagram") public void genprocessdiagram(httpservletresponse httpservletresponse, string processinstanceid) throws exception { process.genprocessdiagram(httpservletresponse,processinstanceid); } @getmapping("/isexisthistoricprocessinstance") public boolean isexisthistoricprocessinstance(string processinstanceid){ return process.isexisthistoricprocessinstance(processinstanceid); } @getmapping("/isprocessfinished") public boolean isprocessfinished(string processinstanceid){ return process.isprocessfinished(processinstanceid); } @getmapping("/isexistrunningprocessinstance") public boolean isexistrunningprocessinstance(string processinstanceid){ return process.isexistrunningprocessinstance(processinstanceid); } @putmapping("/suspendprocessinstance") public string suspendprocessinstance(string processinstanceid){ process.suspendprocessinstance(processinstanceid); return "流程 " processinstanceid " 已经挂起"; } @putmapping("/terminateprocessinstance") public string terminateprocessinstance(paramobj paramobj){ process.terminateprocessinstance(paramobj); return "流程 " paramobj.getprocessinstanceid() " 已经终止"; } @putmapping("/activateprocessinstance") public string activateprocessinstance(string processinstanceid) { process.activateprocessinstance(processinstanceid); return "流程 " processinstanceid " 已经激活"; } @putmapping("/deleteprocessinstance") public string deleteprocessinstance(paramobj paramobj){ process.deleteprocessinstance(paramobj); return "流程 " paramobj.getprocessinstanceid() " 已经删除"; } @putmapping("/rollback") public string rollbacktask(string taskid, string targettaskkey){ process.rollbacktask(taskid,targettaskkey); return "流程回退成功"; } }

试运行工程,运行工程后,系统会自动生成一些表单,是一些act_和flw_开头的表单,如下,利用postman创建一个流程实例:

查看流程示例图:

调用查看任务接口,可以查看某个人有哪些任务需要处理:

调用任务处理接口:

查看处理后的结果:

以上是flowable流程框架的简单应用,更为详细的使用等待后续的挖掘......

到此这篇关于springboot整合flowable-初步入门的文章就介绍到这了,更多相关springboot整合flowable内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

展开全文
内容来源于互联网和用户投稿,文章中一旦含有米乐app官网登录的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系米乐app官网登录删除

最新文章

网站地图