JBPM工作流入门(上)

原创 2011年10月14日 12:42:23

  JBPM(java Business Process Management);jPDL - JBPM Process Definition Language

JBPM工作流入门:
按照此基本文档测试:


 

JBPM简要过程:

1、定义流程(利用JPDL)

2、部署流程(部署到数据库)

3、创建公文并与流程实例绑定

4、可通过JBPM的接口,触发流程向下流动

5、可通过JBPM的接口,获得流动到某个用户那里的文档(即待处理任务列表)

6、可通过JBPM的接口,结束某个用户的任务(这将触发流程继续向下流动)

7、如此,直到结束

 

----------------------------------------------

测试:

 

1、安装JBPM

       -引入Hibernate依赖包

       -引入JBPM依赖包

        * bsh.jar

        * jcr-1.0.jar

        * jbpm-identity.jar

        * jbpm-jpdl.jar

       -引入数据库驱动

        * mysql-connector-java-3.1.13-bin.jar

2、定义相关配置文件

       -Hibernate配置文件

        * 提供hibernate配置文件(可以从config/目录下拷贝,并修改其中的数据库连接设置即可)

3、假设现在有一个公文,需要经过:张三、李四、王五的审批之后才能结束

4、我们定义一个Document对象,及其hibernate映射,并将修改hibernate配置文件,将映射添加到其配置中(以便创建相应的数据库表)

5、现在让我们来测试一下:

       -创建数据库表:JbpmConfiguration.getInstance().createSchema();

       -定义流程: 参考process.xml

       -部署流程:

              *JbpmConfiguration.getInstance() - 创建jbpmConfiguration对象

              *ProcessDefinition.parseXmlResource(String); - 读取流程定义文件,创建processdefinition对象

              *jbpmConfiguration.createJbpmContext(); - 创建jbpmContext对象

              *context.deployProcessDefinition(definition); - 部署流程到数据库

              *context.close(); - 关闭context对象

       -创建公文

       -将公文与流程绑定(即需要创建流程实例)

              *JbpmConfiguration.getInstance() - 创建jbpmConfiguration对象

              *jbpmConfiguration.createJbpmContext(); - 创建jbpmContext对象

              *context.setSessionFactory(sessionFactory),将JBPM与程序中的session绑定

              *context.getGraphSession().findLatestProcessDefinition("流程名称");

              *new ProcessInstance(definition); - 创建流程实例

              *context.save(processInstance); - 存储流程实例

              *在Document中添加Long processInstanceId 属性

              *context.getSession().load 操作,加载Document对象

              *document.setProcessInstanceId - 绑定流程实例到公文

              *processInstance.getContextInstance.createVariable("document",document.getId())- 绑定公文到流程实例

       -公文创建者提交公文

              *(Document)context.getSession().load(Document.class, 1); - 加载公文信息

              *context.getProcessInstance(从公文中获取的流程实例ID); - 即根据流程实例ID加载流程实例

              *processInstance.getRootToken().signal(); - 触发流程往下走(即到达第一个节点)

       -这时候,我们可以测试一下,看看流程当前所处的节点

              *processInstance.getRootToken().getNode().getName()

       -第一个节点对应的用户登录,应该能够查询到其当前的任务(有公文等待其审批)

              *List tasks = context.getTaskMgmtSession().findTaskInstances("张三"); - 查找张三的任务列表

              *列表元素是TaskInstance实例

              *通过:taskInstance.getProcessInstance().getContextInstance().getVariable("document");可以找到其绑定的公文ID

       -查找到当前的任务对应的公文之后,即可对其审批,并继续往下走

              *taskInstance.end();

       -如此,直到结束

              *processInstance.hasEnded() - 如果流程已经到达终点,本调用将返回true



一.引入jar包:


