jbpm连接mysql数据库的实例以及问题解决方案
jbpm连接mysql数据库的实例实现步骤:
第一步:在eclipse中新建一个java project,项目名称为:jbpmExample_2。
第二步:导入Junit、jbpm以及mysql的jar.
第三步:将jbpm下的config中的jbpm.cfg.xml拷贝到src目录下。
第四步:将jbpm下的config(我的目录是:D:\Program Files\jbpm-jpdl-3.2.3\config)中的hibernate.cfg.xml拷贝到src目录下,并修改hibernate.cfg.xml的配置文件。
将hibernate.cfg.xml中的如下的代码:
org.hibernate.dialect.HSQLDialect
-
org.hsqldb.jdbcDriver
jdbc:hsqldb:mem:jbpm
sa
-
org.hibernate.cache.HashtableCacheProvider
改成如下的代码,连接mysql数据库:
org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/jbpm
root
root
并在如上的代码添加如下的代码,用于自动创建jbpm所需的数据表:
create
true
第五步:在src目录下新建jbpmConfiguration.xml文件,内容如下:
jbpmConfiguration.xml此xml文件可以不写,因为系统会自动去找org/jbpm/default.jbpm.cfg.xml文件。
第六步:在src目录下新建myProcess.xml文件,内容如下:
第七步:新建一个Junit Test Case 类名为:TestHelloWorldDB,代码如下:
package com.baoz.je.test;
import java.util.List;
import junit.framework.TestCase;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
public class TestHelloWorldDB extends TestCase {
static JbpmConfiguration jbpmConfiguration = null;
static{
jbpmConfiguration=JbpmConfiguration.parseResource("jbpmConfiguration.xml");
}
protected void setUp() throws Exception {
//创建jbpm的表结构
jbpmConfiguration.createSchema();
}
/**
* 部署流程
*/
public void deployProcessDefinition(){
//定义流程
ProcessDefinition processDefinition = new ProcessDefinition().parseXmlResource("myProcess.xml");
//查找上面配置的pojo持久性上下文生成器
JbpmContext jbpmContext= jbpmConfiguration.createJbpmContext();
//在数据库中部署流程定义
try {
jbpmContext.deployProcessDefinition(processDefinition);
} finally {
jbpmContext.close();
}
}
/**
* 假如一个用户提交表单上,流程的执行过程
*/
public void processInstancesCreatedWhenUserSubmitWebappForm(){
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
GraphSession graphSession = jbpmContext.getGraphSession();
ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition("hello world");
ProcessInstance processInstance = new ProcessInstance(processDefinition);
Token token = processInstance.getRootToken();
assertEquals("start", token.getNode().getName());
token.signal();
assertEquals("s",token.getNode().getName());
jbpmContext.save(processInstance);
} finally{
jbpmContext.close();
}
}
/**
* 当一个异步消息被接收时,流程的执行过程
*/
public void theProcessInstancesContinuesWhenAnAsyncMessageIsReceived(){
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
GraphSession graphSession = jbpmContext.getGraphSession();
ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition("hello world");
List piList = graphSession.findProcessInstances(processDefinition.getId());
ProcessInstance processInstance = piList.get(0);
processInstance.signal();
assertTrue(processInstance.hasEnded());
jbpmContext.save(processInstance);
} finally{
jbpmContext.close();
}
}
public void testSimplePersistence(){
deployProcessDefinition();
processInstancesCreatedWhenUserSubmitWebappForm();
theProcessInstancesContinuesWhenAnAsyncMessageIsReceived();
}
protected void tearDown() throws Exception {
//删除创建的jbpm的表结构
//jbpmConfiguration.dropSchema();
}
}
第八步:在mysql中新建一个数据库,数据库名为jbpm。
第九步:运行如上的Test Case,如果没有报错,而且控制台打印出如下的内容,表示此示例运行成功:
log4j:WARN No appenders could be found for logger (org.jbpm.JbpmConfiguration).
log4j:WARN Please initialize the log4j system properly.
alter table JBPM_ACTION drop foreign key FK_ACTION_EVENT
alter table JBPM_ACTION drop foreign key FK_ACTION_EXPTHDL
alter table JBPM_ACTION drop foreign key FK_ACTION_PROCDEF
alter table JBPM_ACTION drop foreign key FK_CRTETIMERACT_TA
alter table JBPM_ACTION drop foreign key FK_ACTION_ACTNDEL
alter table JBPM_ACTION drop foreign key FK_ACTION_REFACT
alter table JBPM_BYTEARRAY drop foreign key FK_BYTEARR_FILDEF
alter table JBPM_BYTEBLOCK drop foreign key FK_BYTEBLOCK_FILE
alter table JBPM_COMMENT drop foreign key FK_COMMENT_TOKEN
alter table JBPM_COMMENT drop foreign key FK_COMMENT_TSK
alter table JBPM_DECISIONCONDITIONS drop foreign key FK_DECCOND_DEC
alter table JBPM_DELEGATION drop foreign key FK_DELEGATION_PRCD
alter table JBPM_EVENT drop foreign key FK_EVENT_PROCDEF
alter table JBPM_EVENT drop foreign key FK_EVENT_NODE
alter table JBPM_EVENT drop foreign key FK_EVENT_TRANS
alter table JBPM_EVENT drop foreign key FK_EVENT_TASK
alter table JBPM_JOB drop foreign key FK_JOB_TOKEN
alter table JBPM_JOB drop foreign key FK_JOB_NODE
alter table JBPM_JOB drop foreign key FK_JOB_PRINST
alter table JBPM_JOB drop foreign key FK_JOB_ACTION
alter table JBPM_JOB drop foreign key FK_JOB_TSKINST
alter table JBPM_LOG drop foreign key FK_LOG_SOURCENODE
alter table JBPM_LOG drop foreign key FK_LOG_TOKEN
alter table JBPM_LOG drop foreign key FK_LOG_OLDBYTES
alter table JBPM_LOG drop foreign key FK_LOG_NEWBYTES
alter table JBPM_LOG drop foreign key FK_LOG_CHILDTOKEN
alter table JBPM_LOG drop foreign key FK_LOG_DESTNODE
alter table JBPM_LOG drop foreign key FK_LOG_TASKINST
alter table JBPM_LOG drop foreign key FK_LOG_SWIMINST
alter table JBPM_LOG drop foreign key FK_LOG_PARENT
alter table JBPM_LOG drop foreign key FK_LOG_NODE
alter table JBPM_LOG drop foreign key FK_LOG_ACTION
alter table JBPM_LOG drop foreign key FK_LOG_VARINST
alter table JBPM_LOG drop foreign key FK_LOG_TRANSITION
alter table JBPM_MODULEDEFINITION drop foreign key FK_TSKDEF_START
alter table JBPM_MODULEDEFINITION drop foreign key FK_MODDEF_PROCDEF
alter table JBPM_MODULEINSTANCE drop foreign key FK_TASKMGTINST_TMD
alter table JBPM_MODULEINSTANCE drop foreign key FK_MODINST_PRCINST
alter table JBPM_NODE drop foreign key FK_PROCST_SBPRCDEF
alter table JBPM_NODE drop foreign key FK_NODE_PROCDEF
alter table JBPM_NODE drop foreign key FK_NODE_SCRIPT
alter table JBPM_NODE drop foreign key FK_NODE_ACTION
alter table JBPM_NODE drop foreign key FK_DECISION_DELEG
alter table JBPM_NODE drop foreign key FK_NODE_SUPERSTATE
alter table JBPM_POOLEDACTOR drop foreign key FK_POOLEDACTOR_SLI
alter table JBPM_PROCESSDEFINITION drop foreign key FK_PROCDEF_STRTSTA
alter table JBPM_PROCESSINSTANCE drop foreign key FK_PROCIN_PROCDEF
alter table JBPM_PROCESSINSTANCE drop foreign key FK_PROCIN_ROOTTKN
alter table JBPM_PROCESSINSTANCE drop foreign key FK_PROCIN_SPROCTKN
alter table JBPM_RUNTIMEACTION drop foreign key FK_RTACTN_PROCINST
alter table JBPM_RUNTIMEACTION drop foreign key FK_RTACTN_ACTION
alter table JBPM_SWIMLANE drop foreign key FK_SWL_ASSDEL
alter table JBPM_SWIMLANE drop foreign key FK_SWL_TSKMGMTDEF
alter table JBPM_SWIMLANEINSTANCE drop foreign key FK_SWIMLANEINST_TM
alter table JBPM_SWIMLANEINSTANCE drop foreign key FK_SWIMLANEINST_SL
alter table JBPM_TASK drop foreign key FK_TSK_TSKCTRL
alter table JBPM_TASK drop foreign key FK_TASK_ASSDEL
alter table JBPM_TASK drop foreign key FK_TASK_TASKNODE
alter table JBPM_TASK drop foreign key FK_TASK_PROCDEF
alter table JBPM_TASK drop foreign key FK_TASK_STARTST
alter table JBPM_TASK drop foreign key FK_TASK_TASKMGTDEF
alter table JBPM_TASK drop foreign key FK_TASK_SWIMLANE
alter table JBPM_TASKACTORPOOL drop foreign key FK_TSKACTPOL_PLACT
alter table JBPM_TASKACTORPOOL drop foreign key FK_TASKACTPL_TSKI
alter table JBPM_TASKCONTROLLER drop foreign key FK_TSKCTRL_DELEG
alter table JBPM_TASKINSTANCE drop foreign key FK_TSKINS_PRCINS
alter table JBPM_TASKINSTANCE drop foreign key FK_TASKINST_TMINST
alter table JBPM_TASKINSTANCE drop foreign key FK_TASKINST_TOKEN
alter table JBPM_TASKINSTANCE drop foreign key FK_TASKINST_SLINST
alter table JBPM_TASKINSTANCE drop foreign key FK_TASKINST_TASK
alter table JBPM_TOKEN drop foreign key FK_TOKEN_PARENT
alter table JBPM_TOKEN drop foreign key FK_TOKEN_NODE
alter table JBPM_TOKEN drop foreign key FK_TOKEN_PROCINST
alter table JBPM_TOKEN drop foreign key FK_TOKEN_SUBPI
alter table JBPM_TOKENVARIABLEMAP drop foreign key FK_TKVARMAP_CTXT
alter table JBPM_TOKENVARIABLEMAP drop foreign key FK_TKVARMAP_TOKEN
alter table JBPM_TRANSITION drop foreign key FK_TRANSITION_TO
alter table JBPM_TRANSITION drop foreign key FK_TRANS_PROCDEF
alter table JBPM_TRANSITION drop foreign key FK_TRANSITION_FROM
alter table JBPM_VARIABLEACCESS drop foreign key FK_VARACC_TSKCTRL
alter table JBPM_VARIABLEACCESS drop foreign key FK_VARACC_SCRIPT
alter table JBPM_VARIABLEACCESS drop foreign key FK_VARACC_PROCST
alter table JBPM_VARIABLEINSTANCE drop foreign key FK_VARINST_TK
alter table JBPM_VARIABLEINSTANCE drop foreign key FK_VARINST_TKVARMP
alter table JBPM_VARIABLEINSTANCE drop foreign key FK_VARINST_PRCINST
alter table JBPM_VARIABLEINSTANCE drop foreign key FK_VAR_TSKINST
alter table JBPM_VARIABLEINSTANCE drop foreign key FK_BYTEINST_ARRAY
drop table if exists JBPM_ACTION
drop table if exists JBPM_BYTEARRAY
drop table if exists JBPM_BYTEBLOCK
drop table if exists JBPM_COMMENT
drop table if exists JBPM_DECISIONCONDITIONS
drop table if exists JBPM_DELEGATION
drop table if exists JBPM_EVENT
drop table if exists JBPM_EXCEPTIONHANDLER
drop table if exists JBPM_JOB
drop table if exists JBPM_LOG
drop table if exists JBPM_MODULEDEFINITION
drop table if exists JBPM_MODULEINSTANCE
drop table if exists JBPM_NODE
drop table if exists JBPM_POOLEDACTOR
drop table if exists JBPM_PROCESSDEFINITION
drop table if exists JBPM_PROCESSINSTANCE
drop table if exists JBPM_RUNTIMEACTION
drop table if exists JBPM_SWIMLANE
drop table if exists JBPM_SWIMLANEINSTANCE
drop table if exists JBPM_TASK
drop table if exists JBPM_TASKACTORPOOL
drop table if exists JBPM_TASKCONTROLLER