jbpm工作流


一.開發環境的搭建

1.下載Jbpm4.4

1.1下載地址:

https://sourceforge.net/projects/jbpm/files/jBPM%204/jbpm-4.4/

1.2解壓后的目錄

 

 

2.安裝GPD插件(eclipse圖形化流程設計器)

2.1打開eclipse--------選擇help--------選擇install new software

   

 

2.2選擇add添加插件:

  

 

點擊Archive選擇插件目錄:

插件目錄所在位置就是下載的解壓后的路徑中找jbpm-4.4\install\src\gpd

 

 

 

2.3選擇插件

后續直接下一步就行,等待安裝插件。

1.配置運行時的環境

打開eclipse-------------preference

 

 

 

1.配置JPDL xml的schema dtd

4.1目的

有時在建jpdlxml文件的時候沒有節點的提示,我們配置一下這個schema dtd,就有提示了。

4.2配置步驟

打開eclipse-------------preference

二.JBPM簡介

2.1什么是jbpm

   JBPM,全稱是Java Business Process Management(業務流程管理),它是覆蓋了業務流程管理、工作流、服務協作等領域的一個開源的、靈活的、易擴展的可執行流程語言框架。

2.2為什么用jbpm

     業務分析師和開發人員使用的是同一種語言來交談,大大降低了開發的風險,如果要開發一個項目,速度也更快了,因為開發人員不用再將用戶需求轉化成軟件設計了。其次,JBPM采用的不是一般的開發工具,而是自己的圖形化開發工具,非常方便隨時了解和掌握運行的進程。最后,JBPM的流程框架非常靈活,使用起來也非常安全,它經過了安全認證,因此可以保證一些商業機密不被泄露。

.開發步驟

1.引入jar包

      在jbpm4.4的解壓包中根目錄Jbpm的jar包還有lib文件夾中除report-engine,war包外所有jar包

注:因為Jbpm4.4是用Mysql作為DB的,如果要用Oracle作為DB需要引入相應的驅動包。

2.創建配置文件

 

復制代碼
<?xml version="1.0" encoding="utf-8"?>

 

<!DOCTYPE hibernate-configuration PUBLIC

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

  <session-factory>

      <!-- 連接數據庫的一些信息 -->

     <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

     <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

     <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>

     <property name="hibernate.connection.username">jbpm</property>

     <property name="hibernate.connection.password">jbpm</property>

     <property name="hibernate.hbm2ddl.auto">update</property>

     <property name="hibernate.format_sql">true</property>

     <!-- 小配置:映射文件 -->

     <mapping resource="jbpm.repository.hbm.xml" />

     <mapping resource="jbpm.execution.hbm.xml" />

     <mapping resource="jbpm.history.hbm.xml" />

     <mapping resource="jbpm.task.hbm.xml" />

     <mapping resource="jbpm.identity.hbm.xml" />

  </session-factory>

</hibernate-configuration>
復制代碼

 

 

 

3.畫流程圖

  新建jpdl.xml文件

4.創建表結構

  

復制代碼
 /**

 * 創建表結構:有18張表

 */

@Test

public void createTable(){

  // 讀取配置文件  

  new org.hibernate.cfg.Configuration().configure("jbpm.hibernate.cfg.xml").buildSessionFactory();

}
復制代碼

 

 

18張表的含義:

http://www.cnblogs.com/a757956132/p/4774188.html

5.獲取工作流引擎

復制代碼
//獲取工作流引擎

//ProcessEngine processEngine = new Configuration().setResource("jbpm.cfg.xml").buildProcessEngine();

 

 

//獲取工作流引擎

ProcessEngine processEngine =Configuration.getProcessEngine();
復制代碼

 

6.部署流程定義

 

復制代碼
//獲取工作流引擎

//ProcessEngine processEngine = new Configuration().setResource("jbpm.cfg.xml").buildProcessEngine();

 

 

//獲取工作流引擎

ProcessEngine processEngine =Configuration.getProcessEngine();
復制代碼

 

7.啟動流程實例

復制代碼
//獲取工作流引擎

//ProcessEngine processEngine = new Configuration().setResource("jbpm.cfg.xml").buildProcessEngine();

 

 

//獲取工作流引擎

ProcessEngine processEngine =Configuration.getProcessEngine();
復制代碼

 

8.查詢任務

   

復制代碼
 @Test