二.添加hibernate.cfg.xml文件,可以到JBPM工程文件conf目录下去copy,然后修改,修改内容如下:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>

    <!-- hibernate dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- JDBC connection properties (begin) -->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jbpm</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123456</property>
    <!-- JDBC connection properties (end) -->
    
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    
    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
    
        <mapping resource="com/jbpm/model/Document.hbm.xml"/>
    
    <!-- DataSource properties (begin) ===
    <property name="hibernate.connection.datasource">java:/JbpmDS</property>
    ==== DataSource properties (end) -->
    
    <!-- JTA transaction properties (begin) ===
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
    <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
    ==== JTA transaction properties (end) -->

    <!-- CMT transaction properties (begin) ===
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
    <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
    ==== CMT transaction properties (end) -->

    <!-- logging properties (begin) ===
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.use_sql_comments">true</property>
    ==== logging properties (end) -->
    
    <!-- ############################################ -->
    <!-- # mapping files with external dependencies # -->
    <!-- ############################################ -->

    <!-- following mapping file has a dependendy on   -->
    <!-- 'bsh-{version}.jar'.                         -->
    <!-- uncomment this if you don't have bsh on your -->
    <!-- classpath.  you won't be able to use the     -->
    <!-- script element in process definition files   -->
    <mapping resource="org/jbpm/graph/action/Script.hbm.xml"/>

    <!-- following mapping files have a dependendy on  -->
    <!-- 'jbpm-identity.jar', mapping files            -->
    <!-- of the pluggable jbpm identity component.     -->
    <!-- Uncomment the following 3 lines if you        -->
    <!-- want to use the jBPM identity mgmgt           -->
    <!-- component.                                    -->
    <!-- identity mappings (begin) -->
    <mapping resource="org/jbpm/identity/User.hbm.xml"/>
    <mapping resource="org/jbpm/identity/Group.hbm.xml"/>
    <mapping resource="org/jbpm/identity/Membership.hbm.xml"/>
    <!-- identity mappings (end) -->
    
    <!-- following mapping files have a dependendy on  -->
    <!-- the JCR API                                   -->
    <!-- jcr mappings (begin) ===
    <mapping resource="org/jbpm/context/exe/variableinstance/JcrNodeInstance.hbm.xml"/>
    ==== jcr mappings (end) -->


    <!-- ###################### -->
    <!-- # jbpm mapping files # -->
    <!-- ###################### -->

    <!-- hql queries and type defs -->
    <mapping resource="org/jbpm/db/hibernate.queries.hbm.xml" />
    
    <!-- graph.def mapping files -->
    <mapping resource="org/jbpm/graph/def/ProcessDefinition.hbm.xml"/>
    <mapping resource="org/jbpm/graph/def/Node.hbm.xml"/>
    <mapping resource="org/jbpm/graph/def/Transition.hbm.xml"/>
    <mapping resource="org/jbpm/graph/def/Event.hbm.xml"/>
    <mapping resource="org/jbpm/graph/def/Action.hbm.xml"/>
    <mapping resource="org/jbpm/graph/def/SuperState.hbm.xml"/>
    <mapping resource="org/jbpm/graph/def/ExceptionHandler.hbm.xml"/>
    <mapping resource="org/jbpm/instantiation/Delegation.hbm.xml"/>

    <!-- graph.node mapping files -->
    <mapping resource="org/jbpm/graph/node/StartState.hbm.xml"/>
    <mapping resource="org/jbpm/graph/node/EndState.hbm.xml"/>
    <mapping resource="org/jbpm/graph/node/ProcessState.hbm.xml"/>
    <mapping resource="org/jbpm/graph/node/Decision.hbm.xml"/>
    <mapping resource="org/jbpm/graph/node/Fork.hbm.xml"/>
    <mapping resource="org/jbpm/graph/node/Join.hbm.xml"/>
    <mapping resource="org/jbpm/graph/node/State.hbm.xml"/>
    <mapping resource="org/jbpm/graph/node/TaskNode.hbm.xml"/>

    <!-- context.def mapping files -->
    <mapping resource="org/jbpm/context/def/ContextDefinition.hbm.xml"/>
    <mapping resource="org/jbpm/context/def/VariableAccess.hbm.xml"/>

    <!-- taskmgmt.def mapping files -->
    <mapping resource="org/jbpm/taskmgmt/def/TaskMgmtDefinition.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/def/Swimlane.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/def/Task.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/def/TaskController.hbm.xml"/>

    <!-- module.def mapping files -->
    <mapping resource="org/jbpm/module/def/ModuleDefinition.hbm.xml"/>

    <!-- bytes mapping files -->
    <mapping resource="org/jbpm/bytes/ByteArray.hbm.xml"/>

    <!-- file.def mapping files -->
    <mapping resource="org/jbpm/file/def/FileDefinition.hbm.xml"/>

    <!-- scheduler.def mapping files -->
    <mapping resource="org/jbpm/scheduler/def/CreateTimerAction.hbm.xml"/>
    <mapping resource="org/jbpm/scheduler/def/CancelTimerAction.hbm.xml"/>

    <!-- graph.exe mapping files -->
    <mapping resource="org/jbpm/graph/exe/Comment.hbm.xml"/>
    <mapping resource="org/jbpm/graph/exe/ProcessInstance.hbm.xml"/>
    <mapping resource="org/jbpm/graph/exe/Token.hbm.xml"/>
    <mapping resource="org/jbpm/graph/exe/RuntimeAction.hbm.xml"/>

    <!-- module.exe mapping files -->
    <mapping resource="org/jbpm/module/exe/ModuleInstance.hbm.xml"/>
        
    <!-- context.exe mapping files -->
    <mapping resource="org/jbpm/context/exe/ContextInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/TokenVariableMap.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/VariableInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/variableinstance/ByteArrayInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/variableinstance/DateInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/variableinstance/DoubleInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/variableinstance/HibernateLongInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/variableinstance/HibernateStringInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/variableinstance/LongInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/variableinstance/NullInstance.hbm.xml"/>
    <mapping resource="org/jbpm/context/exe/variableinstance/StringInstance.hbm.xml"/>

    <!-- job mapping files -->
    <mapping resource="org/jbpm/job/Job.hbm.xml"/>
    <mapping resource="org/jbpm/job/Timer.hbm.xml"/>
    <mapping resource="org/jbpm/job/ExecuteNodeJob.hbm.xml"/>
    <mapping resource="org/jbpm/job/ExecuteActionJob.hbm.xml"/>

    <!-- taskmgmt.exe mapping files -->
    <mapping resource="org/jbpm/taskmgmt/exe/TaskMgmtInstance.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/exe/TaskInstance.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/exe/PooledActor.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/exe/SwimlaneInstance.hbm.xml"/>

    <!-- logging mapping files -->
    <mapping resource="org/jbpm/logging/log/ProcessLog.hbm.xml"/>
    <mapping resource="org/jbpm/logging/log/MessageLog.hbm.xml"/>
    <mapping resource="org/jbpm/logging/log/CompositeLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/ActionLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/NodeLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/ProcessInstanceCreateLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/ProcessInstanceEndLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/ProcessStateLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/SignalLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/TokenCreateLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/TokenEndLog.hbm.xml"/>
    <mapping resource="org/jbpm/graph/log/TransitionLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/VariableLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/VariableCreateLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/VariableDeleteLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/VariableUpdateLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/variableinstance/ByteArrayUpdateLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/variableinstance/DateUpdateLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/variableinstance/DoubleUpdateLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/variableinstance/HibernateLongUpdateLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/variableinstance/HibernateStringUpdateLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/variableinstance/LongUpdateLog.hbm.xml"/>
    <mapping resource="org/jbpm/context/log/variableinstance/StringUpdateLog.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/log/TaskLog.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/log/TaskCreateLog.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/log/TaskAssignLog.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/log/TaskEndLog.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/log/SwimlaneLog.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/log/SwimlaneCreateLog.hbm.xml"/>
    <mapping resource="org/jbpm/taskmgmt/log/SwimlaneAssignLog.hbm.xml"/>
    
  </session-factory>
