也许部署流程定义的方法有很多,这里选用的是用Java代码调用工作流引擎提供的部署服务API。在这之前,假设你的Eclipse已经安装好了GPD工作流画图工具,并且学会了画出最简单的HelloWorld程序。本博客中用到的最简单的jBPM HelloWorld程序如下:
<?xml version="1.0" encoding="UTF-8"?> <process name="HelloWorld" xmlns="http://jbpm.org/4.4/jpdl"> <start name="start1" g="168,60,48,48"> <transition name="to HelloWorld" to="HelloWorld" g="-101,-22"/> </start> <end name="end1" g="168,432,48,48"/> <state name="HelloWorld" g="138,236,92,52"> <transition name="to end1" to="end1" g="-57,-22"/> </state> </process>
该流程实例的预览图如上图所示。
当然了,你需要建立一个Java工程,这个工程可以是Java SE就够了,项目的结构和引入的包如图:
我这里用的jBPM版本是4.4,所有依赖的jar如上图所示,你需要在你的src目录下配置两个xml文件。
jbpm.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <jbpm-configuration> <import resource="jbpm.default.cfg.xml" /> <import resource="jbpm.businesscalendar.cfg.xml" /> <import resource="jbpm.tx.hibernate.cfg.xml" /> <import resource="jbpm.jpdl.cfg.xml" /> <import resource="jbpm.bpmn.cfg.xml" /> <import resource="jbpm.identity.cfg.xml" /> </jbpm-configuration>
jbpm.hibernate.cfg.xml
<?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> <!-- 如果是MySQL,一定要使用org.hibernate.dialect.MySQL5InnoDBDialect方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///jbpm4_study</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 这些映射文件是JBPM的,不能删除!!! --> <mapping resource="jbpm.repository.hbm.xml" /> <mapping resource="jbpm.execution.hbm.xml" /> <mapping resource="jbpm.history.hbm.xml" /> <mapping resource="jbpm.task.hbm.xml" /> <mapping resource="jbpm.identity.hbm.xml" /> </session-factory> </hibernate-configuration>
这时候需要你子数据库中创建上述Hibernate连接的库,创建的语法如下:
mysql> create database jbpm4_study;
现在编写我们的单元测试代码:
package com.springchang.jbpm01; import org.jbpm.api.Configuration; import org.jbpm.api.ProcessEngine; import org.junit.Test; public class MyTest01 { private static ProcessEngine processEngine; //直接将流程引擎写成静态变量 //静态代码块,获取processEngine static { Configuration cfg = new Configuration(); cfg.setResource("jbpm.cfg.xml"); processEngine = cfg.buildProcessEngine(); } // 1,部署流程定义文档 @Test public void testDeployProcessDefinition() throws Exception { processEngine.getRepositoryService() //调用processEngine提供的接口 .createDeployment() .addResourceFromClasspath("com/springchang/jbpm01/HelloWorld.jpdl.xml") //加载HelloWorld.jpdl.xml文件 .addResourceFromClasspath("com/springchang/jbpm01/HelloWorld.png") //加载Helloworld.png图片 .deploy(); //发布流程定义 } }
这时候可以运行我们的单元测试,由于是第一次加载jBPM框架,系统需要向数据库中创建表结构,所以稍微等会儿。如果你的单元测试没有报错,这时候你的数据库就应该新建了18张以JBPM4开头的表。
mysql> show tables; +-----------------------+ | Tables_in_jbpm4_study | +-----------------------+ | JBPM4_DEPLOYMENT | | JBPM4_DEPLOYPROP | | JBPM4_EXECUTION | | JBPM4_HIST_ACTINST | | JBPM4_HIST_DETAIL | | JBPM4_HIST_PROCINST | | JBPM4_HIST_TASK | | JBPM4_HIST_VAR | | JBPM4_ID_GROUP | | JBPM4_ID_MEMBERSHIP | | JBPM4_ID_USER | | JBPM4_JOB | | JBPM4_LOB | | JBPM4_PARTICIPATION | | JBPM4_PROPERTY | | JBPM4_SWIMLANE | | JBPM4_TASK | | JBPM4_VARIABLE | +-----------------------+ rows in set (0.00 sec)
当我的HelloWorld流程部署成功之后,查询JBPM4_LOB表,可以看到如下两条记录:
10002 0 (BLOB) 12.67 KB 10001 com/springchang/jbpm01/HelloWorld.png 10003 0 (BLOB) 391 bytes 10001 com/springchang/jbpm01/HelloWorld.jpdl.xml