论坛首页 Java企业应用论坛

jbpm的调度应用1

浏览 18155 次
该帖已经被评为良好帖
作者 正文
   发表时间:2006-12-07  

以前开始接触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的深圳发布会。

 

   发表时间:2006-12-08  
好像jdk的timer和opensynphony的quartz已经很方便
0 请登录后投票
   发表时间:2006-12-08  
presses 写道
好像jdk的timer和opensynphony的quartz已经很方便

汗......
jBPM为设计及开发工作流和业务流程管理系统提供了一个先进的平台。由API、特定领域的语言和图形建模工具组成的框架让开发人员和业务分析人员能够使用通用平台进行沟通及操作。
0 请登录后投票
   发表时间:2006-12-11  
作业调度框架quartz
0 请登录后投票
   发表时间:2006-12-12  
定时调度。
0 请登录后投票
   发表时间:2006-12-14  
我按照这个执行,可是还是没有调度,jbmp_timer表中为空的
0 请登录后投票
   发表时间:2006-12-14  
俺做的项目也要改为jBpm了
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics