现在流行抱大腿,不过对眼光的要求也颇高。要不就如高也,即使四眼,一样无用。对Java企业开发而言,Spring的腿则是一定要抱的。而所谓抱Spring的腿,无外乎三点:
一是通过Spring暴露出服务,将服务配置到Spring的IOC容器里;
二是在自己的运行环境里访问到Spring的IOC容器,能够轻松使用Spring容器里所配置的服务;
三是对于具有事务管理特性的项目来说,将事务管理与Spring的事务管理进行合并。
下面分别讨论:一、 通过Spring暴露服务还记得在jBPM4的运行期环境里提到的JbpmConfiguration吗?它是整个jBPM4的入口,并且是整个应用独此一份的。通过它可以获取processEngine,并藉此获得工作流引擎所提供的各种服务:
Java代码 ProcessEngine processEngine = new Configuration() .buildProcessEngine(); ProcessEngine processEngine = new Configuration() .buildProcessEngine();
Java代码 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暴露这些服务,配置如下:
Xml代码 <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里加入SpringContextjBPM4的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。
Java代码 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里干掉:
Xml代码 <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里:
Xml代码 <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提供的事务:
Xml代码 <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)强制从当前线程中获取:
Xml代码 <hibernate-session current="true"/> <hibernate-session current="true"/>
并干掉原有的事务实现:
Xml代码 <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流程文件名。
相关资源:Jbpm4.4 整合Spring Hibernate4