jBPM4实战系列(二)jBPM4.4+ssh 整合配置及完整实例[转]

//from http://hellotommy.iteye.com/blog/804233整合jBPM4.4+ssh过程(spring 接管struts2和hibernate,例中都整合在application.xml中,没有单独的jbpm.hibernate.cfg.xml):1.在sessionFactory的mappingLocations属性加入以下几个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>

2.jBPM自己内部的配置(spring来接管processEngine)

<bean id=”springHelper” />
<bean id=”processEngine” factory-bean=”springHelper” factory-

method=”createProcessEngine”  />

这样子就配好啦。只要在要用的地方注入processEngine就可以了!简单吧

3.当然为了编程的方便,可以自己写个工具类,直接就可以通过注入这个类来获取所需要的jBPM服务

Java代码  收藏代码
  1. package com.ht.util;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.zip.ZipInputStream;
  5. import org.jbpm.api.ExecutionService;
  6. import org.jbpm.api.HistoryService;
  7. import org.jbpm.api.ManagementService;
  8. import org.jbpm.api.ProcessDefinition;
  9. import org.jbpm.api.ProcessEngine;
  10. import org.jbpm.api.ProcessInstance;
  11. import org.jbpm.api.RepositoryService;
  12. import org.jbpm.api.TaskService;
  13. import org.jbpm.api.task.Task;
  14. /**
  15.  * jBPM4.4工具类
  16.  *
  17.  * @author ht
  18.  *
  19.  */
  20. public class JBPMUtil {
  21.     private ProcessEngine processEngine;
  22.     private RepositoryService repositoryService = null;
  23.     private ExecutionService executionService = null;
  24.     private TaskService taskService = null;
  25.     private HistoryService historyService = null;
  26.     private ManagementService managementService = null;
  27.     public JBPMUtil(){
  28.     }
  29.     public JBPMUtil(ProcessEngine processEngine) {
  30.         this.processEngine = processEngine;
  31.         repositoryService = processEngine.getRepositoryService();
  32.         executionService = processEngine.getExecutionService();
  33.         taskService = processEngine.getTaskService();
  34.         historyService = processEngine.getHistoryService();
  35.         managementService = processEngine.getManagementService();
  36.     }
  37.     public ProcessEngine getProcessEngine() {
  38.         return processEngine;
  39.     }
  40.     public void setProcessEngine(ProcessEngine processEngine) {
  41.         this.processEngine = processEngine;
  42.         System.out.println(“processEngine=”+processEngine);
  43.         repositoryService = processEngine.getRepositoryService();
  44.         executionService = processEngine.getExecutionService();
  45.         taskService = processEngine.getTaskService();
  46.         historyService = processEngine.getHistoryService();
  47.         managementService = processEngine.getManagementService();
  48.     }
  49.     public RepositoryService getRepositoryService() {
  50.         return repositoryService;
  51.     }
  52.     public void setRepositoryService(RepositoryService repositoryService) {
  53.         this.repositoryService = repositoryService;
  54.     }
  55.     public ExecutionService getExecutionService() {
  56.         return executionService;
  57.     }
  58.     public void setExecutionService(ExecutionService executionService) {
  59.         this.executionService = executionService;
  60.     }
  61.     public TaskService getTaskService() {
  62.         return taskService;
  63.     }
  64.     public void setTaskService(TaskService taskService) {
  65.         this.taskService = taskService;
  66.     }
  67.     public HistoryService getHistoryService() {
  68.         return historyService;
  69.     }
  70.     public void setHistoryService(HistoryService historyService) {
  71.         this.historyService = historyService;
  72.     }
  73.     public ManagementService getManagementService() {
  74.         return managementService;
  75.     }
  76.     public void setManagementService(ManagementService managementService) {
  77.         this.managementService = managementService;
  78.     }
  79.     /**
  80.      * 部署新流程定义
  81.      * @param resourceName
  82.      * @return 返回流程定义id
  83.      */
  84.     public String deployNew(String resourceName) {
  85.         return repositoryService.createDeployment().addResourceFromClasspath(
  86.                 resourceName).deploy();
  87.     }
  88.     /**
  89.      * 部署新流程定义(zip)
  90.      * @param resourceName
  91.      * @return 返回流程定义id
  92.      */
  93.     public String deployZipNew(String resourceZipName){
  94.         ZipInputStream zis = new ZipInputStream(this.getClass().getResourceAsStream(resourceZipName));
  95.         return repositoryService.createDeployment().addResourcesFromZipInputStream(zis).deploy();
  96.     }
  97.     /**
  98.      * 开始一个流程实例
  99.      * @param id
  100.      * @param map
  101.      * @return
  102.      */
  103.     public ProcessInstance startPIById(String id,Map<String,?> map){
  104.         return executionService.startProcessInstanceById(id, map);
  105.     }
  106.     /**
  107.      * 完成任务
  108.      * @param taskId
  109.      * @param map
  110.      */
  111.     public void completeTask(String taskId,Map map){
  112.         taskService.completeTask(taskId, map);
  113.     }
  114.     /**
  115.      * 完成任务
  116.      * @param taskId
  117.      */
  118.     public void completeTask(String taskId){
  119.         taskService.completeTask(taskId);
  120.     }
  121.     /**
  122.      * 将任务流转到指定名字的流程outcome中去
  123.      * @param taskId
  124.      * @param outcome
  125.      */
  126.     public void completeTask(String taskId,String outcome){
  127.         taskService.completeTask(taskId, outcome);
  128.     }
  129.     /**
  130.      * 获得所有发布了的流程
  131.      * @return
  132.      */
  133.     public List<ProcessDefinition> getAllPdList(){
  134.         return repositoryService.createProcessDefinitionQuery().list();
  135.     }
  136.     /**
  137.      * 获得所有流程实例
  138.      * @return
  139.      */
  140.     public List<ProcessInstance> getAllPiList(){
  141.         return executionService.createProcessInstanceQuery().list();
  142.     }
  143.     /**
  144.      * 根据流程实例Id,即executionId获取指定的变量值
  145.      * @param executionId
  146.      * @param variableName
  147.      * @return
  148.      */
  149.     public Object getVariableByexecutionId(String executionId,String variableName){
  150.         return executionService.getVariable(executionId, variableName);
  151.     }
  152.     /**
  153.      * 根据任务id,即taskId获取指定变量值
  154.      * @param taskId
  155.      * @param variableName
  156.      * @return
  157.      */
  158.     public Object getVariableByTaskId(String taskId,String variableName){
  159.         return taskService.getVariable(taskId, variableName);
  160.     }
  161.     /**
  162.      * 获取指定用户名字的任务
  163.      * @param userName
  164.      * @return
  165.      */
  166.     public List<Task> findPersonalTasks(String userName){
  167.         return taskService.findPersonalTasks(userName);
  168.     }
  169.     /**
  170.      * 根据任务id获取任务
  171.      * @param taskId
  172.      * @return
  173.      */
  174.     public Task getTask(String taskId) {
  175.         return taskService.getTask(taskId);
  176.     }
  177.     /**
  178.      * 级联删除流程定义,直接删除该流程定义下的所有实例
  179.      *
  180.      * @param deploymentId  流程定义id
  181.      */
  182.     public void deleteDeploymentCascade(String deploymentId) {
  183.         repositoryService.deleteDeploymentCascade(deploymentId);
  184.     }
  185. }

