`
kyo100900
  • 浏览: 637877 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2,spring,jbpm实战——spring和jbpm的详细配置

阅读更多

<!----><!----> <!---->

Spring jbpm 整合还是挺复杂的,我把整合的过程以及遇到的一些问题和大家共同分享一下,有不当之处,希望大家指正。我的开发环境是xp3,我所用到的框架版本如下:

 

struts2.0.11并且降级支持JDK1.4

spring2.0,

spring-modules-jbpm31-0.6.jar
jbpm3.1.4

hibernate.3.2.3

 

 

附件就是源码,请参照里面的readme.txt进行相应的配置。

 

<!----><!----> <!---->

Spring 中配置 jbpm 所使用的 SessionFactory

 

Spring 其它该怎么配还是怎么配置,对于 jbpm 所依赖的 sessionFactory ,我是这么配置的:

 

 

 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="mappingLocations">
			<list>       
			    <!-- jbpm's hbm.xml -->
    				<value>classpath*:/org/jbpm/**/*.hbm.xml</value>
    			
    			<!-- other hbm.xml -->
    		<value>classpath*:/com/resoft/system/domain/*.hbm.xml</value>      
  		 	</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
				<prop key="show_sql">true</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
			</props>
		</property>
	</bean>

 

<!----><!----> <!---->

我看有的喜欢将配置

 

 

<property name="mappingLocations">
			<list>       
			    <!-- jbpm's 的hbm.xml -->
    				<value>classpath*:/org/jbpm/**/*.hbm.xml</value>
    			
    			<!-- 其它地方自己配置的 hbm.xml -->
    				<value>classpath*:/com/resoft/system/domain/*.hbm.xml</value>      
  		 	</list>
		</property>
 

 

<!----><!----> <!---->

配置成:

 

 

<property name="configLocations">       
 			 <list>       
    				<value>classpath:/hibernate.cfg.xml</value>  
  		   		......
  		 	</list>       
		</property>
 

 

<!----><!----> <!---->

其实也行,不过hibernate.cfg.xml 文件对于后续的 spring jbpm 整合也没有多大用处,尽量争取统一在一处配置即可。下面还会有详细说明。

还有一点就是本 demo 是一个很简单的整合实例,所以其它的 PO 对象所对应的表也和 jbpm 放在一起。实际用的时候,可能需要配置多个数据源,多个 sessionFactory ,并用 JTA 来进行全局事务处理。

 

 

<!----><!----> <!---->

开始在 Spring 中配置 jbpm

 

请注意,现在才是配置的开始,继续在spring的xml中配置

 

 

 

 

<!-- 关于jbpmConfiguration的配置 -->
	<bean id="jbpmConfiguration" class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean">
		<property name="sessionFactory" ref="sessionFactory" />
		<property name="configuration" value="classpath:jbpm.cfg.xml" />
		<!-- 第一处需要注意的地方 -->
		<property name="processDefinitions">
			<list>
				<ref local="holidayWorkflow" />
			</list>
		</property>
		<!-- 没什么好说的,是否需要重新建表 -->
		<property name="createSchema" value="${jbpmConfiguration.createSchema}" />
	</bean>


<bean id="holidayWorkflow" class="org.springmodules.workflow.jbpm31.definition.ProcessDefinitionFactoryBean">
	<!-- 第二处需要注意的地方 -->
		<property name="definitionLocation" value="classpath:jbmp/workflow/holiday/holiday.zip" />
	</bean>

	<bean id="holidayTemplate" class="org.springmodules.workflow.jbpm31.JbpmTemplate">
		<constructor-arg index="0" ref="jbpmConfiguration" />
		<!-- 第三处需要注意的地方 -->
		<constructor-arg index="1" ref="holidayWorkflow" />
	</bean>

 

 

<!----><!----> <!---->

先看

 

 

<!-- 第一处需要注意的地方 -->
		<property name="processDefinitions">
			<list>
				<ref local="holidayWorkflow" />
			</list>
		</property>

 

<!----><!----> <!---->

这样配置后,每次重新启动的时候,都会重新发布 list 里的流程,开发的时候建议这么做。

 

再看

 

 

<!-- 第二处需要注意的地方 -->
	<property name="definitionLocation" value="classpath:jbmp/workflow/holiday/holiday.zip" />
 

 

<!----><!----> <!---->

以前整合过的人非常奇怪我这个地方怎么是一个 zip ,而不是什么 processdefinition.xml 文件之类的。最开始的时候,我下载了 javaeye 中某个人写的代码,里面说流程发布一定要通过插件发布,不能通过代码, 后来慢慢在自己摸索中发现,这是 springmodule 的一个不完善的地方。所谓的插件形式发布,我看了代码,就是调用一个执行上传 zip 包的 servlet 。然后再通过 jbpm API zip 包中的 gpd.xml processdefinition.xml processimage.jpg 解析发布,最后可以在页面使用 jbmp 自带的 jsp tag 标签 ( 可以随便修改 ) ,将 processimage.jpg 所在流程的正确位置显示出来。

 

好了,下载 spring-module-jbmp3.1 的源码打开

org.springmodules.workflow.jbpm31.definition. ProcessDefinitionFactoryBean 类,大概在第 56 行左右的地方,将

 

 

InputStream inputStream = null;
		try {
			inputStream = this.definitionLocation.getInputStream();
			this.processDefinition = ProcessDefinition.parseXmlInputStream(inputStream);

		}
 

<!----><!----> <!---->

改成

 

 

ZipInputStream inputStream = null;
		try {
			inputStream = new ZipInputStream(definitionLocation.getURL().openStream());
			this.processDefinition = ProcessDefinition.parseParZipInputStream(inputStream);

		}

 

<!----><!----> <!---->

这样,就可以注入 zip 包了。

 

最后看看

 

 

<!-- 第三处需要注意的地方 -->
		<constructor-arg index="1" ref="holidayWorkflow" />
 

 

<!----><!----> <!---->

这里的 holidayWorkflow 就是上面配置的一个 processDefinition 。有的人说此处可有可无。我看了 JbpmTemplate 的源码,也确实存在有一个参数的构造函数:

 

 

public JbpmTemplate(JbpmConfiguration jbpmConfiguration) {
		this.jbpmConfiguration = jbpmConfiguration;
	}
 

 

<!----><!----> <!---->

但是如果你想使用这个 JbpmTemplate ,如果某个方法涉及到要使用 processDefinition ,那么就会抛出空指针异常。比如在 JbpmTemplate 源码中,大概第 230 行的如下方法:

 

 

 

public List findProcessInstances() {
		return (List) execute(new JbpmCallback() {

			public Object doInJbpm(JbpmContext context) {
				return context.getGraphSession().findProcessInstances(processDefinition.getId());//这里就使用到了processDefinition
			}
		});
	}
 

 

 

<!----><!----> <!---->

 

至于 jbpm 其它方面的配置可以参考 spring-module 的文档中第 9 章的  jBPM 3.1.x 。我这里简单说说。

 

比如我在 spring 中配置了如下 ApplyAssignmentHandler

 

 

<bean id="applyAssignmentHandler" class="com.resoft.workflow.ApplyAssignmentHandler">
		<property name="userService" ref="userService" />
	</bean>

 

<!----><!----> <!---->

这个时候在我的 ApplyAssignmentHandler 就可以享受 spring IoC 优势,帮我把我需要用的那个 userService 注入进去,供我调用:

 

 

public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception {
   	userService.find(hql); // 现在可以使用注入后的userService
		assignable.setActorId(Constants.USER);// 将任务分配给单个用户
	}
 

 

<!----><!----> <!---->

但是可别忘记了还得在你的流程定义配置文件 processdefinition.xml 中,也配置上

 

 

<assignment class="org.springmodules.workflow.jbpm31.JbpmHandlerProxy" config-type="bean">
				<factoryKey>jbpmConfiguration</factoryKey>
				<targetBean>applyAssignmentHandler</targetBean>
			</assignment>

 

 

<!----><!----><!----> <!---->

两边配置自然觉得还是有点麻烦,希望以后再简化争取一个地方配置就完事了。

 

 

 

关于 hibernate.cfg.xml 是否需要的详细分析

 

是时候谈谈 hibernate.cfg.xml 的时候了。

 

查看 jbpm-3.1.4 的源码。找到 org.jbpm.persistence.db.DbPersistenceServiceFactory 类,在大概 82 行左右的地方,有如下方法:

 

//得到jbpm的SessionFactory
public synchronized SessionFactory getSessionFactory() {
    if (sessionFactory==null) { //与spring整合后,这里永远不会为null

      if (sessionFactoryJndiName!=null) {
        log.debug("looking up hibernate session factory in jndi '"+sessionFactoryJndiName+"'");
        sessionFactory = (SessionFactory) JndiUtil.lookup(sessionFactoryJndiName, SessionFactory.class);
        
      } else {
        log.debug("building hibernate session factory");
        sessionFactory = getConfiguration().buildSessionFactory(); //注意这一句中的getConfiguration()
      }
    }
    return sessionFactory;
  }
 

<!----><!----> <!---->

该方法在上方第 59 行左右,我们看到了 getConfiguration() 方法:

 

 

public synchronized Configuration getConfiguration() {
    if (configuration==null) {
      String hibernateCfgXmlResource = null;
      if (JbpmConfiguration.Configs.hasObject("resource.hibernate.cfg.xml")) {
        hibernateCfgXmlResource = JbpmConfiguration.Configs.getString("resource.hibernate.cfg.xml"); //取得jbpm.cfg.xml中配置的hibernate.cfg.xml
      }
      String hibernatePropertiesResource = null;
      if (JbpmConfiguration.Configs.hasObject("resource.hibernate.properties")) {
        hibernatePropertiesResource = JbpmConfiguration.Configs.getString("resource.hibernate.properties"); //取得jbpm.cfg.xml中配置的hibernate.properties
      }
      configuration = HibernateHelper.createConfiguration(hibernateCfgXmlResource, hibernatePropertiesResource);
    }
    return configuration;
  }

 

 

<!----><!----> <!---->

因为 spring 已经为注入好了 SessionFactory ,所以 if (sessionFactory==null) 永远为 false ,那这又意味着什么呢?

还记得 classpath 下的 jbpm.cfg.xml ? 里面就有关于 hibernate.cfg.xml 的配置:

 

 

 

<string name="resource.hibernate.cfg.xml" value="hibernate.cfg.xml" />

 

<!----><!----> <!---->

但通过上面的源码,咱们还发现其实 hibernate.properties 也可以类似的定义如下:

 

 

<string name="resource.hibernate.properties" value="hibernate.properties" />
 

 

<!----><!----> <!---->

只不过大家一般都是直接照着把 default.jbpm.cfg.xml 的内容直接复制出来,而且一般配置了 hibernate.cfg.xml 就足够了。

 

最后因为 if (sessionFactory==null) 永远为 false ,所以 getConfiguration() 方法自始至终都不会被调用到,就算你在 jbpm.cfg.xml 配置了 jbpm.cfg.xmlresource.hibernate.cfg.xml,resource.hibernate.properties 也是没有作用的。因此 jbpm spring 整合后, hibernate.cfg.xml hibernate.propties 两个文件可以不要。 ( 有其它用途除外 )

 

一切似乎至此可以结束了,但我们还是忽悠了一个小的细节。

 

<!----><!----> <!---->

当我删除测试数据很多的数据库,想重新利用 jbpm 自动建库建表功能时 ( 只需要将下面的配置 value 设置为 true ) ,问题出现了。

 

 

<!-- 没什么好说的,是否需要重新建表 -->
		<property name="createSchema" value="true" />
 

 

 

<!----><!----> <!---->

控制台上出现:

 

 

Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found

 

<!----><!----> <!---->

难道上面我分析的不对, hibernate.cfg.xml 始终不能少。带着疑问,我继续打着断点,终于依次看到下述代码(注意,以下的跟踪需要 spring-module jbpm hibernate 的源码支持 ):

 

org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean 150

org.jbpm.JbpmConfiguration 410

org.jbpm.persistence.db.DbPersistenceServiceFactory 108

org.jbpm.persistence.db.DbPersistenceServiceFactory 69

最后来到 org.jbpm.db.hibernate.HibernateHelper 83 行,看到如下代码:

 

 

  if (cfgXmlResource!=null) {
      // use the configured file name
      log.debug("creating hibernate configuration resource '"+cfgXmlResource+"'");
      configuration.configure(cfgXmlResource);
    } else {
      log.debug("using default hibernate configuration resource (hibernate.cfg.xml)");
      configuration.configure();
    }
 

 

 

 

<!----><!----> <!---->

因为没有配置 hibernate.cfg.xml( jbpm.cfg.xml 中可以配置该文件路径 ) ,所以自然会调用 else 中的 configuration.configure() 。当我再次跟进去后,一切真相大白。在 org.hibernate.cfg.Configuration 类中的 1413 行出现了下面代码:

 

 

public Configuration configure() throws HibernateException {
		configure( "/hibernate.cfg.xml" );
		return this;
	}
 

 

<!----><!----> <!---->

可见,如果你想依赖于 jbpm 的建库功能, hibernate.cfg.xml 不能少。

 

最后结论,如果你的 jbpm 建库不依赖于它的 API hibernate.cfg.xml 可以不要,否则目前还是必须配置一下。

 

 

至此, spring jbpm 整合结束。

 

 

 

文章太长了,结合struts2的应用部分下次再补发吧。

 

 

 

28
2
分享到:
评论
7 楼 judykula 2010-01-20  
不知道你现在还看不看,关于你的

最后因为  if (sessionFactory==null) 永远为 false ,所以 getConfiguration() 方法自始至终都不会被调用到,就算你在 jbpm.cfg.xml 配置了 jbpm.cfg.xmlresource.hibernate.cfg.xml,resource.hibernate.properties 也是没有作用的

这段 我看着应该同意,可是我的ssh+jbpm中,通过spring可以读到数据库,说明spring+hibernate整合是成功的,但是运行jbpm的时候还是会读  
<string name="resource.hibernate.cfg.xml" value="hibernate.cfg.xml" /> 
,我把hibernate.cfg.xml换成applicationContext.xml后出现错:
16:12:24,531 [http-8080-3] DEBUG HibernateHelper : creating hibernate configuration resource 'applicationContext.xml'
16:12:24,531 [http-8080-3] INFO  Configuration : configuring from resource: applicationContext.xml
16:12:24,531 [http-8080-3] INFO  Configuration : Configuration resource: applicationContext.xml

您觉得是什么原因,知道的话 麻烦告诉我:QQ:16753810 十分感谢
6 楼 winhonourxzg 2009-09-17  
最好说下。你的事务是怎么样整合的。。期待
5 楼 popkar77 2009-03-03  
我希望您能讲一下怎么打开一个JBPM 事务,我的毕业设计就是做JBPM的。我最近为这件事苦恼了好多天了,希望能得到您的帮助。
我邮箱:popkar77@163.com QQ:470534800
4 楼 hyzhuhu 2009-02-26  
还原数据库时出错
Error 1064<42000> at line 1:you have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to yse near '锘?/*!40101 SET NAMES utf8*/' at line 1
3 楼 hyzhuhu 2009-02-26  
还原数据库时出错
2 楼 wj539h 2009-02-07  
superleo兄,你好!我一直关注你写的所有文章,对我都很有帮助,而且很多是关于底层的知识,让我受益匪浅,而且还有相关的一些具体项目,给我的工作带来了很大的帮助,希望你能多写一些比较好的文章,让广大的JAVA开发者一起进步,非常感谢!
1 楼 zhangying 2009-02-06  
很好..

相关推荐

    spring2_struts2_jbpm3.2.rar_jbpm struts2_jbpm-demo_jbpm3 spring_

    总结来说,Spring、Struts2与JBPM的整合利用了各自的优势,Spring提供了强大的依赖管理和事务控制,Struts2简化了Web应用的开发,而JBPM则实现了流程的可视化设计和自动化执行。通过熟练掌握这些技术的整合,开发者...

    精通eclipse整合web开发struts hibernate spring jbpm 第18章源代码(source )

    第18章的源代码可能涵盖了这些框架的集成和实际应用,包括如何配置Eclipse以支持这些框架,如何搭建Struts的Action和Result,如何利用Hibernate进行持久化操作,Spring如何管理bean并协调各个组件,以及如何在JBPM中...

    精通Eclipse整合Web开发——Struts,Spring,Hibernate,JBPM-part2

    精通Eclipse整合Web开发——Struts,Spring,Hibernate,JBPM 图书馆下载的,很不容易。 不是很清晰,如果介意的,请不要下载,不要拍砖。 总共有三部分。

    精通Eclipse整合Web开发——Struts,Spring,Hibernate,JBPM-part1

    精通Eclipse整合Web开发——Struts,Spring,Hibernate,JBPM 图书馆下载的,不是很清晰,如果介意的,请不要下载,不要拍砖。 总共有三部分。

    精通eclipse整合web开发struts hibernate spring jbpm 第20章源代码(部分)

    精通eclipse整合web开发struts hibernate spring jbpm 第20章源代码 文件审批系统 articleaudit.war (由于太大,一次上传不完,source和sql见本账号另外资源) 学习jbpm的好实例

    精通eclipse整合web开发struts hibernate spring jbpm 第20章源代码(source sql)

    精通eclipse整合web开发struts hibernate spring jbpm 第20章源代码 文件审批系统(source和sql) (由于太大,一次上传不完,articleaudit.war 见本账号另外资源) 学习jbpm的好实例

    spring2.5.4+hibernate3.2.6+struts2+jbpm3.2.2配置方法

    在这个配置中,我们将探讨如何将这些框架集成在一起,创建一个基于Spring 2.5.4、Hibernate 3.2.6、Struts2和jbpm3.2.2的应用。 1. **Spring框架**: - Spring 2.5.4作为应用的核心容器,负责管理应用的bean(对象...

    spring2.5.4+hibernate3.2.6+struts2+jbpm3.2.2收藏

    在构建企业级应用时,整合多个...整合这些框架后,开发人员可以利用它们的优势,如Spring的依赖注入和事务管理,Hibernate的数据持久化,Struts2的MVC结构,以及jbpm的流程管理能力,构建出高效、灵活的企业级应用。

    jbpm4整合struts2+spring2.5+hibernate3.3

    总结来说,jbpm4整合struts2+spring2.5+hibernate3.3是一项复杂的工作,涉及到多个层次的配置和集成。正确理解和配置每个框架的角色,以及它们之间的交互,是实现高效流程管理的关键。这需要开发者具备扎实的Java...

    jbpm4整合struts2+spring2[1].5+hibernate3.3

    整合jbpm4、Struts2、Spring和Hibernate是一个复杂的任务,需要对每个框架有深入的理解。正确配置和调试是成功整合的关键,同时也提高了系统的可维护性和扩展性。在实践中,开发者应根据具体需求灵活调整整合策略,...

    struts2+spring+hibernate+jbpm工作流实例

    Struts2、Spring、Hibernate和Jbpm是Java开发中常用的技术栈,它们结合在一起可以构建出高效、灵活的企业级应用。在这个“struts2+spring+hibernate+jbpm工作流实例”中,主要展示了如何利用这些技术实现一个公文...

    struts+hibernate+spring+jbpm智能工作流系统

    Struts、Hibernate、Spring 和 jBPM 是四个在企业级应用开发中广泛使用的开源框架,它们结合在一起可以构建出高效且灵活的智能工作流系统。下面将分别介绍这些技术及其在工作流系统中的作用。 **Struts** Struts 是...

    ssh-jbpm.rar_jbpm stru_jbpm-web-test_ssh_struts2_struts2 jbpm

    总的来说,这个压缩包是一个关于Struts2应用的实战案例,特别强调了与jbpm的集成,对于想要了解和学习SSH框架、Struts2特性和jbpm工作流程管理的开发者来说,这是一个宝贵的资源。通过研究这个项目,可以提升对Java ...

    spring+struts1+hibernate+jbpm实例

    标题中的"spring+struts1+hibernate+jbpm实例"揭示了这是一个基于Java技术栈的Web应用程序开发实例,其中涉及四大核心技术:Spring、Struts1、Hibernate和JBPM。接下来,我们将深入探讨这些技术及其在实际项目中的...

    Struts+Spring+Hibernate+Jbpm实现的OA办公系统

    本系统采用经典的技术架构——SSH+JBPM,即Struts、Spring、Hibernate和Jbpm的组合,构建了一个功能完善的办公自动化平台。 **Struts** 是一个用于构建MVC(Model-View-Controller)架构的Java框架,负责处理用户的...

    精通Eclipse整合Web开发——Struts,Spring,Hibernate,JBPM-part3

    精通Eclipse整合Web开发——Struts,Spring,Hibernate,JBPM 图书馆下载的,不是很清晰,如果介意的,请不要下载,不要拍砖。 总共有三部分。

    spring+struts+hibernate+jbpm +dwr整合开发的OA办公系统源码

    这是一个基于Java技术栈,采用Spring、Struts、Hibernate、jbpm和DWR等多种框架整合开发的OA(Office Automation)办公系统源码项目。这个系统的构建旨在实现企业的日常办公自动化,提高工作效率,简化工作流程,...

Global site tag (gtag.js) - Google Analytics