Vanlin Study Club

          Java Js Flex

          jBPM4與Spring的集成[轉載]

                 現在流行抱大腿,不過對眼光的要求也高。要不就如高也,即使四眼,一樣無用。對Java企業開發而言,Spring的腿則是一定要抱的。而所謂抱Spring的腿,無外乎三點:

          一是通過Spring暴露出服務,將服務配置到Spring的IOC容器里;
          二是在自己的運行環境里訪問到Spring的IOC容器,能夠輕松使用Spring容器里所配置的服務;
          三是對于具有事務管理特性的項目來說,將事務管理與Spring的事務管理進行合并。

                  下面分別討論:

          一、    通過Spring暴露服務
          還記得在jBPM4的運行期環境里提到的JbpmConfiguration嗎?它是整個jBPM4的入口,并且是整個應用獨此一份的。通過它可以獲取processEngine,并藉此獲得工作流引擎所提供的各種服務:

          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 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" />


          細心的你會發現,配置時使用了JbpmConfiguration 的子類SpringConfiguration。SpringConfiguration相比JbpmConfiguration有哪些增強呢,下面再講。總之,現在,就可以使用Spring來獲取或注入這些Jbpm4所提供的服務了。

          二、在environment里加入SpringContext
          jBPM4的environment(運行期環境)提供Engine IOC(process-engine-context)和Transaction IOC(transaction-context)。要想在運行期方便地訪問到Spring里所配置的服務,最直接的方法就是在environment里加入Spring IOC(applicationContext)的引用。
          SpringConfiguration即是對JbpmConfiguration增強了對Spring IOC的一個引用。
           
          SpringConfiguration是如何做到的呢?簡單,實現Spring的ApplicationContextAware接口,自動持有applicationContext,然后openEnvironment時將其加入environment。

          environment.setContext(new SpringContext(applicationContext));


          SpringContext是對applicationContext的簡單封裝。

          那么什么從Engine IOC移民到Spring IOC了呢?是的,最重要的就是Hibernate Session Factory。

          在jbpm.cfg.xml的process-engine-context里干掉:

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

              
          <hibernate-session-factory />

            
          相關配置挪動至Spring配置文件。

          三、    事務
          哪里有數據庫操作,哪里就有事務。對于嵌入式工作流而言,最重要的集成就是事務的集成。這里先分析jBPM4的事務實現,然后再介紹集成入Spring的事務實現。

          1、    Command模式
          jBPM4的邏輯實現采用了Command模式。
           
          采用Command模式后,jBPM4對CommandService構造攔截器(Interceptor)鏈,配置在jbpm.cfg.xml的process-engine-context里:
          <command-service>
                
          <retry-interceptor />
                
          <environment-interceptor />
                
          <standard-transaction-interceptor />
              
          </command-service>


          2、    原有的事務實現
          jBPM4原有的事務通過StandardTransactionInterceptor實現,在CommandService執行Command之前打開事務(實際委派Hibernate的事務管理),完成后提交/回滾。
           
          jBPM4的事務是基于Command的。

          3、    集成入Spring的事務實現
          Spring的事務是基于服務調用的。

          使jBPM4使用Spring提供的事務:
          <command-service>
                
          <retry-interceptor />
                
          <environment-interceptor />
                
          <spring-transaction-interceptor current="true" />
          </command-service>


          攔截器換用SpringTransactionInterceptor,SpringTransactionInterceptor從environment 提供的Spring IOC獲取PlatformTransactionManager,使用事務模板回調Command,事務傳播模式強制加入當前事務。

          同時,對hibernate session的配置(jbpm.cfg.xml的transaction-context)強制從當前線程中獲取:
          <hibernate-session current="true"/>

          并干掉原有的事務實現:
          <transaction />

          參考文檔:
          http://www.slideshare.net/guest8d4bce/spring-integration-with-jbpm4




          http://www.aygfsteel.com/ronghao 榮浩原創

          posted on 2009-10-11 10:09 vanlin 閱讀(239) 評論(0)  編輯  收藏 所屬分類: jbpm

          主站蜘蛛池模板: 景洪市| 和林格尔县| 惠水县| 德化县| 葫芦岛市| 新兴县| 高唐县| 德昌县| 建瓯市| 泸定县| 盈江县| 龙州县| 商洛市| 德昌县| 百色市| 会东县| 驻马店市| 遂宁市| 广东省| 平湖市| 抚松县| 建平县| 郯城县| 安图县| 宜君县| 饶河县| 山西省| 溆浦县| 浮山县| 徐闻县| 青铜峡市| 保康县| 边坝县| 滦南县| 津市市| 宜良县| 临漳县| 浦东新区| 凤冈县| 钦州市| 得荣县|