|
本文介绍了编程方式部署jbpm工作流定义的方法。并向您提供了源代码。只要您正确配置了jbpm的数据库和hibernate,使用本文提供的这个工具类,就可以非常方便的部署您创建的jbpm工作流定义。 部署jbpm工作流要使用jbpm的工作流,必须首先部署工作流定义。就是把工作流定义文件载入到jbpm的数据库中。 jbpm的工作流有多种部署方式。包括:ant文件部署,eclipse图形设计器通过jboss(jboss上必需运行jbpm.war)部署,以及本文我要介绍的编程部署的方式。 一、ant文件发布方式 编写ant文件非常复杂。因为jbpm工作流的部署,需要使用jbpm数据库。这就必须要进行繁琐的配置。 二、eclipse图形设计器直接部署 这种方式非常简单。但是,这种部署方式,必须要同时运行jboss服务器。并且需要在jboss上运行配置正确的jbpm.war。它会把业务程序定义发布到jbpm.war使用的数据库上。 而jbpm.war的部署和配置非常麻烦。特别是你要更改使用的数据库时。jboss的一大特点,就是配置方式不标准,与通用的配置方式相差甚多。 因此,我不喜欢配置jboss下的jbpm.war。我已经创建了新的,可以部署到tomcat等所有服务器上的jbpm.war文件(我会在有空时推出一篇文章介绍如何制作可以运行在所有javaee服务器上的jbpm.war文件,并提供直接下载)。 但是,部署在tomcat上的jbpm.war,不能支持eclipse图形设计器部署业务程序定义。 而且,今天我重装eclipse之后,安装的图形设计器没有了发布功能!汗!可能是需要安装eclipse的某些插件吧!今天,我无法访问外国网站,所以找不到原因和解决办法。(又是中国特色) 三、编程方式部署jbpm业务程序(就是常说的“工作流”)定义 因为今天我无法使用eclipse图形设计器直接部署,所以就写了一个类,通过编程的方式直接部署。 这种方式也是非常简单而直接的。不需要再安装eclipse图形设计器,也不需要配置和运行支持jbpm的jboss。 只要你的应用程序中集成并正确配置了jbpm,(可以参考我的blog上的文章《向应用程序中加入jbpm组件》http://blog.csdn.net/shendl/archive/2006/10/23/1346877.aspx )然后把下面的类加入你的项目,运行junit测试或者执行main方法,就可以轻松的部署你的业务程序定义了! 编程方式部署jbpm工作流定义一、基本知识 1,junit测试和执行main方法,实际上是classpath目标目录下的.class文件的运行。查找资源文件,也是从classpath开始的。 2,我们的web项目应用程序,classpath是web-inf/classes。我们的业务程序定义文件所在目录processes设为src目录。所以,路径应该是“业务程序定义名字/processdefinition.xml”。 这里,我的业务程序定义的名字是checkshownews,所以classpath的路径应该是checkshownews/processdefinition.xml。 二、部署业务程序定义的工具类 下面是这个类的源文件,可以通过main方法和junit测试部署业务程序定义。 /** * */ package com.withub.common.util; import org.jbpm.jbpmconfiguration; import org.jbpm.jbpmcontext; import org.jbpm.graph.def.processdefinition; import junit.framework.testcase; /** * @author 沈东良 shendl_s@hotmail.com * 7:21:19 pm * deployjbpmprocessdefinition类,提供了部署jbpm工作流定义到数据库的功能! */ public class deployjbpmprocessdefinition extends testcase { static jbpmconfiguration jbpmconfiguration = null; static { jbpmconfiguration = jbpmconfiguration.getinstance(); } public void setup() { //创建数据库表 //jbpmconfiguration.createschema(); } public void teardown() { //删除数据库表 //jbpmconfiguration.dropschema(); } /** * 测试方法 * */ public void testsimplepersistence() { // between the 3 method calls below, all data is passed via the // database. here, in this unit test, these 3 methods are executed // right after each other because we want to test a complete process // scenario情节. but in reality, these methods represent different // requests to a server. // since we start with a clean, empty in-memory database, we have to // deploy the process first. in reality, this is done once by the // process developer. /** * 这个方法把业务处理定义通过hibernate保存到数据库中。 */ deployprocessdefinition("checkshownews/processdefinition.xml"); } /*
<process-definition xmlns="" name="checkshownews"> <swimlane name="checknewsmanagers"> <assignment class="com.withub.wcms.manage.publishnews.jbpmhandler.assignmenthandler.checknewsassignmenthandler" config-type="bean"></assignment> </swimlane> <swimlane name="editnewsuser"> <assignment class="com.withub.wcms.manage.publishnews.jbpmhandler.assignmenthandler.editnewsassignmenthandler" config-type="bean"></assignment> </swimlane> <start-state name="relatingnewschannel"> <transition name="" to="checknews"></transition> </start-state> <task-node name="checknews"> <task name="checknews" swimlane="checknewsmanagers"></task> <transition name="rejected" to="editnews"></transition> <transition name="passed" to="shownews"></transition> </task-node> <end-state name="end"></end-state> <task-node name="editnews"> <task name="editnews" swimlane="editnewsuser"></task> <transition name="commited" to="checknews"></transition> </task-node> <node name="shownews"> <action name="shownewsaction" class="com.withub.wcms.manage.publishnews.jbpmhandler.actionhandler.shownews" config-type="bean"/> <transition name="" to="end"></transition> </node> </process-definition> */ /** * "checkshownews/processdefinition.xml" */ public void deployprocessdefinition(string filepath) { // this test shows a process definition and one execution // of the process definition. the process definition has // 3 nodes: an unnamed start-state, a state 's' and an // end-state named 'end'. processdefinition processdefinition = processdefinition.parsexmlresource(filepath); // lookup the pojo persistence context-builder that is configured above jbpmcontext jbpmcontext = jbpmconfiguration.createjbpmcontext(); try { // deploy the process definition in the database jbpmcontext.deployprocessdefinition(processdefinition); } finally { // tear down the pojo persistence context. // this includes flush the sql for inserting the process definition // to the database. /* * 关闭jbpm上下文。删除pojo持久化上下文。 * 这包括刷新sql来真正的把业务处理定义插入到数据库中。 * */ jbpmcontext.close(); } } /** * * @param args */ public static void main(string[] args){ deployjbpmprocessdefinition instance=new deployjbpmprocessdefinition(); instance.deployprocessdefinition(args[0]); } }
三、eclipse下使用main测试的方法 1,点击run选项:
2,选中main方法测试的 1)项目-----需要classpath,所有的.class文件、jar包和资源文件所在地。 2)main方法所在的类。 3,由于我们的main方法使用了一个参数,所以需要提供一个参数。就是jbpm业务程序定义文件相对于项目的classpath的相对路径。 4,点run,运行ok!
|