JBPM入门
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
理解JBPM(java Business Process Management)的基本概念:jPDL - JBPM Process Definition Language
JBPM簡要過程:
1、定義流程(利用JPDL)
2、部署流程(部署到數(shù)據(jù)庫)
3、創(chuàng)建公文并與流程實例綁定
4、可通過JBPM的接口,觸發(fā)流程向下流動
5、可通過JBPM的接口,獲得流動到某個用戶那里的文檔(即待處理任務(wù)列表)
6、可通過JBPM的接口,結(jié)束某個用戶的任務(wù)(這將觸發(fā)流程繼續(xù)向下流動)
7、如此,直到結(jié)束
----------------------------------------------
測試:
1、安裝JBPM
- 引入Hibernate依賴包
- 引入JBPM依賴包
* bsh.jar
* jcr-1.0.jar
* jbpm-identity.jar
* jbpm-jpdl.jar
- 引入數(shù)據(jù)庫驅(qū)動
* mysql-connector-java-3.1.13-bin.jar
2、定義相關(guān)配置文件
- Hibernate配置文件
* 提供hibernate配置文件(可以從config/目錄下拷貝,并修改其中的數(shù)據(jù)庫連接設(shè)置即可)
3、假設(shè)現(xiàn)在有一個公文,需要經(jīng)過:張三、李四、王五的審批之后才能結(jié)束
4、我們定義一個Document對象,及其hibernate映射,并將修改hibernate配置文件,將映射添加到其配置中(以便創(chuàng)建相應(yīng)的數(shù)據(jù)庫表)
Document對象定義如下:
public class Document {private int id;private String title;private String content;private String creator;private Long processIntanceId;/*** @return the id*/public int getId() {return id;}/*** @param id* the id to set*/public void setId(int id) {this.id = id;}/*** @return the title*/public String getTitle() {return title;}/*** @param title* the title to set*/public void setTitle(String title) {this.title = title;}/*** @return the content*/public String getContent() {return content;}/*** @param content* the content to set*/public void setContent(String content) {this.content = content;}/*** @return the creator*/public String getCreator() {return creator;}/*** @param creator* the creator to set*/public void setCreator(String creator) {this.creator = creator;}/*** @return the processIntanceId*/public Long getProcessIntanceId() {return processIntanceId;}/*** @param processIntanceId* the processIntanceId to set*/public void setProcessIntanceId(Long processIntanceId) {this.processIntanceId = processIntanceId;} }
對應(yīng)的hibernate配置文件如下:
<hibernate-mapping><class table="T_Document" name="org.darkness.jbpmtest.Document"><id name="id"><generator class="native"/></id><property name="title"/><property name="content"/><property name="creator"/><property name="processIntanceId"/></class> </hibernate-mapping>
5、現(xiàn)在讓我們來測試一下:
- 創(chuàng)建數(shù)據(jù)庫表: JbpmConfiguration.getInstance().createSchema();
- 定義流程: process.xml
<?xml version="1.0" encoding="UTF-8"?> <process-definition name="文檔測試流程"><start-state name="開始"><transition name="提交張三審批" to="張三審批" /></start-state><task-node name="張三審批"><task name="審批1"><assignment actor-id="張三" /></task><transition name="提交李四審批" to="李四審批" /></task-node><task-node name="李四審批"><task name="審批2"><assignment actor-id="李四" /></task><transition name="提交王五審批" to="王五審批" /></task-node><task-node name="王五審批"><task name="審批3"><assignment actor-id="王五" /></task><transition name="結(jié)束流程" to="結(jié)束" /></task-node><end-state name="結(jié)束" /> </process-definition>
- 部署流程:
JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();// 創(chuàng)建jbpmConfiguration對象 ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("process.xml");// 讀取流程定義文件,創(chuàng)建processdefinition對象// context對象類似于hibernate session對象的功能JbpmContext context = jbpmConfiguration.createJbpmContext();// 創(chuàng)建jbpmContext對象try {context.deployProcessDefinition(processDefinition);// 部署流程到數(shù)據(jù)庫} finally {// context對象需要關(guān)閉context.close();// 關(guān)閉context對象}
????????? - 創(chuàng)建公文
Session session = HibernateUtils.getSession();try {session.beginTransaction();Document doc = new Document();doc.setTitle("測試公文");doc.setContent("測試公文的內(nèi)容");doc.setCreator("趙一");session.save(doc);session.getTransaction().commit();} catch (Exception e) {session.getTransaction().rollback();} finally {HibernateUtils.closeSession(session);}
- 將公文與流程綁定(即需要創(chuàng)建流程實例)
JbpmContext context = jbpmConfiguration.createJbpmContext();context.setSessionFactory(HibernateUtils.getSessionFactory());// 將JBPM與程序中的session綁定// 從數(shù)據(jù)庫中加載ProcessDefinition對象ProcessDefinition definition = context.getGraphSession().findLatestProcessDefinition("文檔測試流程");// 從流程中創(chuàng)建一個流程實例ProcessInstance processInstance = new ProcessInstance(definition);// 存儲流程實例context.save(processInstance);// 加載公文Document doc = (Document) context.getSession().load(Document.class, 1);// 綁定流程實例到公文doc.setProcessIntanceId(processInstance.getId());// 綁定公文到流程實例(ContextInstance相當(dāng)于一個變量的容器)processInstance.getContextInstance().createVariable("document",doc.getId());context.close();
* context.getGraphSession().findLatestProcessDefinition("流程名稱");
- 公文創(chuàng)建者提交公文
JbpmContext context = jbpmConfiguration.createJbpmContext();context.setSessionFactory(HibernateUtils.getSessionFactory());Document doc = (Document) context.getSession().load(Document.class, 1);// 加載公文信息ProcessInstance processInstance = context.getProcessInstance(doc.getProcessIntanceId());// 即根據(jù)流程實例ID加載流程實例// 觸發(fā)流程往下走(即到達第一個節(jié)點)processInstance.getRootToken().signal();context.close();
- 這時候,我們可以測試一下,看看流程當(dāng)前所處的節(jié)點
JbpmContext context = jbpmConfiguration.createJbpmContext();context.setSessionFactory(HibernateUtils.getSessionFactory());Document doc = (Document) context.getSession().load(Document.class, 1);ProcessInstance processInstance = context.getProcessInstance(doc.getProcessIntanceId());// 當(dāng)前節(jié)點?第一個節(jié)點對應(yīng)的用戶登錄,應(yīng)該能夠查詢到其當(dāng)前的任務(wù)(有公文等待其審批)System.err.println(processInstance.getRootToken().getNode().getName());context.close();
查找某個人的任務(wù)列表:
List tasks = context.getTaskMgmtSession().findTaskInstances("張三");// 查找張三的任務(wù)列表,列表元素是TaskInstance實例 taskInstance.getProcessInstance().getContextInstance().getVariable("document");
通過:可以找到其綁定的公文ID
- 查找到當(dāng)前的任務(wù)對應(yīng)的公文之后,即可對其審批,并繼續(xù)往下走
taskInstance.end();
- 如此,直到結(jié)束
processInstance.hasEnded()- 如果流程已經(jīng)到達終點,本調(diào)用將返回true
轉(zhuǎn)載于:https://my.oschina.net/darkness/blog/510204
總結(jié)
- 上一篇: Android 超高仿微信图片选择器 图
- 下一篇: iOS NSNotificationCe