您的位置:首页 > 产品设计 > 产品经理

编程方式部署jBPM工作流

2006-11-07 22:18 295 查看
编程方式部署jBPM工作流

本文介绍了编程方式部署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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: