首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

JBPM 工作流引擎實戰指南:流程定義、部署與任務處理

     編輯:Java 相關知識

JBPM 簡介

JBPM (JBoss Process Management) 是 JBoss 推出的開源工作流引擎,用於將業務流程(如請假、報銷、出差申請、保險理賠)通過程序化方式描述、執行與管理。工作流本質是業務流程自動化,而非單純技術概念。

環境搭建與配置

開發環境準備

  1. 安裝 JBPM 插件(Eclipse/MyEclipse),支持流程圖可視化建模
  2. 導入 JBPM 運行時依賴:jbpm.jar、Hibernate 相關包、antlr 等
  3. 配置數據庫:執行 JBPM 提供的建表 SQL 腳本創建流程引擎表

核心配置文件

<!-- jbpm.hibernate.cfg.xml -->
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:oracle:thin:@host:1521:SID</property>
    <property name="connection.username">user</property>
    <property name="connection.password">pass</property>
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <property name="show_sql">true</property>
    <!-- JBPM 核心映射文件 -->
    <mapping resource="jbpm.repository.hbm.xml"/>
    <mapping resource="jbpm.execution.hbm.xml"/>
    <mapping resource="jbpm.history.hbm.xml"/>
    <mapping resource="jbpm.task.hbm.xml"/>
    <mapping resource="jbpm.identity.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

流程定義建模

節點類型

流程定義示例(報銷流程)

<process key="reim" name="Reimbursement" xmlns="https://jbpm.org/4.4/jpdl">
  <start name="start1" g="173,72,48,48">
    <transition to="填寫報銷單" />
  </start>
  <task name="填寫報銷單" g="148,185,92,52">
    <assignment-handler class="com.kane.handler.InsertHandler" />
    <transition to="財務審核" />
  </task>
  <task name="財務審核" g="400,185,92,52">
    <assignment-handler class="com.kane.handler.FinancialHandler" />
    <transition name="通過" to="exclusive1" />
    <transition name="不通過" to="填寫報銷單" />
  </task>
  <task name="CEO審核" g="144,365,92,52">
    <assignment-handler class="com.kane.handler.CEOHandler" />
    <transition name="通過" to="end1" />
    <transition name="不通過" to="填寫報銷單" />
  </task>
  <decision name="exclusive1" g="421,281,48,48">
    <handler class="com.kane.handler.MoneyCheckHandler" />
    <transition name="超過2000" to="CEO審核" />
    <transition name="不到2000" to="end1" />
  </decision>
  <end name="end1" g="423,375,48,48" />
</process>

核心 Handler 實現

任務分配處理器

// 任務節點實現 AssignmentHandler
public class FinancialHandler implements AssignmentHandler {
    public void assign(Assignable assignable, ExecutionContext context) {
        // 邏輯:分配給角色為財務的用戶組
        assignable.setPooledActors(new String[]{"finance_role"});
    }
}

決策處理器

// 判斷節點實現 DecisionHandler
public class MoneyCheckHandler implements DecisionHandler {
    public String decide(ExecutionContext context) {
        Double money = (Double) context.getVariable("money");
        return (money > 2000) ? "超過2000" : "不到2000";
    }
}

流程部署與啟動

部署流程定義


public class ProcessDeployer {
    public static void deploy() {
        ProcessEngine engine = new Configuration().buildProcessEngine();
        NewDeployment deployment = engine.getRepositoryService().createDeployment();
        deployment.addResourceFromFile(new File("Reimbursement.jpdl.xml"));
        deployment.deploy();
        engine.close();
    }
}

啟動流程實例


ProcessEngine engine = new Configuration().buildProcessEngine();
ProcessInstance instance = engine.getExecutionService()
    .startProcessInstanceByKey("reim", variables);

任務查詢與處理

用戶任務查詢工具類


public class JbpmTaskUtil {
    private static ProcessEngine engine = new Configuration().buildProcessEngine();
    
    public static List getUserTasks(String userId) {
        List tasks = new ArrayList<>();
        // 個人任務
        tasks.addAll(engine.getTaskService().findPersonalTasks(userId));
        // 組任務(候選人組)
        tasks.addAll(engine.getTaskService().findGroupTasks(userId));
        return tasks;
    }
    
    public static void completeTask(String taskId, String outcome) {
        engine.getTaskService().completeTask(taskId, outcome);
    }
}

常見問題與解決

antlr.NoSuchMethodError

現象:java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I

原因:antlr 版本衝突,JBPM 依賴舊版 antlr,項目中引入了新版

解決:排除傳遞依賴中的新版 antlr,強制使用 JBPM 兼容版本(通常為 antlr-2.7.7)

<dependency>
  <groupId>org.jbpm</groupId>
  <artifactId>jbpm-jpdl</artifactId>
  <exclusions>
    <exclusion><groupId>antlr</groupId><artifactId>antlr</artifactId></exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>antlr</groupId>
  <artifactId>antlr</artifactId>
  <version>2.7.7</version>
</dependency>

數據庫表結構

業務表需與流程引擎表關聯,關鍵字段:

  • 業務主鍵(如 reimbursement.rid)
  • 流程實例 ID(關聯 jbpm_execution)
  • 任務 ID(關聯 jbpm_task)

最佳實踐建議

  1. 流程設計先行:業務確認流程圖後再建模,避免返工
  2. Handler 解耦:業務邏輯放在 Service 層,Handler 只做參數轉換與調用
  3. 版本管理:流程定義文件納入版本控制,部署記錄版本號
  4. 異常處理:任務處理異常時提供重試、退回、人工干預機制
  5. 監控告警:監控長時間未處理任務、流程卡頓情況

總結

JBPM 提供了完整的工作流建模、部署、執行、監控能力。核心在於流程定義(JPDL/XML)、任務分配(AssignmentHandler)、決策路由(DecisionHandler)與業務系統集成。雖然 JBPM 4.x 已較老舊,但其核心思想(流程與業務分離、可視化建模、狀態機驅動)在現代工作流引擎(Activiti、Flowable、Camunda)中得以延續。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
節點類型說明
Start開始節點流程入口,唯一
End結束節點流程出口,可多個
Task任務節點人工處理環節,需指定處理人
Decision判斷節點條件分支,需實現 DecisionHandler