public void selectMyStack(){

//根據指定的人員去查看他的任務

List<Task> stacks= processEngine.getTaskService().findPersonalTasks("部門經理");

System.out.println("經理的任務");

for (Task task : stacks) {

System.out.println("任務ID:"+task.getId()+"\t任務名稱:"+task.getName()+"\t任務的辦理人:"+task.getAssignee());

}

}
復制代碼

 

9.查詢當前任務所在節點

復制代碼
@Test

public void getActivity(){

String name=processEngine.getExecutionService().createProcessInstanceQuery().processInstanceId("jbpm01.10001").uniqueResult().findActiveActivityNames().toString();

System.out.println("當前任務所在節點:"+name);

}
復制代碼

 

10.辦理任務

 

復制代碼
@Test

public void saveTask(){

processEngine.getTaskService().completeTask("80002");

System.out.println("任務辦理成功");

}
復制代碼

 

       

 

四.流程管理

4.1部署流程

     

4.2查看所有流程定義的信息

復制代碼
@Test

public void findAll(){

List<ProcessDefinition> list = pe.getRepositoryService().createProcessDefinitionQuery().list();

for (ProcessDefinition pd : list) {

/**

 * id的格式  name-版本號

 * name:jbpm.cfg.xml文件中根據點的name屬性值

 * key,如果在配置文件中指定了,那么就是根據點中的test的 值,如果不指定默認是name的值

 * version版本號,同一個名稱的第一個為1后續的自增

 */

System.out.println("id:"+pd.getId()+"\tname:"+pd.getName()+"\tkey"+pd.getKey()+"\t版本號:"+pd.getVersion()+"\t部署的id:"+pd.getDeploymentId());

}

}
復制代碼

 

 

4.3刪除指定id流程信息

  

復制代碼
@Test

public void deleteByid(){

pe.getRepositoryService().deleteDeploymentCascade("100001");

System.out.println("刪除成功");

}
復制代碼

 

4.4刪除指定key的所有流程信息

復制代碼
 @Test

public void deleteByids(){

//01.取出當前key對應的版本信息

List<ProcessDefinition> list = pe.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey("jbpmtest").list();

for (ProcessDefinition pd : list) {

  pe.getRepositoryService().deleteDeploymentCascade(pd.getDeploymentId());

}

System.out.println("刪除成功");

}
復制代碼

 

4.5獲取流程圖

 

復制代碼
@Test

public void getImage() throws Exception{

String deploymentId="10001";

String resouceName="jbpmtest.png";

//根據id去查詢所有對應的資源名

Set<String> resourceNames = pe.getRepositoryService().getResourceNames(deploymentId);

for(String item: resourceNames){

System.out.println("資源名稱:"+item);

}

InputStream in=pe.getRepositoryService().getResourceAsStream(deploymentId, resouceName);

OutputStream os=new FileOutputStream("D:\\1.png");

int a=in.read();

while (a!=-1) {

os.write(a);

a=in.read();

}

    in.close();

    os.close();

    System.out.println("成功");

}
復制代碼

 

 

4.6部署zip格式的資源

復制代碼
public void addZipResource(){

InputStream is=this.getClass().getClassLoader().getResourceAsStream("two.zip");

ZipInputStream zs=new ZipInputStream(is);

String id= pe.getRepositoryService().createDeployment().addResourcesFromZipInputStream(zs).deploy();

  System.out.println("部署成功:deploymentid:"+id);

}
復制代碼

 

4.7找到所有最新版本的流程定義

復制代碼
@Test

public void findNewVersionInfo(){

//查詢所有

List<ProcessDefinition> list = pe.getRepositoryService().createProcessDefinitionQuery()//

.orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION)

.list();

Map<String,ProcessDefinition> map=new java.util.HashMap<String,ProcessDefinition>();

for (ProcessDefinition item : list) {

map.put(item.getKey(),item);

}

for (ProcessDefinition pd : map.values()) {

System.out.println("id:"+pd.getId()+"\tname:"+pd.getName()+"\tkey"+pd.getKey()+"\t版本號:"+pd.getVersion()+"\t部署的id:"+pd.getDeploymentId());

}

}
復制代碼

 

4.8設置流程變量

復制代碼
public void setVariable() {

pe.getExecutionService().setVariable("jbpm01.90001", "請假天數", 18);

}

 

@Test

