下面是一个赛跑的小流程。这里我已经把流程持久化到数据库中。
为什么第一种方法不行呢?
代码如下:
类都实现ActionHandler接口:
方法都类似RunAction类。如下:
RunAction: public void execute(ExecutionContext executionContext) throws Exception {System.out.println("开始了...");}
MatchAction:{System.out.println("比赛中...");
PauseAction:System.out.println("受伤了...");
EndAction:System.out.println("比赛结束!");
Test1类:
ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("E:\\workspace\\articleaudit\\WebRoot\\WEB-INF\\test1\\processdefinition.xml");
ProcessInstance ProcessInstance = new ProcessInstance(processDefinition);
Token token = ProcessInstance.getRootToken();
System.out.println(token.getNode().getName());
token.signal("tr1");
System.out.println(token.getNode().getName());
token.signal();
System.out.println(token.getNode().getName());
程序运行到第1句就异常了,下面是所有异常信息:
16:32:21,093 INFO JbpmConfiguration:277 - using jbpm configuration resource 'jbpm.cfg.xml'
16:32:21,453 ERROR JpdlXmlReader:158 - couldn't parse process definition
org.dom4j.DocumentException: null Nested exception: null
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.jbpm.jpdl.xml.JpdlParser.parse(JpdlParser.java:57)
at org.jbpm.jpdl.xml.JpdlXmlReader.readProcessDefinition(JpdlXmlReader.java:138)
at org.jbpm.graph.def.ProcessDefinition.parseXmlInputStream(ProcessDefinition.java:172)
at org.jbpm.graph.def.ProcessDefinition.parseXmlResource(ProcessDefinition.java:153)
at test1.struts.Test1.main(Test1.java:11)
Nested exception:
java.net.MalformedURLException
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.jbpm.jpdl.xml.JpdlParser.parse(JpdlParser.java:57)
at org.jbpm.jpdl.xml.JpdlXmlReader.readProcessDefinition(JpdlXmlReader.java:138)
at org.jbpm.graph.def.ProcessDefinition.parseXmlInputStream(ProcessDefinition.java:172)
at org.jbpm.graph.def.ProcessDefinition.parseXmlResource(ProcessDefinition.java:153)
at test1.struts.Test1.main(Test1.java:11)
Exception in thread "main" org.jbpm.jpdl.JpdlException: [[ERROR] couldn't parse process definition]
at org.jbpm.jpdl.xml.JpdlXmlReader.readProcessDefinition(JpdlXmlReader.java:163)
at org.jbpm.graph.def.ProcessDefinition.parseXmlInputStream(ProcessDefinition.java:172)
at org.jbpm.graph.def.ProcessDefinition.parseXmlResource(ProcessDefinition.java:153)
at test1.struts.Test1.main(Test1.java:11)
****************************************************************
但当我把第一句改为直接写的方式:
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString("" +
"<process-definition xmlns='urn:jbpm.org:jpdl-3.1' name='test1'>" +
"<start-state name=\"开始\">" +
"<event type=\"node-leave\">" +
"<action name=\"action1\"></action></event>" +
"<transition name=\"tr1\" to=\"赛跑中\"></transition>" +
"<transition name=\"tr2\" to=\"受伤中\"></transition>" +
"</start-state>" +
"<state name=\"赛跑中\">" +
"<event type=\"node-enter\">" +
"<action name=\"赛跑\" class=\"test1.struts.MatchAction\"></action>" +
"</event>" +
"<transition name=\"结束\" to=\"比赛结束\"></transition></state>" +
"<state name=\"受伤中\">" +
"<event type=\"node-enter\">" +
"<action name=\"受伤\" class=\"test1.struts.PauseAction\"></action>" +
"</event>" +
"<transition name=\"结束\" to=\"比赛结束\"></transition></state>" +
"<end-state name=\"比赛结束\">" +
"<event type=\"node-enter\">" +
" <action name=\"结束\" class=\"test1.struts.EndAction\"></action></event>" +
"</end-state>" +
"</process-definition>");
正常输出:
开始
比赛中...
赛跑中
比赛结束!
比赛结束
为什么前一方式不行呢?
帮助文档上就这么一句:
引用
15.2.2. 解析xml文件
其他情况, 你可能想手写 processdefinition.xml 文件然后打包进zip文件通过ant script.这钟情况下你可以选择 JpdlXmlReader
...
static ProcessDefinition auctionProcess =
ProcessDefinition.parseXmlResource("org/jbpm/tdd/auction.xml");
...