jbpm实现第一个流程定义

第一步:定义流程定义文件:processdefinition.xml

<?xml version="1.0" encoding="UTF-8"?>
<process-definition
  xmlns="urn:jbpm.org:jpdl-3.1"  name="HelloWorld">
   <start-state name="start">
      <transition name="to_state1" to="state1"></transition>
   </start-state>
   <end-state name="end"></end-state>
   <state name="state1">
      <transition name="to_end" to="end"></transition>
   </state>
</process-definition>

 

第2步:发布流程

import java.util.List;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;

import junit.framework.TestCase;

public class HelloWorldBdTest extends TestCase {
   
    public static JbpmConfiguration jbpmConfiguration = null;
   
    public static JbpmContext jbpmContext = null ;
   
   
    static {
        //构造一个jbpmConfiguration 实例
        jbpmConfiguration = JbpmConfiguration.parseXmlString(
          "<jbpm-configuration>" +
          "  <jbpm-context>" +
          "    <service name='persistence' " +
          "             factory='org.jbpm.persistence.db.DbPersistenceServiceFactory' />" +
          "  </jbpm-context>" +   
          "  <string name='resource.hibernate.cfg.xml' " +
          "          value='hibernate.cfg.xml' />" +
          "  <string name='resource.business.calendar' " +
          "          value='org/jbpm/calendar/jbpm.business.calendar.properties' />" +
          "  <string name='resource.default.modules' " +
          "          value='org/jbpm/graph/def/jbpm.default.modules.properties' />" +
          "  <string name='resource.converter' " +
          "          value='org/jbpm/db/hibernate/jbpm.converter.properties' />" +
          "  <string name='resource.action.types' " +
          "          value='org/jbpm/graph/action/action.types.xml' />" +
          "  <string name='resource.node.types' " +
          "          value='org/jbpm/graph/node/node.types.xml' />" +
          "  <string name='resource.varmapping' " +
          "          value='org/jbpm/context/exe/jbpm.varmapping.xml' />" +
          "</jbpm-configuration>"
        );
      }
   
   
    public void testBD(){
        //创建数据库表结构
        dbCreate();
      //流程定义和部署
      deployProcessDefinition();       
        //流程启动
        processInstence();
       
        //流程执行继续
        processContinues();
    }
   
    //    数据库表创建
    public void dbCreate(){
        //创建jbpm的数据库表结构
        jbpmConfiguration.createSchema() ;
        System.out.println("创建jbpm数据库表结构成功!");

        //销毁所有创建的数据库表
        //jbpmConfiguration.dropSchema();   
       
    }
   
    //流程定义和部署
    public void deployProcessDefinition(){//       
        //xml文件部署
        ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("helloworld/processdefinition.xml");
       
        //创建jbpmContext操作对象。
        jbpmContext = jbpmConfiguration.createJbpmContext() ;
        try{
            //发布定义的流程
            jbpmContext.deployProcessDefinition(processDefinition);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //使用jbpmContext对象操作完成后必须进行关闭,在创建jbpmContext对象是时开始了事务控制,知道
            //jbpm关闭,才进行事务的提交。把所有的操作持久化到数据库中
            jbpmContext.close() ;
        }
    }
   
    //流程的启动和执行
    public void processInstence(){
        //取得jbpmContext操作对象.
        jbpmContext = jbpmConfiguration.createJbpmContext();
        try{
            //取得grapgSession 进行对数据库的操作
            GraphSession graphSession = jbpmContext.getGraphSession() ;
            //利用graphSession取得流程定义对象
            ProcessDefinition processDefinition =  graphSession.findLatestProcessDefinition("HelloWorld") ;
            //创建一个流程定义实例,启动一个流程
            ProcessInstance processInstance=processDefinition.createProcessInstance();           
            //取得流程定义的 根令牌,这里得到的是流程定义中的start-state节点
            Token token = processInstance.getRootToken() ;
            System.out.println("节点:"+token.getNode().getName());
           
            //通过signal使流程往下执行执行到了 name = ‘s’的节点
            token.signal() ;
            System.out.println("token.signal()------------------------ nodeName :"+token.getNode().getName());
           
            //保存当前流程实例
            jbpmContext.save(processInstance) ;
           
        }finally{
            jbpmContext.close() ;
        }
    }
   
    //流程的继续执行
    public void processContinues(){
        //创建jbpmContext 对象
        jbpmContext = jbpmConfiguration.createJbpmContext() ;
        try{
            //取得GraphSession
            GraphSession graphSession = jbpmContext.getGraphSession() ;
           
            //取得流程定义
            ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition("HelloWorld");
            //根据流程定义id 取得该流程所有的实例list
            List piList = graphSession.findProcessInstances(processDefinition.getId());
           
            //取得流程实例
            ProcessInstance processInstance = (ProcessInstance)piList.get(0);
           
            //流程实例发送信令 流程执行到下一步
            Token token = processInstance.getRootToken();
            System.out.println("node 执行到 :"+token.getNode().getName());
            processInstance.signal() ;
            System.out.println("node 下一节点:"+processInstance.getRootToken().getNode().getName());
            //保存流程实例
            jbpmContext.save(processInstance);
           
        }finally{
            jbpmContext.close() ;
           
        }
       
    }
   
}

 

你可能感兴趣的:(数据结构,Hibernate,xml,jbpm,JUnit)