如果不结合其他的框架进行整个开发(如: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"?>
<?xml version="1.0" encoding="UTF-8"?>  
 
  
 <jbpm-configuration>
<jbpm-configuration>  
 
  
 <import resource="jbpm.spring.default.cfg.xml" />
  <import resource="jbpm.spring.default.cfg.xml" />  
 <import resource="jbpm.spring.hibernate.cfg.xml" />
  <import resource="jbpm.spring.hibernate.cfg.xml" />  
 <import resource="jbpm.jpdl.cfg.xml" />
  <import resource="jbpm.jpdl.cfg.xml" />  
 <import resource="jbpm.identity.cfg.xml" />
  <import resource="jbpm.identity.cfg.xml" />  
 
  
 <!-- Job executor is excluded for running the example test cases. -->
  <!-- Job executor is excluded for running the example test cases. -->  
 <!-- To enable timers and messages in production use, this should be included. -->
  <!-- To enable timers and messages in production use, this should be included. -->  
 <!--
  <!--  
 <import resource="jbpm.jobexecutor.cfg.xml" />
  <import resource="jbpm.jobexecutor.cfg.xml" />  
 -->
  -->  
 
  
 <import resource="jbpm.mail.templates.examples.xml" />
  <import resource="jbpm.mail.templates.examples.xml" />  
 
  
 
  
 </jbpm-configuration>
</jbpm-configuration>  

jbpm.mail.properties,文件内容:
 mail.smtp.host  localhost
mail.smtp.host  localhost   
 mail.smtp.port  2525
mail.smtp.port  2525   
 mail.from       noreply@jbpm.org
mail.from       noreply@jbpm.org  
jbpm.mail.templates.examples.xml,文件内容:
 <?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>  
 
  
 <jbpm-configuration>
<jbpm-configuration>  
 
  
 <process-engine-context>
  <process-engine-context>  
 
  
 <mail-template name="rectify-template">
    <mail-template name="rectify-template">  
 <to addresses="${addressee}" />
      <to addresses="${addressee}" />  
 <cc users="bb" groups="innerparty" />
      <cc users="bb" groups="innerparty" />  
 <bcc groups="thinkpol" />
      <bcc groups="thinkpol" />  
 <subject>rectify ${newspaper}</subject>
      <subject>rectify ${newspaper}</subject>  
 <text>${newspaper} ${date} ${details}</text>
      <text>${newspaper} ${date} ${details}</text>  
 </mail-template>
    </mail-template>  
 
  
 </process-engine-context>
  </process-engine-context>  
 
  
 </jbpm-configuration>
</jbpm-configuration>  

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

jbpm.spring.hibernate.cfg.xml,文件内容:
 <?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>  
 
  
 <jbpm-configuration>
<jbpm-configuration>  
 
  
 <process-engine-context>
  <process-engine-context>  
 <command-service>
    <command-service>  
 <retry-interceptor />
      <retry-interceptor />  
 <environment-interceptor />
      <environment-interceptor />  
 <spring-transaction-interceptor />
      <spring-transaction-interceptor />  
 </command-service>
    </command-service>  
 </process-engine-context>
  </process-engine-context>  
 
  
 <transaction-context>
  <transaction-context>  
 <transaction />
    <transaction />  
 <hibernate-session />
    <hibernate-session />  
 </transaction-context>
  </transaction-context>  
 
  
 </jbpm-configuration>
</jbpm-configuration>  

logback.xml,文件内容:
 <configuration>
<configuration>  
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
 <layout class="ch.qos.logback.classic.PatternLayout">
        <layout class="ch.qos.logback.classic.PatternLayout">  
 <Pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>
            <Pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>  
 <!--
<!--  
 <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
            <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>  
 -->
-->  
 </layout>
        </layout>  
 </appender>
    </appender>  
 
  
 <root level="info">
    <root level="info">  
 <appender-ref ref="STDOUT" />
        <appender-ref ref="STDOUT" />  
 </root>
    </root>  
 </configuration>
</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,
public class ProcessEngineFactoryBean implements FactoryBean,   

 InitializingBean, DisposableBean, ApplicationContextAware
    InitializingBean, DisposableBean, ApplicationContextAware  {
{   
 private String jbpmConfigurationLocation = "jbpm.cfg.xml";
    private String jbpmConfigurationLocation = "jbpm.cfg.xml";   
 private SessionFactory sessionFactory;
    private SessionFactory sessionFactory;   
 private ApplicationContext applicationContext;
    private ApplicationContext applicationContext;   
 private ProcessEngine processEngine;//jbpm4的流程引擎接口,所有的功能服务都是从这里获取的,包括RespoistoryService、ExecutionService
    private ProcessEngine processEngine;//jbpm4的流程引擎接口,所有的功能服务都是从这里获取的,包括RespoistoryService、ExecutionService   
 
  

 public Object getObject() throws Exception
    public Object getObject() throws Exception  {
{   
 return processEngine;
        return processEngine;   
 }
    }   
 
  
 @SuppressWarnings("unchecked")
    @SuppressWarnings("unchecked")   

 public Class getObjectType()
    public Class getObjectType()  {
{   
 return ProcessEngine.class;
        return ProcessEngine.class;   
 }
    }   
 
  

 public boolean isSingleton()
    public boolean isSingleton()  {
{   
 return true;
        return true;   
 }
    }   
 
  

 public void afterPropertiesSet()
    public void afterPropertiesSet()  {
{   
 SpringConfiguration cfg = new SpringConfiguration(jbpmConfigurationLocation);
        SpringConfiguration cfg = new SpringConfiguration(jbpmConfigurationLocation);   
 cfg.setApplicationContext(applicationContext);
        cfg.setApplicationContext(applicationContext);   
 cfg.setSessionFactory(sessionFactory);
        cfg.setSessionFactory(sessionFactory);   
 this.processEngine = cfg.buildProcessEngine();
        this.processEngine = cfg.buildProcessEngine();   
 }
    }   
 
  

 public void destroy()
    public void destroy()  {
{   
 this.processEngine = null;
        this.processEngine = null;   
 this.sessionFactory = null;
        this.sessionFactory = null;   
 }
    }   
 
  
 public void setJbpmConfigurationLocation(
    public void setJbpmConfigurationLocation(   

 String jbpmConfigurationLocation)
        String jbpmConfigurationLocation)  {
{   
 this.jbpmConfigurationLocation = jbpmConfigurationLocation;
        this.jbpmConfigurationLocation = jbpmConfigurationLocation;   
 }
    }   
 
  

 public void setSessionFactory(SessionFactory sessionFactory)
    public void setSessionFactory(SessionFactory sessionFactory)  {
{   
 this.sessionFactory = sessionFactory;
        this.sessionFactory = sessionFactory;   
 }
    }   
 
  
 public void setApplicationContext(
    public void setApplicationContext(   

 ApplicationContext applicationContext) throws BeansException
        ApplicationContext applicationContext) throws BeansException  {
{   
 this.applicationContext = applicationContext;
        this.applicationContext = applicationContext;   
 }
    }   
 }
}  
并且在Spring配置文件中加入如下配置:   
 <bean id="processEngine" class="com.bestsoft.ssh.service.impl.flow.ProcessEngineFactoryBean">
<bean id="processEngine" class="com.bestsoft.ssh.service.impl.flow.ProcessEngineFactoryBean">
 <property name="sessionFactory" ref="sessionFactory"/>
       <property name="sessionFactory" ref="sessionFactory"/> 
 </bean>
     </bean>
通过上述我们创建了工作流引擎,那么这个时候我们可以通过配置来管理JBPM4中的服务(ProcessEngine接口中的服务,如:创建流程的服务、执行流程的服务等),这样做的目的是不想一陀一陀的工厂     类在代码中出现,由spring的注入功能来实现,如下配置(在spring配置文件中):
 <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
 <bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" />
    <bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" />
 <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
 <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
    <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
 <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
 <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
    <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) {
if (!isConfigured) {
 setResource("jbpm.cfg.xml");
      setResource("jbpm.cfg.xml");
 }
    }

