上篇介绍了通过跳过节点可以终止timer,其实也可以直接在流程定义里设置timer的终止,就是使用cancel-timer元素。
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <process-definition xmlns="" name="yytest">
- <start-state name="start">
- <transition name="" to="a"></transition>
- </start-state>
- <state name='a'>
- <timer name='reminder'
- duedate='0 seconds'
- repeat='3 seconds'>
- <script>System.out.println(new Date()+"----node enter:send mail to operator.");</script>
- </timer>
- <timer name='reminderend'
- duedate='12 seconds'
- transition='toend'
- >
- <script>System.out.println(new Date()+"----canceled timer");</script>
- <cancel-timer name='reminder'/>
- </timer>
- <transition name="toend" to="end"></transition>
- </state>
- <end-state name="end"></end-state>
- </process-definition>
reminderend 这个定时器,在12秒后调用cancel-timer终止定时器reminder,同时按照指定的transition结束流程。
java 代码
- package com.jeffentest;
-
- import org.jbpm.*;
- import org.jbpm.graph.def.ProcessDefinition;
- import org.jbpm.graph.exe.*;
- import org.jbpm.scheduler.impl.Scheduler;
-
-
- public class Jeffentest {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- static ProcessDefinition processDefinition = null;
- static ProcessInstance processInstance = null;
- static Scheduler scheduler = null;
-
- public static void initSchedular() {
- scheduler = new Scheduler();
- int interval = 5000;
- scheduler.setInterval(interval);
- int historyMaxSize = 0;
- scheduler.setHistoryMaxSize(historyMaxSize);
- scheduler.start();
- }
-
- public static void destroy() {
- scheduler.stop();
- }
- static class MySchedularThread extends Thread{
- public void run(){
- JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
- try {
- long processInstanceId =1;
- processInstance = jbpmContext.loadProcessInstance(processInstanceId);
- Token token = processInstance.getRootToken();
- System.out.println(token.getNode());
-
- token.signal();
- System.out.println(token.getNode());
- jbpmContext.save(processInstance);
-
- }catch(Exception e){
- e.printStackTrace();
- }finally {
- jbpmContext.close();
- }
- }
- }
-
- public static void main(String[] args) {
- initSchedular ();
- MySchedularThread mst=new MySchedularThread();
- mst.start();
- }
- }
运行结果:
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节点的,见下篇吧。