JBPM 实践

                   JBPM 实践
环境
     安装Eclipse3.3
     安装MyEclipse6.0
     安装Tomcat5.5
     安装MySQL5.0
     下载JBoss jBPM Starters Kit 3.1.2

下载后解压到 D:\jbpm-starters-kit-3.1.2。目录下含有五个子目录:

jbpm jBPM的软件包,包含源码,例子等文件内容。
jbpm-bpel只含有一个网页
jbpm-db各种数据库hibernate配置文件示例,有些还包含了相应的jdbc驱动程序。
jbpm-designer辅助开发jBPM的Eclipse插件,具体在jbpm-gpd-feature子目录中
jbpm-server一个已经配置好了的基于JBoss的jBPM示例。

安装jBPM的Eclipse开发插件(略)

配置
1.导入工程
启动myEclipse,通过“Import”将jBPM工程导入eclipse中.
2.建库
在MySQL中建立数据库jbpm。字符集采用GBK。建表SQL如下:
CREATE DATABASE `jbpm`
    CHARACTER SET 'gbk'
    COLLATE 'gbk_chinese_ci';
3.添加MySQL驱动
在工程jbpm的lib目录下建立mysql文件夹,将MySQL需要的JDBC驱动程序拷贝其中。
4.建立数据库表
在工程jbpm的src/resources目录下建立mysql目录,并将jbpm/src/resources/hsqldb目录下的所有文件(create.db.hibernate.properties和identity.db.xml)拷贝入mysql中.
5修改create.db.hibernate.properties
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://192.200.1.104:3306/jbpm
hibernate.connection.username=zhoucw
hibernate.connection.password=0632
hibernate.show_sql=true
6 修改build.deploy.xml
在<target name="create.db" depends="declare.jbpm.tasks, db.clean, db.start"
description="creates a hypersonic database with the jbpm tables and loads the processes in there">前面添加
    <target
name="create.db.mysql"
depends="declare.jbpm.tasks"
description="creates a mysql database with the jbpm tables and loads the processes in there.
Run this target after starting mysql">
<jbpmschema
actions="create"
cfg="${basedir}/src/config.files/hibernate.cfg.xml"
properties="${basedir}/src/resources/mysql/create.db.hibernate.properties" />
<loadidentities
file="${basedir}/src/resources/mysql/identity.db.xml"
cfg="${basedir}/src/config.files/hibernate.cfg.xml"
properties="${basedir}/src/resources/mysql/create.db.hibernate.properties" />
<ant antfile="build.xml"
target="build.processes"
inheritall="false" />
<deployprocess
cfg="${basedir}/src/config.files/hibernate.cfg.xml"
properties="${basedir}/src/resources/mysql/create.db.hibernate.properties">
<fileset dir="build" includes="*.process" />
</deployprocess>
</target>
<property
file="${basedir}/src/resources/mysql/create.db.hibernate.properties" />
<target
name="execute.mysql.sql"
description="execute a mysql sql script with -Dmysql.script=path-to-script.">
<sql
driver="${hibernate.connection.driver_class}"
url="${hibernate.connection.url}" userid="${hibernate.connection.username}"
password="${hibernate.connection.password}"
delimiter=";"
autocommit="true"
onerror="continue">
<classpath
refid="classpath.ant" />
<transaction
src="${mysql.script}" />
</sql>
</target>
在myeEclipse中,右键点击工程jbpm下的build.deploy.xml文件,在菜单中选择“Run As\Ant Build…”,在出现的窗口中选择“target”为“create db mysql”,然后点击“Run”按钮,开始运行Ant。

7 修改hibernate.cfg.xml
在myeclipse中打开工程jbpm下的src/config.files目录下的hibernate.cfg.xml文件,修改其中的内容为:
<!-- jdbc connection properties -->
    <property name="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </property>
    <property name="hibernate.connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.connection.url">
    jdbc:mysql://192.200.1.104:3306/jbpm
    </property>
    <property name="hibernate.connection.username">zhoucw</property>
    <property name="hibernate.connection.password">0632</property>
    <!-- other hibernate properties -->
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">false</property>
    <property name="hibernate.use_sql_comments">false</property>

8 修改build.deploy.xml
在eclipse中打开工程jbpm下的build.deploy.xml文件,找到如下“target”,<target name="build.webapp" description="builds jbpm.war">
并将此“target”内容更改为如下内容:
<target name="build.webapp" description="builds jbpm.war">
<ant antfile="build.xml" target="build.webapp" />
<mkdir dir="build/jbpm.war.dir" />
<copy todir="build/jbpm.war.dir">
<fileset dir="src/resources/jbpm.war" />
</copy>
<copy todir="build/jbpm.war.dir/WEB-INF/classes">
<fileset dir="src/config.files" />
</copy>
<copy todir="build/jbpm.war.dir/WEB-INF/lib">
<fileset dir="build"
includes="jbpm*.jar,converter.jar"
excludes="*src*.jar" />
<fileset dir="lib/jsf" includes="*.jar" />
<fileset dir="lib/dom4j" includes="*.jar" />
<fileset dir="lib/hibernate" includes="*.jar" />
<fileset dir="lib/mysql" includes="*.jar" />
<fileset dir="lib/commons"
includes="commons-digester-*.jar,
commons-beanutils-*.jar,
commons-fileupload-*.jar" />
<fileset dir="lib/jboss"
includes="*.jar"
excludes="jboss*.jar,
*servlet*.jar,
*hsql*.jar" />
</copy>
<jar destfile="build/jbpm.war">
<fileset dir="build/jbpm.war.dir" />
</jar>
</target>
打包成war文件
在eclipse中,右键点击工程jbpm.3下的build.deploy.xml文件,在菜单中选择“Run As\Ant Build…”,在出现的窗口中选择“target”为“build webapp”,然后点击“Run”按钮,开始运行Ant。



搭建一个例子
    建立流程图如下:
         
  JBPM 实践_第1张图片  

processdefinition.xml 内容如下:
<?xml version="1.0" encoding="UTF-8"?>

<process-definition
  xmlns="urn:jbpm.org:jpdl-3.1"  name="testNo2">
   <start-state name="start">
      <task name="apply account">
      <controller>
         <variable name="baoxiaoId" access="read,write,required" mapped-name="baoxiao_ID"></variable>
         <variable name="baoxiaoNum" access="read,write,required" mapped-name="baoxiao_num"></variable>
      </controller>
      <assignment class="com.test.action.UserAssignment"></assignment>
      </task>
      <transition name="" to="to direct"></transition>
   </start-state>
  
   <decision name="to direct">
      <handler class="com.test.action.DecisionProcess"/>
      <transition name="to caiwu" to="caiwu check apply"></transition>
      <transition name="to boss" to="boss signature apply"></transition>
   </decision>
   <task-node name="boss signature apply">
      <task name="boss signature">
      <controller>
      <variable name="baoxiaoId" access="read" mapped-name="baoxiao_ID"></variable>
      <variable name="baoxiaoNum" access="read" mapped-name="baoxiao_num">
      </variable>
      </controller>
      <assignment class="com.test.action.BossAssignment">
      </assignment>
      </task>
      <transition name="to confirm" to="confirm apply1"></transition>
   </task-node>
   <task-node name="caiwu check apply">
      <task name="caiwu check">
      <controller>
      <variable name="baoxiaoId" access="read" mapped-name="baoxiao_ID"></variable>
      <variable name="baoxiaoNum" access="read" mapped-name="baoxiao_num">
      </variable>
      </controller>
      <assignment class="com.test.action.CaiWuAssignment">
      </assignment>
      </task>
      <transition name="to confirm" to="confirm apply2"></transition>
   </task-node>
  
   <task-node name="confirm apply1">
      <task name="confirm1">
      <assignment class="com.test.action.UserAssignment"></assignment>
     
      </task>
      <transition name="" to="end1"></transition>
   </task-node>
  
   <task-node name="confirm apply2">
      <task name="confirm2">
      <assignment class="com.test.action.UserAssignment"></assignment>
     
      </task>
      <transition name="" to="end1"></transition>
   </task-node>
  
   <end-state name="end1"></end-state>
  
 
</process-definition>

其中涉及到的类如下:
BossAssignment
package com.test.action;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.def.AssignmentHandler;
import org.jbpm.taskmgmt.exe.Assignable;

public class BossAssignment implements AssignmentHandler {

public void assign(Assignable assignable, ExecutionContext executionContext)
throws Exception {
  assignable.setActorId("boss1");
// TODO Auto-generated method stub

}

}

package com.test.action;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.def.AssignmentHandler;
import org.jbpm.taskmgmt.exe.Assignable;

public class CaiWuAssignment implements AssignmentHandler {

public void assign(Assignable assignable, ExecutionContext executionContext)
throws Exception {
assignable.setActorId("Caiwu1");
// TODO Auto-generated method stub

}

}
CaiWuAssignment
package com.test.action;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.def.AssignmentHandler;
import org.jbpm.taskmgmt.exe.Assignable;