它会去src下找jbpm.cfg.xml文件,如果配置文件不在src目录下,那么就会找不到配置文件(jbpm.cfg.xml)
 
 
第四步:加入JBPM4的实体关系映射文件:
 <property name="mappingLocations">
        <property name="mappingLocations">
 <list>
            <list>
 <value>classpath*:jbpm.repository.hbm.xml</value>
                <value>classpath*:jbpm.repository.hbm.xml</value>
 <value>classpath*:jbpm.execution.hbm.xml</value>
                <value>classpath*:jbpm.execution.hbm.xml</value>
 <value>classpath*:jbpm.history.hbm.xml</value>
                <value>classpath*:jbpm.history.hbm.xml</value>
 <value>classpath*:jbpm.task.hbm.xml</value>
                <value>classpath*:jbpm.task.hbm.xml</value>
 <value>classpath*:jbpm.jpdl.hbm.xml</value>
                <value>classpath*:jbpm.jpdl.hbm.xml</value>
 <value>classpath*:jbpm.identity.hbm.xml</value>
                <value>classpath*:jbpm.identity.hbm.xml</value>
 </list>
            </list>
 </property>
        </property>
可能会出现的问题: 启动中如果提示:找不到org.jbpm.jpdl.internal.model.JpdlExecution,那么这个时候,应该添加一个jbpm4-spring.jar文件,因为该jar中有此实体
第五步:
    请务必配置事务,JBPM4已经将事务改成从当前的Threadlocal中获得,所以jBPM4是必须当前有事务,如果我没有创建事务的话,在使用 processEngine时就会说事务没有启动。   
  
因为该文章是在成功运行后书写的,可能某些步骤漏写,或者描述不清楚,而无法成功整合,导致报错,请大家把报错信息贴出来,让我对文章作相应的修改,让参照着能够成功整合运行。