paulwong

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

如果不结合其他的框架进行整个开发(如:spring、hibernate),JBPM4也有自己的一套IOC容器,能后将自己的服务配置到IOC容器中,能够很容易的运行容器所配置的服务,
这样它也能够在代码中减少一陀一陀的工厂类等代码的调用,降低了偶核性,但是如果结合spring框架来进行整个开发的话,那么就有两个容器,两个SessionFactory,但是系统中只考虑一个容器来
对服务进行管理, 那么我们就要将jbpm4的服务移植到spring的IOC容器中,让spring来进行统一管理,这样通过spring的容器来管理服务、事务。
在网上,不管是百度、还是谷歌 搜索了一下,截至到目前(2009-08-29),没有搜索出一个JBPM4结合其他框架的完成例子,都是一些简单的介绍,而且所有文章大同小异,虽然文章上千章,但是好像出自
一个之手,但是此人也只是大概讲解了一下,可能有些人会认为用JBPM4结合其他框架进行整合是非常简单,那么你的想法是错误的,JBPM4整合其他框架对JBPM4的IOC理解要非常清楚,同时,要对部分源代码进行了解,才可能一步一步的搭建起来框架,下面介绍下我以struts2+spring2.5+hibernate3.4+jbpm4来搭建的一个框架,并且成功部署的过程

ssh2的框架搭建我这里就不啰嗦了,我主要就这个框架的基础上来整合jbpm4

第一步:加入jbpm4的jpdl插件,这个步骤非常简单,但是可能会出现下面的错误
    把jbpm4加入到项目中遇到的问题:
     无法导入插件jpdl(导入插件后没有出现可视化流程操作器的入口,无法进行可视化流程建立)
    解决方法:
    先前本人使用myeclipse6.0.1,在此版本上本人做过JBPM3的项目,在做JBPM4时,很自然的想到在该IDE上进行JBPM4的开发,但是一直都无法成功江JBPM4的jpdl插件安装成功,因此
    不断的换各种安装插件的方式,浪费了很多时间,后无意在myclipse7.1.1中尝试安装jpdl插件,竟然成功,后才发现myeclipse6不支持jbpm4,最终本人选择IDE myclipse7.1.1来进行
    开发.

 


第二步:创建JBPM4的数据库文件、JBPM4相关jar包、JBPM4的相关配置文件
    数据库文件:解压下载的jbpm4.0, jbpm4.0根目录下的db\schema.scripts目录下,这里存放的是jbpm4数据库文件的脚本,根据自己的需要选择相应数据库脚本文件进行导入

   JBPM4的jar包:jbpm4.0根目录下jbpm.jar

                       jbpm4-spring.jar,这两个jar文件内容大致相同,但是某些类也是对方所不具有的

                       lib目下的相关jar包(不要全部导入,因为这里包含hibernate,还有一些公共的jar包,可能你的工程中已经含有这些jar包,根据需要导入相关的jar包即可)

JBPM4的相关配置文件:

鉴于大家jbpm对配置文件位置的存放不清楚,以及因为版本的不同,造成文件内容和文件名不一样,所以对需要在项目中导入的文件及内容(基本配置)做了一个总结:

jbpm.cfg.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>  
  
<jbpm-configuration>  
  
  
<import resource="jbpm.spring.default.cfg.xml" />  
  
<import resource="jbpm.spring.hibernate.cfg.xml" />  
  
<import resource="jbpm.jpdl.cfg.xml" />  
  
<import resource="jbpm.identity.cfg.xml" />  
  
  
<!-- Job executor is excluded for running the example test cases. -->  
  
<!-- To enable timers and messages in production use, this should be included. -->  
  
<!--  
  <import resource="jbpm.jobexecutor.cfg.xml" />  
  
-->  
  
  
<import resource="jbpm.mail.templates.examples.xml" />  
  
  
</jbpm-configuration>  


jbpm.mail.properties,文件内容:

mail.smtp.host  localhost   
mail.smtp.port  2525   
mail.from       noreply@jbpm.org  


jbpm.mail.templates.examples.xml,文件内容:

<?xml version="1.0" encoding="UTF-8"?>  
  
<jbpm-configuration>  
  
  
<process-engine-context>  
  
    
<mail-template name="rectify-template">  
      
<to addresses="${addressee}" />  
      
<cc users="bb" groups="innerparty" />  
      
<bcc groups="thinkpol" />  
      
<subject>rectify ${newspaper}</subject>  
      
<text>${newspaper} ${date} ${details}</text>  
    
</mail-template>  
  
  
</process-engine-context>  
  
</jbpm-configuration>  


jbpm.spring.default.cfg.xml,文件内容:



<?xml version="1.0" encoding="UTF-8"?>  
  
<jbpm-configuration>  
  
  
<process-engine-context>  
  
    
<repository-service />  
    
<repository-cache />  
    
<execution-service />  
    
<history-service />  
    
<management-service />  
    
<identity-service />  
    
<task-service />  
<!--   
    <hibernate-configuration>  
      <cfg resource="jbpm.hibernate.cfg.xml" />  
    </hibernate-configuration>  
  
    <hibernate-session-factory />  
-->  
    
<script-manager default-expression-language="juel"  
                    default-script-language
="juel"  
                    read-contexts
="execution, environment, process-engine"  
                    write-context
="">  
      
<script-language name="juel" factory="org.jbpm.pvm.internal.script.JuelScriptEngineFactory" />  
    
</script-manager>  
  
    
<authentication />  
  
    
<id-generator />  
    
<types resource="jbpm.variable.types.xml" />  
  
    
<address-resolver />  
  
    
<business-calendar>  
      
<monday    hours="9:00-12:00 and 12:30-17:00"/>  
      
<tuesday   hours="9:00-12:00 and 12:30-17:00"/>  
      
<wednesday hours="9:00-12:00 and 12:30-17:00"/>  
      
<thursday  hours="9:00-12:00 and 12:30-17:00"/>  
      
<friday    hours="9:00-12:00 and 12:30-17:00"/>  
      
<holiday period="01/07/2008 - 31/08/2008"/>  
    
</business-calendar>  
  
    
<mail-template name='task-notification'>  
      
<to users="${task.assignee}"/>  
      
<subject>${task.name}</subject>  
      
<text><![CDATA[Hi ${task.assignee},  
Task "${task.name}" has been assigned to you.  
${task.description}  
 
Sent by JBoss jBPM  
]]></text>  
    
</mail-template>  
  
    
<mail-template name='task-reminder'>  
      
<to users="${task.assignee}"/>  
      
<subject>${task.name}</subject>  
      
<text><![CDATA[Hey ${task.assignee},  
Do not forget about task "${task.name}".  
${task.description}  
 
Sent by JBoss jBPM  
]]></text>  
    
</mail-template>  
  
  
</process-engine-context>  
  
  
<transaction-context>  
    
<repository-session />  
    
<db-session />  
  
    
<message-session />  
    
<timer-session />  
    
<history-session />  
    
<mail-session>  
      
<mail-server>  
        
<session-properties resource="jbpm.mail.properties" />  
      
</mail-server>  
    
</mail-session>  
  
</transaction-context>  
  
</jbpm-configuration>  


jbpm.spring.hibernate.cfg.xml,文件内容:

<?xml version="1.0" encoding="UTF-8"?>  
  
<jbpm-configuration>  
  
  
<process-engine-context>  
    
<command-service>  
      
<retry-interceptor />  
      
<environment-interceptor />  
      
<spring-transaction-interceptor />  
    
</command-service>  
  
</process-engine-context>  
  
  
<transaction-context>  
    
<transaction />  
    
<hibernate-session />  
  
</transaction-context>  
  
</jbpm-configuration>  


logback.xml,文件内容:

<configuration>  
    
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
        
<layout class="ch.qos.logback.classic.PatternLayout">  
            
<Pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>  
<!--  
            <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>  
-->  
        
</layout>  
    
</appender>  
  
    
<root level="info">  
        
<appender-ref ref="STDOUT" />  
    
</root>  
</configuration>  


sql-error-codes.xml,文件内容:

该文件内容较多,搜索一下jbpm4.0包下的文件,应该都有

 

上述文件存放在src目录下

 

 

第三步:添加JBPM4的服务到spring IOC容器中
   1.jbpm.spring.default.cfg.xml(jbpm.spring.default.cfg.xml)配置文件进行修改,将下面的部分配置注释掉
    <hibernate-configuration>
      <cfg resource="jbpm.hibernate.cfg.xml" />
    </hibernate-configuration>
    <hibernate-session-factory />
   注释的部分是在jBPM4中创建了sessionFactory,因为已经有了spring管理sessionFactory,因此我们只需要一个sessionFactory。既然要将jBPM4与Spring的整合,那就希望由Spring来统一管理   sessionFactory和事务,在Spring的配置文件中构造一个sessionFactory。一切由它与外部交互.

   2.上面已经屏蔽了jbpm4的sessionFactory,那么此处要新建立一个ProcessEngineFactoryBean类,该类用来初始化JBPM4的相关配置文件、工作流引擎,并且把sessionFactory的创建交给spring的容器       进行管理,在ProcessFactoryBean中创建一个SpringConfiguration,然后将sessionFactory放入 SpringConfiguration中,再从SpringConfiguration得到processEngine,代码如下:


public class ProcessEngineFactoryBean implements FactoryBean,   
    InitializingBean, DisposableBean, ApplicationContextAware 
{   
    
private String jbpmConfigurationLocation = "jbpm.cfg.xml";   
    
private SessionFactory sessionFactory;   
    
private ApplicationContext applicationContext;   
    
private ProcessEngine processEngine;//jbpm4的流程引擎接口,所有的功能服务都是从这里获取的,包括RespoistoryService、ExecutionService   
  
    
public Object getObject() throws Exception {   
        
return processEngine;   
    }
   
  
    @SuppressWarnings(
"unchecked")   
    
public Class getObjectType() {   
        
return ProcessEngine.class;   
    }
   
  
    
public boolean isSingleton() {   
        
return true;   
    }
   
  
    
public void afterPropertiesSet() {   
        SpringConfiguration cfg 
= new SpringConfiguration(jbpmConfigurationLocation);   
        cfg.setApplicationContext(applicationContext);   
        cfg.setSessionFactory(sessionFactory);   
        
this.processEngine = cfg.buildProcessEngine();   
    }
   
  
    
public void destroy() {   
        
this.processEngine = null;   
        
this.sessionFactory = null;   
    }
   
  
    
public void setJbpmConfigurationLocation(   
        String jbpmConfigurationLocation) 
{   
        
this.jbpmConfigurationLocation = jbpmConfigurationLocation;   
    }
   
  
    
public void setSessionFactory(SessionFactory sessionFactory) {   
        
this.sessionFactory = sessionFactory;   
    }
   
  
    
public void setApplicationContext(   
        ApplicationContext applicationContext) 
throws BeansException {   
        
this.applicationContext = applicationContext;   
    }
   
}
  

并且在Spring配置文件中加入如下配置:   

<bean id="processEngine" class="com.bestsoft.ssh.service.impl.flow.ProcessEngineFactoryBean">
       
<property name="sessionFactory" ref="sessionFactory"/> 
     
</bean>


通过上述我们创建了工作流引擎,那么这个时候我们可以通过配置来管理JBPM4中的服务(ProcessEngine接口中的服务,如:创建流程的服务、执行流程的服务等),这样做的目的是不想一陀一陀的工厂     类在代码中出现,由spring的注入功能来实现,如下配置(在spring配置文件中):


<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
    
<bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" />
    
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
    
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
    
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
    
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" /> 


添加JBPM4的服务到spring IOC容器中错误:
   注意:所有的jbpm4的配置文件必须在src目录下,并且不能有独立的目录,因为在初始化发布流程的代码中
