以下是一个发布,保存流转状态,继续执行的流程例子。 package com.amway.test; 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 HelloWorldDBTest extends TestCase { static JbpmConfiguration jbpmConfiguration = null; private JbpmContext jbmContextTemp; static { StringBuffer sb = new StringBuffer(); sb.append("<jbpm-configuration>"); sb.append("<jbpm-context>"); sb.append("<service name='persistence' factory='org.jbpm.persistence.db.DbPersistenceServiceFactory'/>"); // 文档没有如下这句,加上是为了避免 // problem closing service 'persistence' // org.jbpm.JbpmException: no jbpm tx service configured sb.append("<service name='tx' factory='org.jbpm.tx.TxServiceFactory'/>"); sb.append("</jbpm-context>"); sb.append("<string name='resource.hibernate.cfg.xml' value='hibernate.cfg.xml'/>"); sb.append("<string name='resource.business.calendar' value='org/jbpm/calendar/jbpm.business.calendar.properties'/>"); sb.append("<string name='resource.default.modules' value='org/jbpm/graph/def/jbpm.default.modules.properties'/>"); sb.append("<string name='resource.converter' value='org/jbpm/db/hibernate/jbpm.converter.properties'/>"); sb.append("<string name='resource.action.types' value='org/jbpm/graph/action/action.types.xml'/>"); sb.append("<string name='resource.node.types' value='org/jbpm/graph/node/node.types.xml'/>"); sb.append("<string name='resource.varmapping' value='org/jbpm/context/exe/jbpm.varmapping.xml'/>"); sb.append("</jbpm-configuration>"); jbpmConfiguration = JbpmConfiguration.parseXmlString(sb.toString()); } @Override public void setUp() { // 创建对应的数据库表 jbpmConfiguration.createSchema(); } @Override public void tearDown() { // 删除创建的数据库表 jbpmConfiguration.dropSchema(); } public void testSimplePersistence() { //发布流程 deployProcessDefinition(); //保存流程流转的状态 processInstanceIsCreatedWhenUserSubmitsWebappForm(); //回复流程继续流转到结束 theProcessInstanceContinuesWhenAnAsyncMessageIsReceived(); } private void theProcessInstanceContinuesWhenAnAsyncMessageIsReceived() { JbpmContext jbmContext = jbpmConfiguration.createJbpmContext(); GraphSession graphSession = jbmContext.getGraphSession();// 类似于jdbc 的 // connection // 获取最近的流程定义 ProcessDefinition processDefinition = graphSession .findLatestProcessDefinition("hello world"); // 根据流程的 id获取所有流程实例 List instances = graphSession.findProcessInstances(processDefinition.getId()); //获取单个流程 ProcessInstance instance = (ProcessInstance)instances.get(0); // 获取流转路径 Token token = instance.getRootToken(); try { this.assertEquals("s", token.getNode().getName()); token.signal(); this.assertTrue(instance.hasEnded()); } catch (Exception e) { e.printStackTrace(); } finally { jbmContext.close(); } } private void processInstanceIsCreatedWhenUserSubmitsWebappForm() { // 获取当前容器的上下文对象 JbpmContext jbmContext = jbpmConfiguration.createJbpmContext(); try { GraphSession graphSession = jbmContext.getGraphSession();// 类似于jdbc // 的 // connection // 获取最近的流程定义 ProcessDefinition processDefinition = graphSession .findLatestProcessDefinition("hello world"); this.assertNotNull(processDefinition); // 获取流程实例 ProcessInstance instance = new ProcessInstance(processDefinition); this.assertNotNull(instance); // 获取流转路径 Token token = instance.getRootToken(); this.assertEquals("start", token.getNode().getName()); token.signal(); this.assertEquals("s", token.getNode().getName()); // save 流程 流转的状态 jbmContext.save(instance); } catch (Exception e) { e.printStackTrace(); } finally { jbmContext.close(); } } /** * 发布流程 */ private void deployProcessDefinition() { StringBuffer sb = new StringBuffer(); sb.append("<process-definition name='hello world'>"); sb.append("<start-state name='start'>"); sb.append("<transition to='s'/>"); sb.append("</start-state>"); sb.append("<state name='s'>"); sb.append("<transition to='end'/>"); sb.append("</state>"); sb.append("<end-state name='end'/>"); sb.append("</process-definition>"); ProcessDefinition processDefinition = ProcessDefinition .parseXmlString(sb.toString()); // JbpmConfiguration jbpmConfiguration = // JbpmConfiguration.getInstance(); JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); try { jbpmContext.deployProcessDefinition(processDefinition); } finally { jbpmContext.close(); } } }