Spring, Springmodules, JBPM持久化集成--优雅的回调
Spring, Springmodules, JBPM持久化集成理解系列一
?
?
【本系列如需转载,请注明作者及出处】
?
本系列文章假设阅者具备以下知识:
1,ThreadLocal相关知识
2,Spring持久化,事务管理相关知识
3,Hibernate 持久化相关知识
4,Jbpm 持久化相关知识
5,Springmodules项目相关知识
6,相关J2EE知识
接触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"jndi-name="java:/comp/env/jdbc/moss" /--><bean id="resource.DataSource"/><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"ref="resource.DataSource" /><property name="configLocations"><list><value>classpath*:hibernate.cfg.xml</value></list></property></bean><!-- jBPM configuration --><bean id="resource.JbpmConfiguration"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"ref="resource.JbpmConfiguration" /></bean></beans>?public Object execute(final JbpmCallback callback) {final JbpmContext context = getContext();try {// use the hibernateTemplate is present and if neededif (hibernateTemplate != null && hasPersistenceService) {// use hibernate templatereturn 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 contextcontext.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);}}?
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;}