`
ginge
  • 浏览: 208625 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring, Springmodules, JBPM持久化集成--优雅的回调

    博客分类:
  • JBPM
阅读更多

Spring, Springmodules, JBPM持久化集成理解系列一

 

 

【本系列如需转载,请注明作者及出处】

 

本系列文章假设阅者具备以下知识:
1,ThreadLocal相关知识
2,Spring持久化,事务管理相关知识
3,Hibernate 持久化相关知识
4,Jbpm 持久化相关知识
5,Springmodules项目相关知识
6,相关J2EE知识


且具备以下材料:
1,Spring源代码
2,Hibernate源代码
3,Jbpm源代码
4,Springmodules源代码



接触JBPM始于两年多前,当时还在广州一个公司实习,公司打算研发ITIL相关的项目,于是就开始知道了工作流,也就开始接触了JBPM,但是对比过不同的工作流,还是觉得JBPM有前途,因为它隶属于JBOSS,社区活跃。当然,和其他JBOSS项目一样缺乏足够的支持,最有用的就是附带的User Guide Reference。


现在受Spring影响太深了,接触什么项目想到的首先就是尝试和Spring集成。接触JBPM也不例外,不过与JBPM和Spring的集成,已经有人做了,这个项目就是SpringModules-jbpm了,而且做的很优雅,充分体现了Spring提倡的Inversion of Control的美。


我尝试将这种美表述出来与大家分享,首先我会摘取Spring Modules手册里与JBPM集成的部分内容。跟着会引用一部分JBPM的手册里的相关章节和阅读一部分相关代码分析SpringModules集成的原理。最后谈一谈JBPM持久化与Spring事务管理的集成。相信能够让困惑于LazyLoading的朋友一些有益的启示。


一,SpringModules与JBPM的集成 
 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
	<!-- framework's contexts -->

	<bean id="resource.PropertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>



	<!-- JNDI DataSource for J2EE environments -->
	<!--jee:jndi-lookup id="dataSource" jndi-name="java:/comp/env/jdbc/moss" /-->
	<bean id="resource.DataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName"
			value="${appserver.jdbc.driverClassName}" />
		<property name="url" value="${appserver.jdbc.url}" />
		<property name="username" value="${appserver.jdbc.username}" />
		<property name="password" value="${appserver.jdbc.password}" />
		<property name="maxActive" value="${appserver.jdbc.maxActive}" />
		<property name="maxIdle" value="${appserver.jdbc.maxIdle}" />
		<property name="maxWait" value="${appserver.jdbc.maxWait}" />
		<property name="defaultAutoCommit"
			value="${appserver.jdbc.defaultAutoCommit}" />
		<property name="removeAbandoned"
			value="${appserver.jdbc.removeAbandoned}" />
		<property name="removeAbandonedTimeout"
			value="${appserver.jdbc.removeAbandonedTimeout}" />
	</bean>



	<!-- Hibernate SessionFactory -->
	<bean id="resource.SessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="resource.DataSource" />
		<property name="configLocations">
			<list>
				<value>classpath*:hibernate.cfg.xml</value>
			</list>
		</property>
	</bean>
	
	<!-- jBPM configuration -->
	<bean id="resource.JbpmConfiguration"
		class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean">
		<property name="configuration" value="classpath:jbpm.cfg.xml" />
		<property name="createSchema" value="false" />
		<property name="sessionFactory">
		  <ref local="resource.SessionFactory"/>
		</property>
	</bean>
	
	<!-- jBPM template -->
	<bean id="resource.JbpmTemplate"
		class="org.springmodules.workflow.jbpm31.JbpmTemplate">
		<constructor-arg index="0" ref="resource.JbpmConfiguration" />
	</bean>

</beans>
 

LocalJbpmConfigurationFactoryBean

最重要的就是LocalJbpmConfigurationFactoryBean了,相信Spring的用户对这样的Bean都很熟悉。根据jbpmConfiguration配置文件和提供的sessionFactory,它会创建一个可以与提供的流程定义协同工作的jBPM Configuration。sessionFactory属性可有可无,如果提供了,将会使用sessionFactory提供的session进行数据库操作。但是如果注入了sessionFactory,就可以重用Spring提供的事务管理架构,且不需要对jBPM做任何代码修改,还可以结合使用OpenSessionInView模式,好处显而易见吧。


JbpmTemplate and JbpmCallback

像Spring与Hibernate,JDBC的集成有相应的Template, Callback类一样,Spring与Jbpm的集成也有JbpmTemplate和JbpmCallback,且默认提供了一些便利方法供用户使用,例如findProcessInstance,findPooledTaskInstances,saveProcessInstance,signal等。

因为JbpmTemplate继承了JbpmAccessor,父类JbpmAccessor实现了InitializingBean,所以Spring在初始化这个JbpmTemplate时会调用afterPropertiesSet方法来配置JbpmTemplate所使用到的HibernateTemplate。JbpmAccessor 还做了一些访问异常的转换操作。



这两个类协同起来要完成的事情是,如果提供了SessionFactory或者
HibernateTemplate, 在JbpmCallback里的doInJbpm方法之前注入SessionFactory提供的hibernate session。如果没有提供SessionFactory,那么持久化的操作还是交回给Jbpm。如果存在SessionFactory, session的打开,关闭就由SessionFactory管理。否则由JBPM自身的持久化逻辑来维护。
这部分逻辑可以查看HibernateTemplate的execute方法:
public Object execute(final JbpmCallback callback) {
		final JbpmContext context = getContext();

		try {
			// use the hibernateTemplate is present and if needed
			if (hibernateTemplate != null && hasPersistenceService) {

				// use hibernate template
				return hibernateTemplate.execute(new HibernateCallback() {
					/**
					 * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
					 */
					public Object doInHibernate(Session session) throws HibernateException, SQLException {
						// inject the session in the context
						context.setSession(session);
						return callback.doInJbpm(context);
					}
				});
			}

			// plain callback invocation (no template w/ persistence)
			return callback.doInJbpm(context);

		}
		catch (JbpmException ex) {
			throw convertJbpmException(ex);
		}
		finally {
			releaseContext(context);
		}

	}

 

分享到:
评论
3 楼 logcos 2008-12-29  
如果不用JbpmTemplate,向下面这样调用对么?
	private JbpmConfiguration jbpmConfiguration;

	// 获取JbpmContext对象
	private JbpmContext getJbpmContext(){
		JbpmContext context = jbpmConfiguration.createJbpmContext();
		// 关键的一个步骤,我们需要将JBPM使用的session对象跟本系统所使用的
		// ssion对象整合起来。
		context.setSession(getSession());
		return context;
		
	}

	public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) {
		this.jbpmConfiguration = jbpmConfiguration;
	}
2 楼 ginge 2008-11-21  
它是给你用的
1 楼 rmn190 2008-11-19  
<bean id="resource.JbpmTemplate"   定义了但没有调用.

这是为什么?

相关推荐

Global site tag (gtag.js) - Google Analytics