</hibernate-configuration>
三.添加log4j.properties文件,文件内容如下:

# Set root logger level to DEBUG and its only appender to CONSOLE.
log4j.rootLogger=DEBUG, CONSOLE

# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %C{1} : %m%n

# LIMIT CATEGORIES
#log4j.logger.org.jbpm=DEBUG
  
### log just the SQL  
#log4j.logger.org.hibernate.SQL=debug  
  
### log schema export/update ###  
log4j.logger.org.hibernate.hbm2ddl=debug  
  
log4j.logger.com.jbpm=DEBUG  
四.建立Document实体类,以便流程测试,Document.java和Document.hbm.xml文件如下:

package com.jbpm.model;

public class Document {
	private int id ;
	private String name ;
	private String creator ;
	………………//省略get,setXXX方法
	
}

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping package="com.jbpm.model">    
    <class name="Document" table="t_document">  
        <id name="id" column="id">  
            <generator class="native"/>  
        </id>  
        <property name="name" column="name"/>  
        <property name="creator" column="creator"/>    
    </class>    
</hibernate-mapping>  

到此,基本环境准备好了;由于篇幅太长,看着费劲,请查看JBPM工作流初步(下),正式开始测试JBPM流程,链接地址: http://blog.csdn.net/ziyunyangyong/article/details/6879078


