jbpm4学习入门--环境布署
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>
<propertyname="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<propertyname="hibernate.connection.url">jdbc:mysql://localhost:3306/jbpm</property>
<propertyname="hibernate.connection.username">usr</property>
<property name="hibernate.connection.password">pwd</property>4、写一个java类 HelloJBPM4
package ex;
import org.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.jbpm.api.Configuration;
importorg.jbpm.api.Execution;
importorg.jbpm.api.ExecutionService;
importorg.jbpm.api.HistoryService;
importorg.jbpm.api.IdentityService;
importorg.jbpm.api.ManagementService;
importorg.jbpm.api.ProcessEngine;
importorg.jbpm.api.ProcessInstance;
importorg.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;public class HelloJBPM4 {
protectedfinalLoglog=LogFactory.getLog(getClass());
protected static ProcessEngine processEngine = null;protected static RepositoryService repositoryService;
protectedstaticExecutionServiceexecutionService;
protectedstaticManagementServicemanagementService;
protectedstaticTaskServicetaskService;
protectedstaticHistoryServicehistoryService;
protectedstaticIdentityServiceidentityService;
protectedsynchronizedvoidinitialize(){
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();
}
}
privatevoiddoJob(){
initialize();
StringdeploymentId;
deploymentId=repositoryService.createDeployment()
.addResourceFromClasspath("ex/HelloJBPM4.jpdl.xml")
.deploy();
ProcessInstanceprocessInstance=executionService.startProcessInstanceByKey("Custom");
ExecutionexecutionInPrintDots=processInstance.findActiveExecutionIn("printdots");
StringexecutionId=executionInPrintDots.getId();
executionService.signalExecutionById(executionId);
repositoryService.deleteDeploymentCascade(deploymentId);
}
publicstaticvoidmain(Stringarg[]){
HelloJBPM4ex=newHelloJBPM4();
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">
<transitionto="printdots"/>
</start><custom name="print dots"
class="ex.PrintDots"
g="96,16,100,52">
<transitionto="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");
ExecutionexecutionInPrintDots=processInstance.findActiveExecutionIn("printdots");
StringexecutionId=executionInPrintDots.getId();
executionService.signalExecutionById(executionId);