jbpm的调度应用

十二月 6, 2008 by admin  
类别:JAVA开发

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

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

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

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

java 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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() {// 设置Schedular的属性
		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());
				// 一定要运行到有timer生成,触发
				token.signal();
				System.out.println(token.getNode());
				jbpmContext.save(processInstance);
				// 如果这里程序到这里退出的话可以看到jbpm_timer表里有一条数据
				Thread.sleep(30 * 1000);// 为模拟效果,此线程停止30秒
				// 节点跳过,timer结束,jbpm_timer表该数据清空
				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)
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的深圳发布会。

评论

2 评论 on "jbpm的调度应用"

  1. Shark Chaos on 星期一, 22nd 十二 2008 19:38 

    建议用一些显示代码的插件哈,这样效果会好很多
    谢谢你的建议,前两天正在弄代码插件,现在弄好了。

    回复

  2. 木瓜 on 星期二, 10th 二 2009 16:46 

    嗯,很有道理

    回复

告诉我们你想说点啥...
想要有个性的头像, 到gravatar这里申请!