ProcessEngine processEngine = new Configuration().buildProcessEngine();
因为JbpmConfiguration extends Configuration implements Context, ProcessEngine, EnvironmentFactory  因此会
调用JbpmConfiguration类中buildProcessEngine()方法来创建ProcessEngine来得到JBPM4的服务,在这个方法中,有这样一段代码


if (!isConfigured) {
      setResource("jbpm.cfg.xml");
    }


它会去src下找jbpm.cfg.xml文件,如果配置文件不在src目录下,那么就会找不到配置文件(jbpm.cfg.xml)

 

 

第四步:加入JBPM4的实体关系映射文件:



        <property name="mappingLocations">
            
<list>
                
<value>classpath*:jbpm.repository.hbm.xml</value>
                
<value>classpath*:jbpm.execution.hbm.xml</value>
                
<value>classpath*:jbpm.history.hbm.xml</value>
                
<value>classpath*:jbpm.task.hbm.xml</value>
                
<value>classpath*:jbpm.jpdl.hbm.xml</value>
                
<value>classpath*:jbpm.identity.hbm.xml</value>
            
</list>
        
</property>


可能会出现的问题: 启动中如果提示:找不到org.jbpm.jpdl.internal.model.JpdlExecution,那么这个时候,应该添加一个jbpm4-spring.jar文件,因为该jar中有此实体


第五步:
    请务必配置事务,JBPM4已经将事务改成从当前的Threadlocal中获得,所以jBPM4是必须当前有事务,如果我没有创建事务的话,在使用 processEngine时就会说事务没有启动。  

  

因为该文章是在成功运行后书写的,可能某些步骤漏写,或者描述不清楚,而无法成功整合,导致报错,请大家把报错信息贴出来,让我对文章作相应的修改,让参照着能够成功整合运行。

posted on 2009-09-07 00:46 paulwong 阅读(6864) 评论(7)  编辑  收藏 所属分类: JBPM

Feedback

# re: jbpm4整合struts2+spring2.5+hibernate3.3 2009-09-15 15:41 loong

哟西,不错哈。lz还不错哦  回复  更多评论   

# re: jbpm4整合struts2+spring2.5+hibernate3.3 2009-09-15 15:53 loong

请问楼主 jbpm.spring.default.cfg.xml文件 是你自己加的吗?我怎么找不到这个xml  回复  更多评论   

# re: jbpm4整合struts2+spring2.5+hibernate3.3 2009-11-05 16:54 nxwfy

请问,如果用entityManagerFactory而不是sessionFactoy管理的Spring文件怎么整合啊,谢谢。  回复  更多评论   

# re: jbpm4整合struts2+spring2.5+hibernate3.3 2009-11-08 12:36 gjhuai

jbpm4.1中SpringConfiguration没有这个方法

cfg.setSessionFactory(sessionFactory);

怎么办?  回复  更多评论   

# re: jbpm4整合struts2+spring2.5+hibernate3.3 2009-12-13 22:15 钟秀

我想请教一下,我在注入processEngine这个bean的时候,错误提示我有2个bean,而我在applicationContext.xml里面只有配置一个。像我注入responseService等却不会有这种错误,这是什么原因呢?  回复  更多评论   

# re: jbpm4整合struts2+spring2.5+hibernate3.3 2009-12-13 22:38 钟秀

我的配置和你的配置差不多,但是我却在注入processEngine的时候提示错误:No unique bean of type [org.jbpm.api.ProcessEngine] is defined: expected single matching bean but found 2: [jbpmConfiguration, processEngine],而注入responseService...却不会有这错误,想请你帮忙看下。  回复  更多评论   

# re: jbpm4整合struts2+spring2.5+hibernate3.3 2010-08-01 00:10 张伟林

关于JBPM4.4与SSH2之整合
请参见 http://zwllxs.javaeye.com/blog/726303
里面有每一步的详细讲解,并附上完整的可运行源码和所有jar包列表  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航: