第一个jbpm的例子实现步骤:
第一步:在eclipse中新建一个java project,项目名称为:jbpmExample_1。
第二步:导入Junit和jbpm的jar.
第三步:将jbpm下的config中的jbpm.cfg.xml拷贝到src目录下。
第四步:新建一个Junit Test Case 类名为:TestHelloWorld,代码如下:
package com.baoz.je.test;
import junit.framework.TestCase;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
public class TestHelloWorld extends TestCase {
protected void setUp() throws Exception {
}
public void testHelloWorldProcess(){
ProcessDefinition processDefinition = new ProcessDefinition().parseXmlString("" +
"<process-definition>" +" <start-state>" +
" <transition to='s'/> " +" </start-state>" +
" <state name='s'>" +" <transition to='end'/>" +
" </state>" +" <end-state name='end'/> " +
"</process-definition>");
ProcessInstance processInstance = new ProcessInstance(processDefinition);
Token token = processInstance.getRootToken();
assertSame(processDefinition.getStartState(), token.getNode());
System.out.println("processDefinition.getStartState()="+processDefinition.getStartState().getName());
System.out.println("token.getNode()="+token.getNode().getName());
token.signal();
assertSame(processDefinition.getNode("s"), token.getNode());
System.out.println("processDefinition.getNode('s')="+processDefinition.getNode("s").getName());
System.out.println("token.getNode()="+token.getNode().getName());
token.signal();
assertSame(processDefinition.getNode("end"), token.getNode());
System.out.println("processDefinition.getNode('end')="+processDefinition.getNode("end").getName());
System.out.println("token.getNode()="+token.getNode().getName());
}
protected void tearDown() throws Exception {
super.tearDown();
}
}
第五步:运行如上的Test Case,如果没有报错,而且控制台打印出如下的内容,表示此示例运行成功:
processDefinition.getStartState()=null
token.getNode()=null
processDefinition.getNode('s')=s
token.getNode()=s
processDefinition.getNode('end')=end
token.getNode()=end
注意点:假如你的项目中报如下错误:
org.jbpm.JbpmException: couldn't parse jbpm configuration from resource 'jbpm.cfg.xml'
at org.jbpm.JbpmConfiguration.getInstance(JbpmConfiguration.java:292)
at org.jbpm.JbpmConfiguration.getInstance(JbpmConfiguration.java:257)
at org.jbpm.JbpmConfiguration$Configs.getObjectFactory(JbpmConfiguration.java:425)
at org.jbpm.JbpmConfiguration$Configs.getObject(JbpmConfiguration.java:437)
at org.jbpm.JbpmConfiguration$Configs.getString(JbpmConfiguration.java:441)
at org.jbpm.graph.def.ProcessDefinition.createNewProcessDefinition(ProcessDefinition.java:97)
at org.jbpm.jpdl.xml.JpdlXmlReader.readProcessDefinition(JpdlXmlReader.java:138)
at org.jbpm.graph.def.ProcessDefinition.parseXmlString(ProcessDefinition.java:150)
at com.baoz.je.test.TestHelloWorld.testHelloWorldProcess(TestHelloWorld.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.jbpm.JbpmException: jbpm configuration resource 'jbpm.cfg.xml' is not available
at org.jbpm.JbpmConfiguration.getInstance(JbpmConfiguration.java:286)
... 26 more
那么说明你没有将jbpm下的config中的jbpm.cfg.xml拷贝到src目录下,只有将jbpm下的config中的jbpm.cfg.xml拷贝到src目录下就能解决此问题,尽管jbpm.cfg.xml中没有内容,但是也必须拷贝进来。