public class CaiWuAssignment implements AssignmentHandler {

public void assign(Assignable assignable, ExecutionContext executionContext)
throws Exception {
assignable.setActorId("Caiwu1");
// TODO Auto-generated method stub

}

}

DecisionProcess
package com.test.action;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.node.DecisionHandler;

public class DecisionProcess implements DecisionHandler {

public String decide(ExecutionContext executionContext) throws Exception {
// TODO Auto-generated method stub
String go="";
Object baoxiaoId=executionContext.getContextInstance().getVariable("baoxiaoId");
Object baoxiaoNum=executionContext.getContextInstance().getVariable("baoxiaoNum");

if(Integer.parseInt((String)baoxiaoNum) >1000){
go="to boss";
}
else{

go="to caiwu";
}

return go;
}

}
UserAssignment
package com.test.action;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.def.AssignmentHandler;
import org.jbpm.taskmgmt.exe.Assignable;

public class UserAssignment implements AssignmentHandler {

public void assign(Assignable assignable, ExecutionContext executionContext)
throws Exception {
   assignable.setActorId("actor1");
// TODO Auto-generated method stub

}

}

发布到数据库以后,作为客户端业务层的java类如下:
No1Actor1ConfirmTest
package com.test;

import java.util.List;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.exe.TaskInstance;

public class No1Actor1ConfirmTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String user_name = "actor1";
JbpmConfiguration config=JbpmConfiguration.getInstance();
JbpmContext jbpmContext=config.createJbpmContext();
List ls=jbpmContext.getTaskList(user_name);
int num = ls.size();
System.out.println(String.valueOf(num));
for(int i= 0;i<num;i++){
TaskInstance  ti = (TaskInstance)ls.get(i);
System.out.println(ti.getActorId()+"完成"+ti.getName());
ti.end();
}
jbpmContext.close();
}

}

No1Actor1Test
package com.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;

public class No1Actor1Test {

/**
* @param args
*/
public static void main(String[] args) {
String user_name = "actor1";
String baoxiaoNum = "1300";
JbpmConfiguration config=JbpmConfiguration.getInstance();
JbpmContext jbpmContext=config.createJbpmContext();
Session session=jbpmContext.getSessionFactory().openSession();
Transaction trans=session.beginTransaction();
ProcessDefinition pd=jbpmContext.getGraphSession().findLatestProcessDefinition("testNo2");
ProcessInstance pi=pd.createProcessInstance();
TaskInstance ti=pi.getTaskMgmtInstance().createStartTaskInstance();
    long pi_id= pi.getId();
    No1Constant.baoxiaoId =pi_id;
ti.setVariable("baoxiaoId",pi_id);
ti.setVariable("baoxiaoNum",baoxiaoNum);
ti.end();
trans.commit();
session.close();
jbpmContext.close();
}

}

No1Boss1Test
package com.test;

import java.util.List;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.taskmgmt.exe.TaskInstance;


public class No1Boss1Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String user_name = "boss1";
JbpmConfiguration config=JbpmConfiguration.getInstance();
JbpmContext jbpmContext=config.createJbpmContext();

List ls=jbpmContext.getTaskList(user_name);
int num = ls.size();
System.out.println(String.valueOf(num));
for(int i= 0;i<num;i++){
TaskInstance  ti = (TaskInstance)ls.get(i);
System.out.println(ti.getActorId()+"完成"+ti.getName());
ti.end();
}
jbpmContext.close();
}

}
No1CaiwuTest
package com.test;

import java.util.List;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.exe.TaskInstance;

public class No1CaiwuTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String user_name = "Caiwu1";
JbpmConfiguration config=JbpmConfiguration.getInstance();
JbpmContext jbpmContext=config.createJbpmContext();
//TaskInstance ti = jbpmContext.getTaskInstance(19);

ProcessInstance pi=jbpmContext.getProcessInstance(12);
Token token = pi.getRootToken();
System.out.println(token.getNode().getName());
//System.out.println(ti.getActorId());
List ls=jbpmContext.getTaskList(user_name);

int num = ls.size();
System.out.println(String.valueOf(num));
for(int i= 0;i<num;i++){
TaskInstance  ti = (TaskInstance)ls.get(i);
System.out.println(ti.getActorId()+"完成"+ti.getName());
ti.end();
}
jbpmContext.close();
}

}
No1Constant
package com.test;

public class No1Constant {
public static long baoxiaoId=0;
}

你可能感兴趣的:(eclipse,mysql,Hibernate,jdbc,jbpm)