说明:例子jbpm版本为3.1.2。实现完全整合,并且由spring管理jbpmContext的session,此实例运行环境为tomcat 5.5.17。
一、创建MySql数据库:
设jbpm3.1.2解压文件夹为jbpm-starters-kit-3.1.2,查看目录\jbpm-db\build\mysql\scripts是否存在,是否有4个SQL文件,有就不必费话了。看名字就知道各文件的作用。不存在进行如下操作:
1.配置好ANT,改根目录下文件夹jbpm为jbpm.3。
2.打开CMD进入根目录下的jbpm-db目录,输入:ant mysql.scripts,回车运行后会在\jbpm-db\build\mysql\scripts下产生4个MySql文件,在现有工程的数据库中添加jbpm表。
二、下载springmodules,新增spring配置文件。
把jbpm整合到spring中,一般都用springmodules。新增spring配置文件,如本人的工程中有一文件springframework-plugins.xml,增加<import resource="classpath:cn/xiaolangs/plugins/jbpm/spring-jbpm.xml"/>,文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Local Hibernate Session Factory for jBPM -->
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:org/jbpm/graph/action/Script.hbm.xml</value>
<value>classpath:org/jbpm/identity/User.hbm.xml</value>
<value>classpath:org/jbpm/identity/Group.hbm.xml</value>
<value>classpath:org/jbpm/identity/Membership.hbm.xml</value>
<!-- hql queries and type defs -->
<value>classpath:org/jbpm/db/hibernate.queries.hbm.xml</value>
<!-- graph.def mapping files -->
<value>classpath:org/jbpm/graph/def/ProcessDefinition.hbm.xml</value>
<value>classpath:org/jbpm/graph/def/Node.hbm.xml</value>
<value>classpath:org/jbpm/graph/def/Transition.hbm.xml</value>
<value>classpath:org/jbpm/graph/def/Event.hbm.xml</value>
<value>classpath:org/jbpm/graph/def/Action.hbm.xml</value>
<value>classpath:org/jbpm/graph/def/SuperState.hbm.xml</value>
<value>classpath:org/jbpm/graph/def/ExceptionHandler.hbm.xml</value>
<value>classpath:org/jbpm/instantiation/Delegation.hbm.xml</value>
<!-- graph.node mapping files -->
<value>classpath:org/jbpm/graph/node/StartState.hbm.xml</value>
<value>classpath:org/jbpm/graph/node/EndState.hbm.xml</value>
<value>classpath:org/jbpm/graph/node/ProcessState.hbm.xml</value>
<value>classpath:org/jbpm/graph/node/Decision.hbm.xml</value>
<value>classpath:org/jbpm/graph/node/Fork.hbm.xml</value>
<value>classpath:org/jbpm/graph/node/Join.hbm.xml</value>
<value>classpath:org/jbpm/graph/node/State.hbm.xml</value>
<value>classpath:org/jbpm/graph/node/TaskNode.hbm.xml</value>
<!-- context.def mapping files -->
<value>classpath:org/jbpm/context/def/ContextDefinition.hbm.xml</value>
<value>classpath:org/jbpm/context/def/VariableAccess.hbm.xml</value>
<!-- taskmgmt.def mapping files -->
<value>classpath:org/jbpm/taskmgmt/def/TaskMgmtDefinition.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/def/Swimlane.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/def/Task.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/def/TaskController.hbm.xml</value>
<!-- module.def mapping files -->
<value>classpath:org/jbpm/module/def/ModuleDefinition.hbm.xml</value>
<!-- bytes mapping files -->
<value>classpath:org/jbpm/bytes/ByteArray.hbm.xml</value>
<!-- file.def mapping files -->
<value>classpath:org/jbpm/file/def/FileDefinition.hbm.xml</value>
<!-- scheduler.def mapping files -->
<value>classpath:org/jbpm/scheduler/def/CreateTimerAction.hbm.xml</value>
<value>classpath:org/jbpm/scheduler/def/CancelTimerAction.hbm.xml</value>
<!-- graph.exe mapping files -->
<value>classpath:org/jbpm/graph/exe/Comment.hbm.xml</value>
<value>classpath:org/jbpm/graph/exe/ProcessInstance.hbm.xml</value>
<value>classpath:org/jbpm/graph/exe/Token.hbm.xml</value>
<value>classpath:org/jbpm/graph/exe/RuntimeAction.hbm.xml</value>
<!-- module.exe mapping files -->
<value>classpath:org/jbpm/module/exe/ModuleInstance.hbm.xml</value>
<!-- context.exe mapping files -->
<value>classpath:org/jbpm/context/exe/ContextInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/TokenVariableMap.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/VariableInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/variableinstance/ByteArrayInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/variableinstance/DateInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/variableinstance/DoubleInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/variableinstance/HibernateLongInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/variableinstance/HibernateStringInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/variableinstance/LongInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/variableinstance/NullInstance.hbm.xml</value>
<value>classpath:org/jbpm/context/exe/variableinstance/StringInstance.hbm.xml</value>
<!-- msg.db mapping files -->
<value>classpath:org/jbpm/msg/Message.hbm.xml</value>
<value>classpath:org/jbpm/msg/db/TextMessage.hbm.xml</value>
<value>classpath:org/jbpm/command/ExecuteActionCommand.hbm.xml</value>
<value>classpath:org/jbpm/command/ExecuteNodeCommand.hbm.xml</value>
<value>classpath:org/jbpm/command/SignalCommand.hbm.xml</value>
<value>classpath:org/jbpm/command/TaskInstanceEndCommand.hbm.xml</value>
<!-- taskmgmt.exe mapping files -->
<value>classpath:org/jbpm/taskmgmt/exe/TaskMgmtInstance.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/exe/TaskInstance.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/exe/PooledActor.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/exe/SwimlaneInstance.hbm.xml</value>
<!-- scheduler.exe mapping files -->
<value>classpath:org/jbpm/scheduler/exe/Timer.hbm.xml</value>
<!-- logging mapping files -->
<value>classpath:org/jbpm/logging/log/ProcessLog.hbm.xml</value>
<value>classpath:org/jbpm/logging/log/MessageLog.hbm.xml</value>
<value>classpath:org/jbpm/logging/log/CompositeLog.hbm.xml</value>
<value>classpath:org/jbpm/graph/log/ActionLog.hbm.xml</value>
<value>classpath:org/jbpm/graph/log/NodeLog.hbm.xml</value>
<value>classpath:org/jbpm/graph/log/ProcessInstanceCreateLog.hbm.xml</value>
<value>classpath:org/jbpm/graph/log/ProcessInstanceEndLog.hbm.xml</value>
<value>classpath:org/jbpm/graph/log/SignalLog.hbm.xml</value>
<value>classpath:org/jbpm/graph/log/TokenCreateLog.hbm.xml</value>
<value>classpath:org/jbpm/graph/log/TokenEndLog.hbm.xml</value>
<value>classpath:org/jbpm/graph/log/TransitionLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/VariableLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/VariableCreateLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/VariableDeleteLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/VariableUpdateLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/variableinstance/ByteArrayUpdateLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/variableinstance/DateUpdateLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/variableinstance/DoubleUpdateLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/variableinstance/HibernateLongUpdateLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/variableinstance/HibernateStringUpdateLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/variableinstance/LongUpdateLog.hbm.xml</value>
<value>classpath:org/jbpm/context/log/variableinstance/StringUpdateLog.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/log/TaskLog.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/log/TaskCreateLog.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/log/TaskAssignLog.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/log/TaskEndLog.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/log/SwimlaneLog.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/log/SwimlaneCreateLog.hbm.xml</value>
<value>classpath:org/jbpm/taskmgmt/log/SwimlaneAssignLog.hbm.xml</value>
</list>
</property>
</bean>
<!--Hibernate TransactionManager-->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>
<!-- jBPM Configuration -->
<bean id="jbpmConfiguration" class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
<property name="configuration" value="classpath:cn/xiaolangs/plugins/jbpm/jbpm.cfg.xml"/>
</bean>
<bean id="jbpmTemplate" class="org.springmodules.workflow.jbpm31.JbpmTemplate">
<constructor-arg index="0" ref="jbpmConfiguration"/>
</bean>
<bean id="jbpmProcessDao" class="cn.xiaolangs.oa.dao.jbpm.JbpmProcessDao">
<property name="template" ref="jbpmTemplate"/>
</bean>
<bean id="jbpmBaseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="hibernateTransactionManager"/>
<property name="proxyTargetClass" value="true"/>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
文件说明:<property name="dataSource" ref="dataSource"/>引用现在工程中的dataSource。文件cn/xiaolangs/plugins/jbpm/jbpm.cfg.xml与jbpm-3.1.2.jar中的default.jbpm.cfg.xml基本相同,删除<string name="resource.hibernate.cfg.xml" value="hibernate.cfg.xml" />这句。
DAO类内容节选:
import org.springmodules.workflow.jbpm31.*;
import org.jbpm.JbpmContext;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import java.util.List;
import org.springmodules.workflow.jbpm31.support.JbpmDaoSupport;
public class JbpmProcessDao extends JbpmDaoSupport {
public void deployProcessDefinition(final ProcessDefinition pd) {
getTemplate().execute(new JbpmCallback() {
public Object doInJbpm(JbpmContext context) {
context.deployProcessDefinition(pd);
return null;
}
});
}
public TaskInstance getStartProcessDefinitionById(final long processDefinitionId) {
return (TaskInstance) getTemplate().execute(new JbpmCallback() {
public Object doInJbpm(JbpmContext context) {
ProcessDefinition processDefinition = context.getGraphSession().loadProcessDefinition(processDefinitionId);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
context.save(processInstance);
return taskInstance;
}
});
}
三、把jbpm融入spring,让spring管理jbpmContext的session。
spring+hibernate时,hibernate的session是由spring管理,我们把jbpm的session也交由spring管理。其中方法之一是web.xml的filter。