?現在流行抱大腿,不過對眼光的要求也頗高。要不就如高也,即使四眼,一樣無用。對Java企業開發而言,Spring的腿則是一定要抱的。而所謂抱Spring的腿,無外乎三點:
?
一是通過Spring暴露出服務,將服務配置到Spring的IOC容器里;
二是在自己的運行環境里訪問到Spring的IOC容器,能夠輕松使用Spring容器里所配置的服務;
三是對于具有事務管理特性的項目來說,將事務管理與Spring的事務管理進行合并。
????? 下面分別討論:
一、??? 通過Spring暴露服務
還記得在jBPM4的運行期環境里提到的JbpmConfiguration嗎?它是整個jBPM4的入口,并且是整個應用獨此一份的。通過它可以獲取processEngine,并藉此獲得工作流引擎所提供的各種服務:
- ProcessEngine?processEngine?=? new ?Configuration() ??
- ??????.buildProcessEngine();??
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();??
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" ? /> ??
<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));??
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 ? /> ??
<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 > ??
<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 > ??
<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" /> ??
<hibernate-session current="true"/>
?
并干掉原有的事務實現:
- < transaction ? /> ??
<transaction />
?
參考文檔:
http://www.slideshare.net/guest8d4bce/spring-integration-with-jbpm4
?jbpm4GA發布已有一個月了,作為jbpm的新手,發現關于jbpm4的資源太稀少了,本人把jbpm4與spring的整合過程發布一下。本人使用struts2+hibernate+spring整合環境。
??? 首先復制官方下載的壓縮包中jbpm.jar到項目中,并根據腳本創建數據庫表。
???? 其次在spring的配置文件applicationContext.xml中的sessionFactory中加入
??? <property name="configLocation" value="classpath:jbpm.hibernate.cfg.xml" />
??? <bean id="jbpmConfiguration" class="org.jbpm.pvm.internal.cfg.SpringConfiguration">??
???? <constructor-arg value="jbpm.cfg.xml" />
??? </bean>??
??? <bean id="processEngine" factory-bean="jbpmConfiguration" factory-method="buildProcessEngine" />
??? 把從官方下載的壓縮包里的jbpm.hibernate.cfg.xml、jbpm.cfg.xml、jbpm.mail.templates.examples.xml3個xml文件放入src中。
???? 然后再修改jbpm.hibernate.cfg.xml文件中的數據庫連接信息。
??? 其次strtus2的action文件中添加private SpringConfiguration jbpmConfiguration;并設定其get、set方法。
??? 在處理方法中添加:
???? ProcessEngine processEngine = jbpmConfiguration.buildProcessEngine();
??? RepositoryService repositoryService = processEngine.getRepositoryService();
??? String deploymentId = repositoryService.createDeployment()
?????? .addResourceFromClasspath("****.jpdl.xml").deploy();
??? repositoryService.deleteDeployment(deploymentId);
??? 這里的****是需要發布的jbpm流程文件名。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