完整的application.xml:

Xml代码  收藏代码
  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans xmlns=”http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:context=”http://www.springframework.org/schema/context”
  5.      xmlns:p=”http://www.springframework.org/schema/p”
  6.     xmlns:aop=”http://www.springframework.org/schema/aop”
  7.     xmlns:tx=”http://www.springframework.org/schema/tx”
  8.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  9.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd”>
  13.     <!– 配置数据源,导入c3p0-0.9.1.2.jar–>
  14.     <bean id=”dataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource”
  15.         destroy-method=”close”>
  16.         <property name=”driverClass”>
  17.             <value>net.sourceforge.jtds.jdbc.Driver</value>
  18.         </property>
  19.         <property name=”jdbcUrl”>
  20.             <value>jdbc:jtds:sqlserver://localhost:1433;DatabaseName=jBPM</value>
  21.         </property>
  22.         <property name=”user”>
  23.             <value>sa</value>
  24.         </property>
  25.         <property name=”password”>
  26.             <value>123</value>
  27.         </property>
  28.     </bean>
  29.     <bean name=”hibernateProperties”
  30.         class=”org.springframework.beans.factory.config.PropertiesFactoryBean”>
  31.         <property name=”properties”>
  32.             <props>
  33.                 <prop key=”hibernate.dialect”>org.hibernate.dialect.SQLServerDialect</prop>
  34.                 <prop key=”hibernate.show_sql”>true</prop>
  35.                 <prop key=”hibernate.hbm2ddl.auto”>update</prop>
  36.             </props>
  37.         </property>
  38.     </bean>
  39.     <!– spring集成hibernate配置 –>
  40.     <bean id=”sessionFactory”
  41.         class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
  42.         <property name=”dataSource” ref=”dataSource” />
  43.         <property name=”hibernateProperties” ref=”hibernateProperties” />
  44.         <property name=”mappingLocations”>
  45.             <list>
  46.                 <value>classpath*:com/ht/entity/*.hbm.xml</value>
  47.                 <!– 以下几个jbpm.*.hbm.xml由jBPM自带 –>
  48.                 <value>classpath:jbpm.repository.hbm.xml</value>
  49.                 <value>classpath:jbpm.execution.hbm.xml</value>
  50.                 <value>classpath:jbpm.history.hbm.xml</value>
  51.                 <value>classpath:jbpm.task.hbm.xml</value>
  52.                 <value>classpath:jbpm.identity.hbm.xml</value>
  53.             </list>
  54.         </property>
  55.     </bean>
  56.     <!– jbpm配置 –>
  57.     <bean id=”springHelper” class=”org.jbpm.pvm.internal.processengine.SpringHelper” />
  58.     <bean id=”processEngine” factory-bean=”springHelper” factory-method=”createProcessEngine”  />
  59.     <bean id=”jBPMUtil” class=”com.ht.util.JBPMUtil”>
  60.         <property name=”processEngine” ref=”processEngine”></property>
  61.     </bean>
  62.     <!– 事务配置 –>
  63.     <bean id=”transactionManager”
  64.         class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
  65.         <property name=”sessionFactory” ref=”sessionFactory” />
  66.     </bean>
  67. </beans>

当然其他的配置还是比较多的。

This entry was posted in Computer, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s