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

jBPM 4 與 Spring 整合架構深度解析:服務暴露、環境整合與交易統一

     編輯:Java 相關知識

jBPM 4 與 Spring 整合的三大核心目標:**服務暴露至 Spring IOC**、**jBPM 環境整合 Spring Context**、**交易管理統一**。本文從架構層面深度解析 SpringConfiguration 如何增強 JbpmConfiguration、Environment 如何持有 Spring Context、Command 模式下的交易攔截器替換機制。

一、通過 Spring 暴露 jBPM 服務

標準服務獲取方式

ProcessEngine processEngine = new Configuration().buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ExecutionService executionService = processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
HistoryService historyService = processEngine.getHistoryService();
ManagementService managementService = processEngine.getManagementService();

Spring 配置暴露為 Bean

<bean id="jbpmConfiguration" class="org.jbpm.pvm.internal.cfg.SpringConfiguration">
  <constructor-arg value="be/inze/spring/demo/jbpm.cfg.xml"/>
</bean>

<bean id="processEngine" factory-bean="jbpmConfiguration" factory-method="buildProcessEngine"/>

<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
<bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService"/>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>

現在可直接 @Autowired RepositoryService repositoryService 注入使用。

二、SpringConfiguration:環境整合核心

繼承關係

JbpmConfiguration (jBPM 核心配置)
    ↑
SpringConfiguration (Spring 整合增強)
    - 實作 ApplicationContextAware
    - 持有 ApplicationContext 參考

核心邏輯:openEnvironment 時注入 SpringContext

public class SpringConfiguration extends JbpmConfiguration implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    protected Environment openEnvironment() {
        Environment env = super.openEnvironment();
        // 關鍵:將 Spring Context 封裝為 SpringContext 加入環境
        env.setContext(new SpringContext(applicationContext));
        return env;
    }
}

SpringContext 簡單封裝

public class SpringContext implements Context {
    private ApplicationContext applicationContext;
    public SpringContext(ApplicationContext ac) { this.applicationContext = ac; }
    public Object get(String name) { return applicationContext.getBean(name); }
    public void set(String name, Object value) { /* 不支援設定 */ }
    // ... 其他方法
}

Environment 整合 SpringContext 示意圖

三、移除 jBPM 內建 Hibernate SessionFactory

在 jbpm.cfg.xml 的 process-engine-context 中移除:

<hibernate-configuration>
  <cfg resource="jbpm.hibernate.cfg.xml"/>
</hibernate-configuration>
<hibernate-session-factory/>

改由 Spring 配置 sessionFactory 並注入 SpringConfiguration (參考方式一)。

四、交易整合:Command 模式與攔截器鏈

jBPM 4 Command 模式架構

所有業務操作封裝為 Command,由 CommandService 執行,通過攔截器鏈處理:

<command-service>
  <retry-interceptor/>
  <environment-interceptor/>
  <standard-transaction-interceptor/>  <!-- 原內建交易攔截器 -->
</command-service>

CommandService 攔截器鏈

原有交易實現:StandardTransactionInterceptor

  • 執行 Command 前:開啟 Hibernate 交易
  • 執行後:提交/回滾
  • 委派 Hibernate Transaction API

StandardTransactionInterceptor 流程

整合 Spring 交易:SpringTransactionInterceptor

<command-service>
  <retry-interceptor/>
  <environment-interceptor/>
  <spring-transaction-interceptor current="true"/>  <!-- 替換為 Spring 交易 -->
</command-service>

SpringTransactionInterceptor 核心邏輯

  1. 從 Environment 的 SpringContext 獲取 PlatformTransactionManager
  2. 使用 TransactionTemplate 回調執行 Command
  3. 交易傳播行為:PROPAGATION_MANDATORY (強制加入現有交易)

SpringTransactionInterceptor 流程

同步 Hibernate Session:強制從 ThreadLocal 獲取

在 transaction-context 中配置:

<hibernate-session current="true"/>  <!-- 從 TransactionSynchronizationManager 獲取 -->
<!-- 移除 <transaction/> -->

五、架構對比總結

層面jBPM 內建Spring 整合後
服務獲取程式碼 new Configuration()Spring IOC 注入
環境上下文僅 Engine/Transaction IOC+ Spring IOC (ApplicationContext)
SessionFactoryjBPM 內建管理Spring 統一管理
交易管理StandardTransactionInterceptor (Hibernate)SpringTransactionInterceptor (PlatformTransactionManager)
Session 綁定交易內部管理ThreadLocal (Spring 同步)
外部服務訪問困難 (需手動查 JNDI)直接 @Autowired 任意 Spring Bean

六、參考文獻

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved