jbpm的调度应用

以前开始接触jbpm的时候,也曾经发表了一篇关于调度的文章http://blog.csdn.net/jeffen2006/archive/2006/10/20/1342167.aspx,其中有很多不甚了解的东东,这几天又研究了一下,共享给各位学友。

本系列仅从应用的角度出发进行介绍:

jbpm的调度部分只要分为2块,timer主要是流程设计人员的工作,将timer放置到流程中;scheduler是jbpm自己维护的,我们只需要在后台进行调用即可。

根据吃甘蔗的方法,我们先说相对容易一点的scheduler。我们可以认为scheduler就是一个后台线程在不停的监听着timer(jbpm_timer表),如果有需要触发的timer生成了,就按照timer的属性定时或者循环触发它。

jbpm提供了2种调用scheduler的方法:
一种是用在web应用的,采用org.jbpm.scheduler.impl.SchedulerServlet,具体的方法这个类的javadoc有很好的示例,我们只需在web.xml中加载它就行了;
另一种是针对的c-s程序,jbpm提供了一个很好的示例org.jbpm.scheduler.impl.SchedulerMain,我们可以参照它编写我们自己的Scheduler。

下面我就编写一个cs程序来实现Scheduler,并调用一个最简单的timer。

这个timer从第5秒开始每隔3秒执行script中的内容。

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='5 seconds'    
  9.              repeat='3 seconds'  
  10.              >  
  11.         <script>System.out.println(new Date()+"----node enter:send mail to operator.");script>  
  12.      timer>  
  13.       <transition name="" to="end">transition>  
  14.    state>  
  15.    <end-state name="end">end-state>  
  16.        
  17. process-definition>  

下面的程序看注释就很清楚了:

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生成,触发   
  36.                    token.signal();   
  37.                    System.out.println(token.getNode());   
  38.                    jbpmContext.save(processInstance);   
  39.                    //如果这里程序到这里退出的话可以看到jbpm_timer表里有一条数据   
  40.                    Thread.sleep(30*1000);//为模拟效果,此线程停止30秒   
  41.                    //节点跳过,timer结束,jbpm_timer表该数据清空   
  42.                    token.signal();   
  43.                    System.out.println(token.getNode());   
  44.                    jbpmContext.save(processInstance);   
  45.             }catch(Exception e){   
  46.                 e.printStackTrace();   
  47.             }finally {   
  48.                   jbpmContext.close();   
  49.             }   
  50.         }   
  51.     }   
  52.            
  53.     public static void main(String[] args) {   
  54.         initSchedular ();   
  55.         MySchedularThread mst=new MySchedularThread();   
  56.         mst.start();   
  57.     }   
  58. }   

运行结果:

StartState(start)
State(a)
Thu Dec 07 13:17:11 CST 2006----node enter:send mail to operator.
Thu Dec 07 13:17:16 CST 2006----node enter:send mail to operator.
Thu Dec 07 13:17:21 CST 2006----node enter:send mail to operator.
Thu Dec 07 13:17:26 CST 2006----node enter:send mail to operator.
Thu Dec 07 13:17:31 CST 2006----node enter:send mail to operator.
EndState(end)

scheduler就先说这么多了,至于timer等我下篇吧,等下要去参加jbuilder2007的深圳发布会。

 

上篇介绍了通过跳过节点可以终止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节点的,见下篇吧。

 

timer如果在task中,有一个属性cancel-event,可以指定那些事件可以终止timer的执行,默认是task-end,也可以指定多个事件如:

下面举一个默认为task-end的例子:

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.    <task-node name='a'>  
  7.         <task name='reminder'>  
  8.             <timer name='reminder' duedate='5 seconds' repeat='3 seconds'  
  9.                      >  
  10.                 <script>System.out.println(new Date()+"----node enter:send mail to operator.");script>  
  11.             timer>  
  12.         task>  
  13.         <transition name="" to="end">transition>  
  14.     task-node>  
  15.    <end-state name="end">end-state>       
  16. process-definition>  

 

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生成,触发   
  36.                    token.signal();   
  37.                    System.out.println(token.getNode());   
  38.                    jbpmContext.save(processInstance);   
  39.                    //jbpm_timer表里有一条数据。timer将永久运行下去,直到taskinstance.end()   
  40.                    Thread.sleep(30*1000);//为模拟效果,此线程停止30秒   
  41.                    jbpmContext.getTaskInstance(1).start();   
  42.                    jbpmContext.getTaskInstance(1).end();//如果,屏蔽掉该句试试   
  43.                    System.out.println(token.getNode());       
  44.                    jbpmContext.save(processInstance);     
  45.             }catch(Exception e){   
  46.                 e.printStackTrace();   
  47.             }finally {   
  48.                   jbpmContext.close();   
  49.             }   
  50.         }   
  51.     }   
  52.            
  53.     public static void main(String[] args) {   
  54.         initSchedular ();   
  55.         MySchedularThread mst=new MySchedularThread();   
  56.         mst.start();   
  57.     }   
  58. }   

运行结果如下:

StartState(start)
TaskNode(a)
Fri Dec 08 11:22:17 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:22:22 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:22:27 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:22:32 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:22:37 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:22:42 CST 2006----node enter:send mail to operator.
EndState(end)

如果想要task启动的时候,timer就终止,可以这样配置

xml 代码
  1. <timer name='reminder' duedate='5 seconds' repeat='3 seconds' cancel-event='task-start'>  

结果如下:

StartState(start)
TaskNode(a)
Fri Dec 08 11:35:59 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:36:04 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:36:09 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:36:14 CST 2006----node enter:send mail to operator.
Fri Dec 08 11:36:19 CST 2006----node enter:send mail to operator.
TaskNode(a)

好了调度的应用就说这么多吧。没人了,赶紧去吃饭。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值