ITeye博客

jbpm的调度应用2

jeffen2006 · 2006-12-08 10:42 · jboss-jbpm · 企业架构 · XML · JBPM · thread · Markdown

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

 

运行结果:

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节点的,见下篇吧。

评论(1)

  1. #1 楼 · tomkoo · 2006-12-13 09:29

    现在项目中还没有要用到Timer的地方,不过看了这个系列的文章以后,也对Timer有了一个了解,哈哈!

评论功能维护中,当前仅展示历史评论。