今天索性想捡起很久没有碰过的工作流引擎jBPM,试验了里面的HelloWorld小例子。
代码很简单,就一个类,所以把代码贴出来,很简单,并不复杂,基本简单概括了jBPM运行的几个细节。
这里看得出,我们需要几个类包:jpdl、junit或者还需要dom4j。
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import junit.framework.TestCase;
public class HelloWorld extends TestCase {
public void testHelloWorldProcess(){
//第一个例子,jbpm自带的是直接把xml定义内容复制到这里。
//用的是parseXmlString()方法来解析的。我这里替换成了如下定义文件,注意,解析xml的方法并不一样
ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("processDefinition.xml");
ProcessInstance processInstance = new ProcessInstance(processDefinition);
Token token = processInstance.getRootToken();
assertSame(processDefinition.getStartState(),token.getNode());
token.signal();
assertSame(processDefinition.getNode("s"),token.getNode());
token.signal();
assertSame(processDefinition.getNode("end"),token.getNode());
}
}
过程中,出了问题。
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.parseXmlInputStream(ProcessDefinition.java:179)
at org.jbpm.graph.def.ProcessDefinition.parseXmlResource(ProcessDefinition.java:160)
at HelloWorld.testHelloWorldProcess(HelloWorld.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
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)
... 27 more
Caused by:这句写的很清楚,jbpm配置源jbpm.cfg.xml不可用。也就是我们并没有在src目录下建立这个文件。
解决方法:在jpdl根目录下的config文件夹内直接复制这个文件到src目录下(我用eclipse)。绿条出现。