Activiti進階(二)——部署流程資源的三種方式

來源:互聯網
上載者:User

    

    流程資源可以是各種類型的檔案,在啟動流程或流程執行個體運行過程中會被讀取。下面介紹常用的流程資源。


    一、流程資源


         流程定義檔案:副檔名為bpmn20.xml和bpmn;

         流程定義的圖片:用BPMN2.0規範的各種圖形描繪,一般用PNG的格式;

         表單檔案:把表單內容儲存在一個檔案中,其副檔名為drl;

         規則檔案:其副檔名為drl;


    二、部署流程資源


        部署流程資源有很多種方法,包括classpath、InputStream、字串、zip格式壓縮包,下面將一一介紹。


         1.classpath方式


public class ProcessDefinitionTest {/**流程引擎(核心對象),預設載入類路徑下命名為activiti.cfg.xml*/ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();//部署流程定義@Testpublic void deployementProcessDefinition(){Deployment deployment = processEngine.getRepositoryService()//擷取流程定義和部署對象相關的Service.createDeployment()//建立部署對象.name("helloworld示範")//聲明流程的名稱.addClasspathResource("diagrams/helloworld.bpmn")//載入資源檔,一次只能載入一個檔案.addClasspathResource("diagrams/helloworld.png")//.deploy();//完成部署System.out.println("部署ID:"+deployment.getId());//1System.out.println("部署時間:"+deployment.getDeploymentTime());}}

         

       2.InputStream方式


        使用InputStream方式部署流程資源需要傳入一個輸入資料流及資源的名稱,輸入資料流的來源不限,可以從classpath讀取,也可以從一個絕對路徑檔案讀取,也可以是從網路上讀取。

//Inputstream方式@Testpublic void deployementProcessDefinitionByInputStream() throws FileNotFoundException{//擷取資源相對路徑String bpmnPath = "diagrams/helloworld.bpmn";String pngPath = "diagrams/helloworld.png";//讀取資源作為一個輸入資料流FileInputStream bpmnfileInputStream = new FileInputStream(bpmnPath);FileInputStream pngfileInputStream = new FileInputStream(pngPath);Deployment deployment = processEngine.getRepositoryService()//擷取流程定義和部署對象相關的Service.createDeployment()//建立部署對象.addInputStream("helloworld.bpmn",bpmnfileInputStream).addInputStream("helloworld.png", pngfileInputStream).deploy();//完成部署System.out.println("部署ID:"+deployment.getId());//1System.out.println("部署時間:"+deployment.getDeploymentTime());}


        3.字串方式


        利用字串方式可以直接傳入純文字作為資源的來源,和前兩種方式類似,字串方式的實現原理是把一組字串的內容轉化為位元組流後再部署。

//字串方式@Testpublic void deployementProcessDefinitionByString() throws FileNotFoundException{//字串String text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><definitions>...</definitions>";Deployment deployment = processEngine.getRepositoryService()//擷取流程定義和部署對象相關的Service.createDeployment()//建立部署對象.addString("helloworld.bpmn",text).deploy();//完成部署System.out.println("部署ID:"+deployment.getId());//1System.out.println("部署時間:"+deployment.getDeploymentTime());}


                  4.zip/bar格式壓縮包方式


      以上3種部署方式一次只能部署一個資源,除非執行多次deployment.addXxx()方法,如何一次部署多個資源呢。很簡單,是我們可以使用zip/bar格式壓縮包方式。將資源檔手動或使用Ant指令碼,打包副檔名可以是Activiti官方推薦的bar或普通的zip。


//部署流程定義(zip)@Testpublic void deployementProcessDefinitionByzip(){//從classpath路徑下讀取資源檔InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/helloworld.zip");ZipInputStream zipInputStream = new ZipInputStream(in);Deployment deployment = processEngine.getRepositoryService()//擷取流程定義和部署對象相關的Service.createDeployment()//建立部署對象.addZipInputStream(zipInputStream)//使用zip方式部署,將helloworld.bpmn和helloworld.png壓縮成zip格式的檔案.deploy();//完成部署System.out.println("部署ID:"+deployment.getId());//1System.out.println("部署時間:"+deployment.getDeploymentTime());}


    三、資料庫表變更


              在部署流程定義這一步,資料庫中操作三張表:


         1)ACT_RE_DEPLOYMENT(部署對象表)


         存放流程定義的顯示名和部署時間,每部署一次增加一條記錄


         


         2)ACT_RE_PROCDEF(流程定義表)


         存放流程定義的屬性資訊,部署每個新的流程定義都會在這張表中增加一條記錄。

              注意:當流程定義的key相同的情況下,使用的是版本升級

         

        

        3)ACT_GE_BYTEARRAY(資源檔表)


        儲存流程定義相關的部署資訊。即流程定義文檔的存放地。每部署一次就會增加兩條記錄,一條是關於bpmn規則檔案的,一條是圖片的(如果部署時只指定了bpmn一個檔案,activiti會在部署時解析bpmn檔案內容自動產生流程圖)。兩個檔案不是很大,都是以二進位形式儲存在資料庫中。


         
         
         4)ACT_GE_PROPERTY(主鍵產生表)


         主張表將產生下次流程部署的主鍵ID。

         


        



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.