工作流-jbpm入门例子

原文地址:http://blog.csdn.net/hxirui/article/details/1221911 1.      jBPM的简单介绍 jBPM是JBOSS下的一个开源java工作流项...
  • I_Am_Zou
  • I_Am_Zou
  • 2016-09-07 15:04:39
  • 7965

jbpm入门例子

1.      jBPM的简单介绍jBPM是JBOSS下的一个开源java工作流项目,该项目提供eclipse插件,基于Hibernate实现数据持久化存储。   参考http://www.jboss...
  • hxirui
  • hxirui
  • 2006-09-14 11:30:00
  • 138607

JBPM工作流demo--websale的开发 (1)

   本文是通过JBPM自带的websale这个demo来向大家演示jbpm工作流的开发过程.第一部分:开发前的准备工作:  1.下载JBPM开发包:这里需要两个jar包,到jboss的官方网站上去下...
  • mylifecool
  • mylifecool
  • 2006-08-16 17:05:00
  • 2467

jBPM4工作流示例jbpmDemo

  • 2017年02月16日 20:06
  • 176.92MB
  • 下载

jbpm工作流入门教程

jbpm入门例子新一篇: windows下svn-1.4.0环境布置和eclipse集成function StorePage(){d=document;t=d.selection?(d.selecti...
  • huxin1
  • huxin1
  • 2008-04-25 23:13:00
  • 17073

Java工作流引擎学习----JBPM(二)

四、Demo练习 创建一个java项目,名称:叫jbpm,在src目录下,创建一个demo.jpdl.xml 添加jar包+添加配置文件      将lib中的sub...以上的j...
  • qq_26504875
  • qq_26504875
  • 2016-01-09 10:58:53
  • 1282

【代码篇】JBPM4.4结合业务实现简单工作流

继博客【思想篇】工作流技术JBPM开发入门(三)的实现完成!应对业务的变化。   其实这次对于工作流的学习大概分了3主要个阶段:   1,结合业务开发一个简单的工作流,应对业务的变更 2,结合业务开发...
  • hejingyuan6
  • hejingyuan6
  • 2015-01-13 21:48:35
  • 4792

Java工作流引擎:jBPM、Activiti以及SWF

http://zhouliang.pro/2013/07/06/bpm-jbpm-activiti/ 这只是一篇非常粗浅的记录我对工作流引擎认识的文章。 知道工作流引擎是很久之前了,但是一直都...
  • liangyixin19800304
  • liangyixin19800304
  • 2013-10-15 23:58:28
  • 3221

JBPM学习(一):实现一个简单的工作流例子全过程

最近发现这篇文章挺多人看的,也挺多人有疑问,我很想帮你们解答,但是很无奈,这篇文章只是我当时在自学时看的一个教程的记录,当时对JBPM也没有深入去学习,并且到现在也已经快4年了,这期间我都没用过JBP...
  • v123411739
  • v123411739
  • 2014-05-02 17:32:34
  • 48617

初识工作流 BPMN Activiti以及JBPM

最近做OA系统,接触了工作流,知道了JBPM和Activiti两种工作流引擎。 工作流    BPMN   BPMN全称Business Process Model And Notation...
  • u013045552
  • u013045552
  • 2017-02-05 19:30:06
  • 2306
收藏助手
不良信息举报
您举报文章:JBPM工作流入门(上)
举报原因:
原因补充:

(最多只允许输入30个字)