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

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

    博客分类:
  • SSH
阅读更多

如果不结合其他的框架进行整个开发(如: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代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <jbpm-configuration>  
  4.   
  5.   <import resource="jbpm.spring.default.cfg.xml" />  
  6.   <import resource="jbpm.spring.hibernate.cfg.xml" />  
  7.   <import resource="jbpm.jpdl.cfg.xml" />  
  8.   <import resource="jbpm.identity.cfg.xml" />  
  9.   
  10.   <!-- Job executor is excluded for running the example test cases. -->  
  11.   <!-- To enable timers and messages in production use, this should be included. -->  
  12.   <!--  
  13.   <import resource="jbpm.jobexecutor.cfg.xml" />  
  14.   -->  
  15.   
  16.   <import resource="jbpm.mail.templates.examples.xml" />  
  17.   
  18.   
  19. </jbpm-configuration>  
<?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,文件内容:

Xml代码 复制代码
  1. mail.smtp.host  localhost   
  2. mail.smtp.port  2525   
  3. mail.from       noreply@jbpm.org  
mail.smtp.host	localhost
mail.smtp.port	2525
mail.from		noreply@jbpm.org

 

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

 

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <jbpm-configuration>  
  4.   
  5.   <process-engine-context>  
  6.   
  7.     <mail-template name="rectify-template">  
  8.       <to addresses="${addressee}" />  
  9.       <cc users="bb" groups="innerparty" />  
  10.       <bcc groups="thinkpol" />  
  11.       <subject>rectify ${newspaper}</subject>  
  12.       <text>${newspaper} ${date} ${details}</text>  
  13.     </mail-template>  
  14.   
  15.   </process-engine-context>  
  16.   
  17. </jbpm-configuration>  
<?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代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <jbpm-configuration>  
  4.   
  5.   <process-engine-context>  
  6.   
  7.     <repository-service />  
  8.     <repository-cache />  
  9.     <execution-service />  
  10.     <history-service />  
  11.     <management-service />  
  12.     <identity-service />  
  13.     <task-service />  
  14. <!--   
  15.     <hibernate-configuration>  
  16.       <cfg resource="jbpm.hibernate.cfg.xml" />  
  17.     </hibernate-configuration>  
  18.   
  19.     <hibernate-session-factory />  
  20. -->  
  21.     <script-manager default-expression-language="juel"  
  22.                     default-script-language="juel"  
  23.                     read-contexts="execution, environment, process-engine"  
  24.                     write-context="">  
  25.       <script-language name="juel" factory="org.jbpm.pvm.internal.script.JuelScriptEngineFactory" />  
  26.     </script-manager>  
  27.   
  28.     <authentication />  
  29.   
  30.     <id-generator />  
  31.     <types resource="jbpm.variable.types.xml" />  
  32.   
  33.     <address-resolver />  
  34.   
  35.     <business-calendar>  
  36.       <monday    hours="9:00-12:00 and 12:30-17:00"/>  
  37.       <tuesday   hours="9:00-12:00 and 12:30-17:00"/>  
  38.       <wednesday hours="9:00-12:00 and 12:30-17:00"/>  
  39.       <thursday  hours="9:00-12:00 and 12:30-17:00"/>  
  40.       <friday    hours="9:00-12:00 and 12:30-17:00"/>  
  41.       <holiday period="01/07/2008 - 31/08/2008"/>  
  42.     </business-calendar>  
  43.   
  44.     <mail-template name='task-notification'>  
  45.       <to users="${task.assignee}"/>  
  46.       <subject>${task.name}</subject>  
  47.       <text><![CDATA[Hi ${task.assignee},  
  48. Task "${task.name}" has been assigned to you.  
  49. ${task.description}  
  50.  
  51. Sent by JBoss jBPM  
  52. ]]></text>  
  53.     </mail-template>  
  54.   
  55.     <mail-template name='task-reminder'>  
  56.       <to users="${task.assignee}"/>  
  57.       <subject>${task.name}</subject>  
  58.       <text><![CDATA[Hey ${task.assignee},  
  59. Do not forget about task "${task.name}".  
  60. ${task.description}  
  61.  
  62. Sent by JBoss jBPM  
  63. ]]></text>  
  64.     </mail-template>  
  65.   
  66.   </process-engine-context>  
  67.   
  68.   <transaction-context>  
  69.     <repository-session />  
  70.     <db-session />  
  71.   
  72.     <message-session />  
  73.     <timer-session />  
  74.     <history-session />  
  75.     <mail-session>  
  76.       <mail-server>  
  77.         <session-properties resource="jbpm.mail.properties" />  
  78.       </mail-server>  
  79.     </mail-session>  
  80.   </transaction-context>  
  81.   
  82. </jbpm-configuration>  
<?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代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <jbpm-configuration>  
  4.   
  5.   <process-engine-context>  
  6.     <command-service>  
  7.       <retry-interceptor />  
  8.       <environment-interceptor />  
  9.       <spring-transaction-interceptor />  
  10.     </command-service>  
  11.   </process-engine-context>  
  12.   
  13.   <transaction-context>  
  14.     <transaction />  
  15.     <hibernate-session />  
  16.   </transaction-context>  
  17.   
  18. </jbpm-configuration>  
<?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,文件内容:

Xml代码 复制代码
  1. <configuration>  
  2.     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
  3.         <layout class="ch.qos.logback.classic.PatternLayout">  
  4.             <Pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</Pattern>  
  5. <!--  
  6.             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>  
  7. -->  
  8.         </layout>  
  9.     </appender>  
  10.   
  11.     <root level="info">  
  12.         <appender-ref ref="STDOUT" />  
  13.     </root>  
  14. </configuration>  
<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,代码如下:

Java代码 复制代码
  1. public class ProcessEngineFactoryBean implements FactoryBean,   
  2.     InitializingBean, DisposableBean, ApplicationContextAware {   
  3.     private String jbpmConfigurationLocation = "jbpm.cfg.xml";   
  4.     private SessionFactory sessionFactory;   
  5.     private ApplicationContext applicationContext;   
  6.     private ProcessEngine processEngine;//jbpm4的流程引擎接口,所有的功能服务都是从这里获取的,包括RespoistoryService、ExecutionService   
  7.   
  8.     public Object getObject() throws Exception {   
  9.         return processEngine;   
  10.     }   
  11.   
  12.     @SuppressWarnings("unchecked")   
  13.     public Class getObjectType() {   
  14.         return ProcessEngine.class;   
  15.     }   
  16.   
  17.     public boolean isSingleton() {   
  18.         return true;   
  19.     }   
  20.   
  21.     public void afterPropertiesSet() {   
  22.         SpringConfiguration cfg = new SpringConfiguration(jbpmConfigurationLocation);   
  23.         cfg.setApplicationContext(applicationContext);   
  24.         cfg.setSessionFactory(sessionFactory);   
  25.         this.processEngine = cfg.buildProcessEngine();   
  26.     }   
  27.   
  28.     public void destroy() {   
  29.         this.processEngine = null;   
  30.         this.sessionFactory = null;   
  31.     }   
  32.   
  33.     public void setJbpmConfigurationLocation(   
  34.         String jbpmConfigurationLocation) {   
  35.         this.jbpmConfigurationLocation = jbpmConfigurationLocation;   
  36.     }   
  37.   
  38.     public void setSessionFactory(SessionFactory sessionFactory) {   
  39.         this.sessionFactory = sessionFactory;   
  40.     }   
  41.   
  42.     public void setApplicationContext(   
  43.         ApplicationContext applicationContext) throws BeansException {   
  44.         this.applicationContext = applicationContext;   
  45.     }   
  46. }  
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时就会说事务没有启动。  

  

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics