1、下载, jboss.org下的jbpm4,ecplise 3.5, mysql 5
2、安装jbpm4的数据库
在mysql上建一个学习用的db
在jbpm4里找到 jbpm-4.0\db\schema.scripts\jbpm.mysql.create.sql
执行,生成jbpm4需要的数据库表。
3、在ecplise里建一个学习项目(java),并把 jbpm4\lib里所有的jar以及jbpm.jar加入编译环境
从 jbpm4的example的目录下的xml得到到本项目的src目录下,
主要是要用 jbpm.cfg.xml、jbpm.hibernate.cfg.xml
把jbpm.hibernate.cfg.xml 的数据库连接设置改改如下,使能连上mysql。
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<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">usr</property>
<property name="hibernate.connection.password">pwd</property>
4、写一个java类 HelloJBPM4
package ex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
public class HelloJBPM4 {
protected final Log log = LogFactory.getLog(getClass());
protected static ProcessEngine processEngine = null;
protected static RepositoryService repositoryService;
protected static ExecutionService executionService;
protected static ManagementService managementService;
protected static TaskService taskService;
protected static HistoryService historyService;
protected static IdentityService identityService;
protected synchronized void initialize() {
if (processEngine==null) {
String jbpmTestCfgType = System.getProperty("jbpm.test.cfg.type");
log.debug(jbpmTestCfgType);
Configuration configuration = new Configuration(jbpmTestCfgType);
String jbpmTestCfgResource = System.getProperty("jbpm.test.cfg.resource");
log.debug("jbpmTestCfgResource-->"+jbpmTestCfgResource);
if (jbpmTestCfgResource!=null) {
configuration.setResource(jbpmTestCfgResource);
}
processEngine = configuration.buildProcessEngine();
log.debug("using ProcessEngine "+System.identityHashCode(processEngine));
repositoryService = processEngine.get(RepositoryService.class);
executionService = processEngine.getExecutionService();
historyService = processEngine.getHistoryService();
managementService = processEngine.getManagementService();
taskService = processEngine.getTaskService();
identityService = processEngine.getIdentityService();
}
}
private void doJob(){
initialize();
String deploymentId;
deploymentId = repositoryService.createDeployment()
.addResourceFromClasspath("ex/HelloJBPM4.jpdl.xml")
.deploy();
ProcessInstance processInstance = executionService.startProcessInstanceByKey("Custom");
Execution executionInPrintDots = processInstance.findActiveExecutionIn("print dots");
String executionId = executionInPrintDots.getId();
executionService.signalExecutionById(executionId);
repositoryService.deleteDeploymentCascade(deploymentId);
}
public static void main(String arg[]){
HelloJBPM4 ex=new HelloJBPM4();
ex.doJob();
}
}
代码是从JbpmTestCase.java、org.jbpm.examples.custom中复制过来的
略改了一改,就是一个jbpm4的helloworld了。
注意:org.jbpm.examples.custom下的xml改名为HelloJBPM4.jpdl.xml
类路径也改为ex
5、执行这个类(java application)
成功的话,在ecplise的console里打印一个美女的点图。
HelloJBPM4.jpdl.xml 里调用了PrintDots.java
这个也是从org.jbpm.examples.custom复制过来的
HelloJBPM4.jpdl.xml
<?xml version="1.0" encoding="UTF-8"?>
<process name="Custom" xmlns="http://jbpm.org/4.0/jpdl">
<start g="20,20,48,48">
<transition to="print dots" />
</start>
<custom name="print dots"
class="ex.PrintDots"
g="96,16,100,52">
<transition to="end" />
</custom>
<end name="end" g="231,19,80,40"/>
</process>
6、总结
用ecplise画工作流图,生成jpdl.xml
自己写个java类把jpdl.xml导入数据库。
deploymentId = repositoryService.createDeployment()
.addResourceFromClasspath("ex/HelloJBPM4.jpdl.xml")
.deploy();
下面代码把流程从数据库中取出,并执行
ProcessInstance processInstance = executionService.startProcessInstanceByKey("Custom");
Execution executionInPrintDots = processInstance.findActiveExecutionIn("print dots");
String executionId = executionInPrintDots.getId();
executionService.signalExecutionById(executionId);