ITeye博客

jbpm的调度应用3-end

jeffen2006 · 2006-12-08 11:38 · jboss-jbpm · 企业架构 · XML · JBPM · thread · 多线程 · Markdown

timer如果在task中,有一个属性cancel-event,可以指定那些事件可以终止timer的执行,默认是task-end,也可以指定多个事件如:<timer duedate="2 business minutes" cancel-event="task-start, task-assign, task-end"></timer>。

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

运行结果如下:

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)

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

评论(3)

  1. #3 楼 · delphidoc · 2007-07-19 15:19

    我用jbpmContext.deployProcessDefinition(processDefinition);发布流程后,jbpm_timer中没有我在流程中定义的timer,是什么原因引起的?

  2. #2 楼 · hantsy · 2007-06-27 16:54

    这是3.1的,jBPM 3.2中没有Scheduler

  3. #1 楼 · liu_hua1984cf · 2007-01-15 16:30

    我的怎么 不再jbpmContext.save(processInstance);后面加上 jbpmContext.close()数据库中就没有记录 调度就不执行

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