本文提供使用jBPM工作流引擎测试的一个例子。
提供一个Persistence,用于存储全局的变量值,方便存储和获取
- public class Persistence {
- private static Map variables = null;
- private static String tmpfile = System.getProperty("java.io.tmpdir") + "/temp.object";
- static{
- //加载文件
- try{
- if(new File(tmpfile).exists()){
- FileInputStream in = new FileInputStream(tmpfile);
- ObjectInputStream s = new ObjectInputStream(in);
- variables = (Map)s.readObject();
- }
- if(variables == null){
- variables = new HashMap();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- //设置一个变量的值
- public static void setVariable(String name,Serializable object){
- if(variables != null){
- variables.put(name, object);
- }
- try {
- FileOutputStream fos = new FileOutputStream(tmpfile);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(variables);
- oos.flush();
- oos.close();
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //获取一个变量的值
- public static Serializable getVariable(String name){
- if(variables != null){
- return (Serializable)variables.get(name);
- }
- return null;
- }
- }
1.首先使用流程设计器,创建一个简单的流程规则
2.创建数据库表,创建流程定义对象,并部署流程定义
- //创建数据库表
- public class Jbpm_01_CreateTable extends TestCase {
- public void testCreateTable(){
- JbpmConfiguration.getInstance().createSchema();
- }
- }
- //定义流程定义对象,并部署流程
- public class Jbpm_02_DeployProcessDefinition extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- public void testDeployProcessDefinition(){
- //读取流程定义文件,得到流程定义对象
- ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("test01/processdefinition.xml");
- //可以得到流程定义的名称
- Persistence.setVariable("processName", processDefinition.getName());
- //JbpmContext对象封装了hibernate session 对象的功能,可以执行与数据库持久化相关的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- context.deployProcessDefinition(processDefinition);
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
3.定义公文Doucment 及其映射文件Doucment.hbm.xml
- public class Document {
- private int id;
- private String title;
- private String content;
- private Long processInstanceId;
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public Long getProcessInstanceId() {
- return processInstanceId;
- }
- public void setProcessInstanceId(Long processInstanceId) {
- this.processInstanceId = processInstanceId;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- }
- //Document映射文件Document.hbm.xml
- <hibernate-mapping>
- <class table="tbl_document" name="Document">
- <id name="id">
- <generator class="native"/>
- </id>
- <property name="title"/>
- <property name="content"/>
- <property name="processInstanceId"/>
- </class>
- </hibernate-mapping>
4.创建公文并与流程实例绑定
- public class Jbpm_03_CreateDocument extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- public void testCreateDocument(){
- //JbpmContext对象封装了hibernate session 对象的功能,可以执行与数据库持久化相关的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- Document doc = new Document();
- doc.setTitle("测试公文"+new Random().nextInt(9999));
- context.getSession().save(doc);
- Persistence.setVariable("docId", doc.getId());
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
5.提交公文到流程,触发流程流转
- public class Jbpm_05_SubmitDocument extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- //提交公文到第一个环节
- public void testSubmitDocument(){
- //JbpmContext对象封装了hibernate session 对象的功能,可以执行与数据库持久化相关的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- //已知公文的信息
- int docId = (Integer)Persistence.getVariable("docId");
- Document doc = (Document)context.getSession().load(Document.class, docId);
- long processInstanceId = doc.getProcessInstanceId();
- ProcessInstance processInstance = context.getProcessInstance(processInstanceId);
- //触发流程实例流转到下一个环节
- processInstance.signal();
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
6.查看公文所处的当前任务节点
- public class Jbpm_06_CurrentNode extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- //公文当前所处的环节
- public void testCurrentNode(){
- //JbpmContext对象封装了hibernate session 对象的功能,可以执行与数据库持久化相关的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- //已知公文的信息
- int docId = (Integer)Persistence.getVariable("docId");
- Document doc = (Document)context.getSession().load(Document.class, docId);
- long processInstanceId = doc.getProcessInstanceId();
- ProcessInstance processInstance = context.getProcessInstance(processInstanceId);
- String currentNode = processInstance.getRootToken().getNode().getName();
- System.err.println("公文【"+doc.getTitle()+"】当前所处的环节" +
- "是:"+currentNode+",流程实例是否已结束?"+processInstance.hasEnded());
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
7.获取流转个某个参与者处待处理的任务列表
- public class Jbpm_07_SearchMyTaskList extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- //搜索流转到某个参与者手上的公文列表
- public void testSearchMyTaskList(){
- //JbpmContext对象封装了hibernate session 对象的功能,可以执行与数据库持久化相关的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- printTask(context,"张三");
- printTask(context,"李四");
- printTask(context,"王五");
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- private void printTask(JbpmContext context,String actorId){
- List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
- for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
- TaskInstance ti = (TaskInstance) iter.next();
- Integer docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("documnt");
- Document doc = (Document)context.getSession().load(Document.class, docId);
- System.err.println("正在等待【"+actorId+"】审批的公文是:"+doc.getTitle());
- }
- }
- }
8.参与者执行审批操作,触发流程流转到下一个环节
- public class Jbpm_08_NextNode extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- //从一个TaskNode的中间节点出发,触发流程流转到下一个环节
- public void testNextNode(){
- //JbpmContext对象封装了hibernate session 对象的功能,可以执行与数据库持久化相关的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- //*******************************************
- //某某用户要将其手上的某某公文提交到下一个环节
- //*******************************************
- //已知要提交的公文
- Integer docId = (Integer)Persistence.getVariable("docId");
- nextStep(context,"张三",docId);
- nextStep(context,"李四",docId);
- nextStep(context,"王五",docId);
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- private void nextStep(JbpmContext context,String actorId,Integer docId){
- List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
- for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
- TaskInstance ti = (TaskInstance) iter.next();
- Integer _docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("documnt");
- //找到对应的任务实例
- if(docId.equals(_docId)){
- Document doc = (Document)context.getSession().load(Document.class, docId);
- //参与者的任务已经处理完成,需要结束这个任务实例
- //这个动作,在缺省的情况下,会触发对应的Token的signal()方法,即流向下一个环节
- ti.end();
- System.err.println("公文【"+doc.getTitle()+"】已被【"+actorId+"】审批完成,已提交到下一个环节");
- }
- }
- }
- }
【编辑推荐】