public void getVariable() {

Integer days = (Integer) pe.getExecutionService().getVariable("jbpm01.90001", "請假天數");

System.out.println(days);

}
復制代碼

 

五.JBPM和SSH整合

   目的:整合jBPM的目的就是能夠通過注入的方式得到ProcessEngine實例,因為ProcessEngine是jbpm 的核心.

1.關於jar包

在Tomcat/lib目錄下添加jBPM軟件包里的下列三個jar包,刪掉Tomact/lib下的el-api

 

 

2.關於配置文件

修改jbpm.cfg.xml文件

復制代碼
<?xml version="1.0" encoding="UTF-8"?>

<jbpm-configuration>

  <import resource="jbpm.default.cfg.xml" />

  <import resource="jbpm.businesscalendar.cfg.xml" />

  <!-- Hibernate的事務管理器 -->

  <!-- <import resource="jbpm.tx.hibernate.cfg.xml" /> -->

  <!-- 修改為Spring的事務管理器 -->

  <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" />

 

  <!-- Job executor is excluded for running the example test cases. -->

  <!-- To enable timers and messages in production use, this should be included. -->

  <!--

  <import resource="jbpm.jobexecutor.cfg.xml" />

  -->

 

</jbpm-configuration>

 

修改applicationcontext.xml文件

 

<?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:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

        http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/tx

       http://www.springframework.org/schema/tx/spring-tx.xsd

       http://www.springframework.org/schema/aop

       http://www.springframework.org/schema/aop/spring-aop.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context.xsd

        ">

<!-- 掃描包中注解標注的類 -->

<context:component-scan base-package="cn"></context:component-scan>

 

<!-- 1.引入jdbc.properties文件 -->

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location">

<value>classpath:jdbc.properties</value>

</property>

</bean>

<!-- 2.配置dataSource c3p0數據源 -->

<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<!-- 數據庫連接信息 -->

<property name="jdbcUrl" value="${jdbc.url}"></property>

<property name="driverClass" value="${jdbc.driver}"></property>

<property name="user" value="${jdbc.username}"></property>

<property name="password" value="${jdbc.password}"></property>

<!-- 其他配置 -->

<!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->

<property name="initialPoolSize" value="3"></property>

<!--連接池中保留的最小連接數。Default: 3 -->

<property name="minPoolSize" value="3"></property>

<!--連接池中保留的最大連接數。Default: 15 -->

<property name="maxPoolSize" value="5"></property>

<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->

<property name="acquireIncrement" value="3"></property>

<!-- 控制數據源內加載的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default:

0 -->

<property name="maxStatements" value="8"></property>

<!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 -->

<property name="maxStatementsPerConnection" value="5"></property>

<!--最大空閑時間,1800秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0 -->

<property name="maxIdleTime" value="1800"></property>

</bean>

 

<!-- 3.定義sessionFactory -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- 3.1為LocalSessionFactoryBean注入定義好的數據源 -->

<property name="dataSource">

<ref bean="datasource" />

</property>

<!--3.2添加hibernate配置參數 -->

<!--添加hibernate配置參數 -->

<property name="hibernateProperties">

<props>

<!-- 每個數據庫都有其對應的Dialect以匹配其平台特性 -->

<prop key="hibernate.dialect">

org.hibernate.dialect.Oracle10gDialect

</prop>

<!-- 是否將運行期生成的sql輸出到日志以供調試 -->

<prop key="hibernate.show_sql">

true

</prop>

<!-- 是否格式化sql -->

<prop key="hibernate.format_sql">

true

</prop>

<prop key="hibernate.hbm2ddl.auto">

update

</prop>

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext

</prop>

 

</props>

</property>

<!-- 指定資源文件 -->

<property name="mappingResources">

<list>

<value>jbpm.repository.hbm.xml</value>

<value>jbpm.execution.hbm.xml</value>

<value>jbpm.history.hbm.xml</value>

<value>jbpm.task.hbm.xml</value>

<value>jbpm.identity.hbm.xml</value>

</list>

</property>

</bean>

<!-- 配置 ProcessEngine對象 -->

<bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper">

<property name="jbpmCfg" value="jbpm.cfg.xml"></property>

</bean>

<bean id="processEngine" factory-bean="springHelper"

factory-method="createProcessEngine" />

 

<!-- 定義事務管理器 -->

<bean id="txManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<tx:annotation-driven transaction-manager="txManager" />

</beans>
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM