JBPM4.4与SSH的集成。
1.引入jar包:
2.jbpm.cfg.xml文件的配置:位于src下
<?xml version="1.0" encoding="UTF-8"?> <jbpm-configuration> <import resource="jbpm.default.cfg.xml" /> <import resource="jbpm.tx.spring.cfg.xml" /> <import resource="jbpm.jpdl.cfg.xml" /> <import resource="jbpm.bpmn.cfg.xml" /> <import resource="jbpm.identity.cfg.xml" /> <import resource="jbpm.businesscalendar.cfg.xml" /> <import resource="jbpm.console.cfg.xml" /> <import resource="jbpm.jobexecutor.cfg.xml" /> <process-engine-context> <string name="spring.cfg" value="config/spring/applicationContext.xml" /> </process-engine-context> </jbpm-configuration>
3.spring配置文件applicationContext.xml的配置:位于src/config.spring包下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:database.properties </value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- use JNID Connection Pool --> <!-- <bean id="rlicDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/bookstore"/> </bean> --> <!--Hibernate SessionFatory--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingLocations"> <list> <!-- 以下几个jbpm.*.hbm.xml由jBPM自带 --> <value>classpath:jbpm.repository.hbm.xml</value> <value>classpath:jbpm.execution.hbm.xml</value> <value>classpath:jbpm.history.hbm.xml</value> <value>classpath:jbpm.task.hbm.xml</value> <value>classpath:jbpm.identity.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <prop key="hibernate.show_sql"> ${hibernate.show_sql} </prop> <prop key="hibernate.format_sql"> ${hibernate.format_sql} </prop> <prop key="hibernate.use_sql_comments"> ${hibernate.use_sql_comments} </prop> </props> </property> </bean> <!--Hibernate TransactionManager--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--Base TransactionProxyed Service Bean--> <bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager" /> <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="query*">PROPAGATION_REQUIRED,readOnly </prop> <prop key="count*">PROPAGATION_REQUIRED,readOnly </prop> <prop key="is*">PROPAGATION_REQUIRED,readOnly </prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="edit*">PROPAGATION_REQUIRED</prop> <prop key="modify*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="auto*">PROPAGATION_REQUIRED</prop> <prop key="send*">PROPAGATION_REQUIRED</prop> <prop key="deploy*">PROPAGATION_REQUIRED</prop> <prop key="start*">PROPAGATION_REQUIRED</prop> <prop key="complete*">PROPAGATION_REQUIRED</prop> <prop key="end*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly </prop> </props> </property> </bean> <!-- jbpm配置 --> <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper" /> <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" /> <!--jbpm4.4核心代码--> <bean id="jbpm4Utils" name="jbpm4Utils" class="com.jabberchina.jbpm4.utils.Jbpm4Utils"> <property name="processEngine" ref="processEngine"></property> </bean> </beans>
database.properties数据文件:位于src下
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/jbpmdb jdbc.username=root jdbc.password=root # #<!-- 当你使用mysql的时候,jbpm使用的是org.hibernate.dialect.MySQLInnoDBDialect --> # hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.use_sql_comments=false hibernate.cache.use_query_cache=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.hbm2ddl.auto=update #jbpmPath 工作流流程保存位置 # jbpmPath=com/jabberchina/jbpm/jpdl/ #
3.web.xml文件的配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>JBPM4.4WEB</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring/applicationContext*.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter> <filter-name>struts2Filter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2Filter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring 刷新Introspector防止内存泄露 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
4.当然,数据库中表的创建是比不可少的,除了利用ant或者pom.xml能够自动创建数据库表外,不知道还有没有其他途径能够自动穿件数据库所需要的表。
JBPM4.4默认是试用hsql,内存中的数据库。
如果不利用jbpm(jbpm官网:http://sourceforge.net/projects/jbpm/files/jBPM%204/jbpm-4.4/jbpm-4.4.zip/download,所下载的jbpm-4.4.zip包)中所提供的ant或者pom.xml文件,而是手动创建一个jbpm的web工程,除了采用某人的hsql不用手动创建表外,其他数据,都需要手动的创建表。
首先解压,现在的jbpm-4.4.zip包.在目录%jbpm-4.4%\install\src\db\create文件夹下选择相应的数据库见表文件。本例中使用的是mysql数据库,所以在mysql数据库中导入jbpm.mysql.create.sql文件,在数据库空间名为jbpmdb的数据库空间内创建JBPM所需要的表即可。
至此。jbpm4.4+ssh配置部署完毕.
5.jbpm核心代码:
package com.jabberchina.jbpm4.utils; import java.util.List; import java.util.Map; import java.util.Set; import org.jbpm.api.Execution; import org.jbpm.api.ExecutionService; import org.jbpm.api.HistoryService; 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; import org.jbpm.api.model.ActivityCoordinates; import org.jbpm.api.task.Task; import com.jabberchina.core.Page; /** * @author guwh * @version 创建时间:2011-11-7 下午03:49:35 类说明 jbpm操作数据库模板类 */ public class Jbpm4Utils { private ProcessEngine processEngine; private RepositoryService repositoryService = null; private ExecutionService executionService = null; private TaskService taskService = null; private HistoryService historyService = null; private ManagementService managementService = null; public Jbpm4Utils() { } /** * 部署流程到数据库 * * @param resourceName * 资源文件名字 比如(com/jabberchina/jbpm/jpdl/process.jpdl.xml) * @return 返回流程定义id(格式:key-version) */ public String Deploy(String resourceName) { return repositoryService.createDeployment().addResourceFromClasspath( resourceName).deploy(); } /** * 创建一个新的流程实例 * * @param processDefinitionKey * (process.jpdl.xml中process标签的key) * @param processInstanceKey * (用户给的key,比如一个请假单的id) * @return 流程实例 */ public ProcessInstance startProcessInstance(String processDefinitionKey, String processInstanceKey) { return executionService.startProcessInstanceByKey(processDefinitionKey, processInstanceKey); } /** * 创建一个新的流程实例 * * @param processDefinitionKey * (process.jpdl.xml中process标签的key) * @return 流程实例 */ public ProcessInstance startProcessInstance(String processDefinitionKey) { return executionService.startProcessInstanceByKey(processDefinitionKey); } /** * 创建一个新的流程实例 * * @param processDefinitionKey * (process.jpdl.xml中process标签的key) * @param variables * 该流程实例要用到的变量 * @param processInstanceKey * (用户给定的业务key) * @return */ public ProcessInstance startProcessInstance(String processDefinitionKey, Map<String, ?> variables, String processInstanceKey) { return executionService.startProcessInstanceByKey(processDefinitionKey, variables, processInstanceKey); } /** * 提交任务(适合于第一个任务自动完成) * * @param processInstanceId * 任务id */ public void completeTaskByPid(String processInstanceId, String outcome, Map<String, ?> variables) { Task task = taskService.createTaskQuery().processInstanceId( processInstanceId).uniqueResult(); String taskId = task.getId(); taskService.completeTask(taskId, outcome, variables); } /** * 提交任务(适合于第一个任务自动完成) * * @param processInstanceId * 任务id */ public void completeTaskByPid(String processInstanceId, String outcome) { Task task = taskService.createTaskQuery().processInstanceId( processInstanceId).uniqueResult(); String taskId = task.getId(); taskService.completeTask(taskId, outcome); } /** * 提交任务 * * @param taskId * 任务id */ public void completeTask(String taskId) { taskService.completeTask(taskId); } /** * 将任务流转到指定名字的流程中去 * * @param taskId * @param outcome */ public void completeTask(String taskId, String outcome) { taskService.completeTask(taskId, outcome); } /*** * 将任务流转到指定名字的流程中去,并带有参数变量 * * @param taskId * @param outcome * @param variables */ public void completeTask(String taskId, String outcome, Map<String, ?> variables) { taskService.completeTask(taskId, outcome, variables); } /** * 根据key获取流程实例 * * @param key * * @return 返回查找到得流程实例,没有返回null */ public ProcessInstance getProcessInstance(String key) { return executionService.createProcessInstanceQuery() .processInstanceKey(key).uniqueResult(); } /** * 根据executionId获取指定的变量值 * * @param executionId * @param variableName * @return */ public Object getVariableByexecutionId(String executionId, String variableName) { return executionService.getVariable(executionId, variableName); } /** * 根据任务id获取指定变量值 * * @param taskId * @param variableName * @return */ public Object getVariableByTaskId(String taskId, String variableName) { return taskService.getVariable(taskId, variableName); } /** * 获取指定用户名字的任务,并分页 * * @param userId * @return */ public Page<Task> findPersonalTasksPage(Page<Task> page, String userId) { int totalCount = (int) taskService.createTaskQuery().assignee(userId) .count(); if (page == null) { page = new Page<Task>(); } List<Task> list = taskService.createTaskQuery().assignee(userId).page( page.getFirst(), page.getPageSize()).list(); if (list == null) return null; page.setResult(list); page.setTotalCount(totalCount); return page; } /*** * 获取要查页的Task列表 * * @param firstResult * @param maxResults * @param userId * @return */ public List<Task> findPersonalTasks(int firstResult, int maxResults, String userId) { return taskService.createTaskQuery().assignee(userId).page(firstResult, maxResults).list(); } /*** * 获取当然任务个数 * * @param userId * @return */ public Long findPersonalTaskCount(String userId) { return taskService.createTaskQuery().assignee(userId).count(); } /** * 根据任务id获取任务 * * @param taskId * @return */ public Task findPersonalTasks(String userId) { return taskService.createTaskQuery().assignee(userId).uniqueResult(); } /** * 根据任务id获取任务 * * @param taskId * @return */ public Task getTask(String taskId) { return taskService.getTask(taskId); } /*** * 将任务重新分配给指定人 * * @param taskId * @param assignee */ public void modifyAssignee(String taskId, String assignee) { taskService.assignTask(taskId, assignee); } /*** * 获取当前活动节点位置 * * @param key * 流程发布时KEY * @param taskId * 当前任务id * @return */ public ActivityCoordinates getActivityCoordinates(String key) { ProcessInstance processInstance = getProcessInstance(key); if (processInstance != null) { Set<String> activityNames = processInstance .findActiveActivityNames(); return repositoryService.getActivityCoordinates(getProcessInstance( key).getProcessDefinitionId(), activityNames.iterator() .next()); } else { return null; } } /** * 根据流程实例id获取 * * @param executionId * @return */ public Execution findExecutionById(String executionId) { return executionService.findExecutionById(executionId); } /*** * 删除流程实例 * * @param processInstanceId */ public void deleteProcessInstanceCascade(String processInstanceId) { executionService.deleteProcessInstanceCascade(processInstanceId); } /** * 彻底删除文件的部署 * * @param deploymentId流程定义id */ public void deleteDeploymentCascade(String deploymentId) { repositoryService.deleteDeploymentCascade(deploymentId); } public ProcessEngine getProcessEngine() { return processEngine; } public void setProcessEngine(ProcessEngine processEngine) { this.processEngine = processEngine; repositoryService = processEngine.getRepositoryService(); executionService = processEngine.getExecutionService(); taskService = processEngine.getTaskService(); historyService = processEngine.getHistoryService(); managementService = processEngine.getManagementService(); } public RepositoryService getRepositoryService() { return repositoryService; } public void setRepositoryService(RepositoryService repositoryService) { this.repositoryService = repositoryService; } public ExecutionService getExecutionService() { return executionService; } public void setExecutionService(ExecutionService executionService) { this.executionService = executionService; } public TaskService getTaskService() { return taskService; } public void setTaskService(TaskService taskService) { this.taskService = taskService; } public HistoryService getHistoryService() { return historyService; } public void setHistoryService(HistoryService historyService) { this.historyService = historyService; } public ManagementService getManagementService() { return managementService; } public void setManagementService(ManagementService managementService) { this.managementService = managementService; } }