原创作者: jeffen2006   阅读:4889次   评论:0条   更新时间:2011-05-26    

上篇介绍了通过跳过节点可以终止timer,其实也可以直接在流程定义里设置timer的终止,就是使用cancel-timer元素。

xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <process-definition xmlns="" name="yytest">  
  3.    <start-state name="start">  
  4.       <transition name="" to="a"></transition>  
  5.    </start-state>  
  6.    <state name='a'>  
  7.      <timer name='reminder'    
  8.              duedate='0 seconds'    
  9.              repeat='3 seconds'>  
  10.         <script>System.out.println(new Date()+"----node enter:send mail to operator.");</script>  
  11.      </timer>  
  12.      <timer name='reminderend'    
  13.              duedate='12 seconds'  
  14.              transition='toend'  
  15.              >  
  16.         <script>System.out.println(new Date()+"----canceled timer");</script>  
  17.         <cancel-timer name='reminder'/>  
  18.      </timer>  
  19.      <transition name="toend" to="end"></transition>  
  20.     </state>  
  21.    <end-state name="end"></end-state>       
  22. </process-definition>  

reminderend 这个定时器,在12秒后调用cancel-timer终止定时器reminder,同时按照指定的transition结束流程。

java 代码
  1. package com.jeffentest;   
  2.   
  3. import org.jbpm.*;   
  4. import org.jbpm.graph.def.ProcessDefinition;   
  5. import org.jbpm.graph.exe.*;   
  6. import org.jbpm.scheduler.impl.Scheduler;   
  7.   
  8.   
  9. public class Jeffentest {   
  10.     static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();   
  11.     static ProcessDefinition processDefinition = null;   
  12.     static ProcessInstance processInstance = null;   
  13.     static Scheduler scheduler = null;   
  14.   
  15.     public static void initSchedular() {//设置Schedular的属性   
  16.         scheduler = new Scheduler();   
  17.         int interval = 5000;   
  18.         scheduler.setInterval(interval);   
  19.         int historyMaxSize = 0;   
  20.         scheduler.setHistoryMaxSize(historyMaxSize);   
  21.         scheduler.start();   
  22.     }   
  23.          
  24.     public static void destroy() {//这个例子没用到   
  25.         scheduler.stop();   
  26.     }   
  27.     static class MySchedularThread extends Thread{//实际业务处理线程   
  28.         public void run(){   
  29.             JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();   
  30.             try {   
  31.                 long processInstanceId =1;   
  32.                    processInstance = jbpmContext.loadProcessInstance(processInstanceId);   
  33.                    Token token = processInstance.getRootToken();   
  34.                    System.out.println(token.getNode());   
  35.                    //一定要运行到有timer生成,触发<timer name='reminder'>   
  36.                    token.signal();   
  37.                    System.out.println(token.getNode());   
  38.                    jbpmContext.save(processInstance);   
  39.                    //jbpm_timer表里数据被清空、同时可以看jbpm_processinstance.end_,该流程实例也结束了   
  40.             }catch(Exception e){   
  41.                 e.printStackTrace();   
  42.             }finally {   
  43.                   jbpmContext.close();   
  44.             }   
  45.         }   
  46.     }   
  47.            
  48.     public static void main(String[] args) {   
  49.         initSchedular ();   
  50.         MySchedularThread mst=new MySchedularThread();   
  51.         mst.start();   
  52.     }   
  53. }   

 

运行结果:

StartState(start)
State(a)
Fri Dec 08 10:22:26 CST 2006----node enter:send mail to operator.
Fri Dec 08 10:22:31 CST 2006----node enter:send mail to operator.
Fri Dec 08 10:22:33 CST 2006----node enter:send mail to operator.
Fri Dec 08 10:22:33 CST 2006----canceled timer

说明一下,我的这两个例子都是基于state的,如果采用node则不会给timer运行的机会。

timer还有几个特殊属性是针对task节点的,见下篇吧。

评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics