Integration of a subprocess definition in JBPM JPDL
I'm using the process definition language jPDL from the JBoss Workflow engine : jBPM.
I want to integrate a very simple process definition graph : Basically, I have a main process definition (simple/processdefinition.xml) including :
- a Start node,
- a Process State node,
- an End node.
Here is the definition of the graph :
<process-definition
xmlns="urn:jbpm.org:jpdl-3.2"
name="simple">
<start-state name="start">
<transition name="to_state" to="process-state">
<action name="action" class="com.sample.action.MessageActionHandler">
<message>
Going to the sub-state!
</message>
</action>
</transition>
<event type="node-leave">
<action class="com.sample.action.MessageActionHandler">
<message>
Start.
</message>
</action>
</event>
</start-state>
<process-state name="process-state">
<sub-process name="subsimple" binding="late"/>
<variable access="read,write" name="message" mapped-name="message"> </variable>
<event type="node-enter">
<action class="com.sample.action.MessageActionHandler"></action>
</event>
<transition to="end" name="to_end"></transition>
</process-state>
<end-state name="end">
<event type="node-enter">
<action class="com.sample.action.MessageActionHandler">
<message>
End.
</message>
</action>
</event>
</end-state>
</process-definition>
Within the Process State node I defined a Sub-Process graph (subsimple/processdefinition.xml) containing :
- a Start node,
- a State node,
- an End node.
Here is the definition of the graph :
<process-definition
xmlns="urn:jbpm.org:jpdl-3.2"
name="subsimple">
<start-state name="start">
<transition name="to_state" 开发者_运维问答to="first">
<action name="action" class="com.sample.action.MessageActionHandler">
<message>Going to the first state!</message>
</action>
</transition>
<event type="node-leave">
<action class="com.sample.action.MessageActionHandler">
<message>
Start.
</message>
</action>
</event>
</start-state>
<state name="first">
<event type="node-enter">
<action class="com.sample.action.MessageActionHandler">
<message>
In first state !!
</message>
</action>
</event>
<transition name="to_end" to="end">
<action name="action" class="com.sample.action.MessageActionHandler">
<message>
About to go out of sub-process !
</message>
</action>
</transition>
</state>
<end-state name="end">
<event type="node-enter">
<action class="com.sample.action.MessageActionHandler">
<message>
End.
</message>
</action>
</event>
</end-state>
</process-definition>
Here is the ActionHandler allocated to every events and transitions :
public class MessageActionHandler implements ActionHandler {
private static final long serialVersionUID = 1L;
String message;
public void execute(ExecutionContext context) throws Exception {
context.getContextInstance().setVariable("message", message);
System.out.println(context.getContextInstance().getVariable("message"));
}
}
Eventually, here is the Main class from which I start the whole process :
public class testJBPM {
public static void main(String[] args) {
ProcessDefinition processDefinition = ProcessDefinition
.parseXmlResource("simple/processdefinition.xml");
// Create an instance of the process definition.
ProcessInstance instance = new ProcessInstance(processDefinition);
// Move the process instance from its start state to the first state.
instance.signal();
// Move the process instance to the end state.
instance.signal();
}
}
As I read on JBoss forum, I correctly set binding=late
on sub-process binding in main process definition. Then I tried different ways to instanciate the process definition of the sub-process but I always get the following Exception when trying to enter the sub-state/sub-process node :
Exception in thread "main" org.jbpm.JbpmException: can't create a process instance when processDefinition is null
at org.jbpm.graph.exe.ProcessInstance.<init>(ProcessInstance.java:128)
at org.jbpm.graph.exe.ProcessInstance.<init>(ProcessInstance.java:92)
at org.jbpm.graph.exe.Token.createSubProcessInstance(Token.java:624)
at org.jbpm.graph.node.ProcessState.execute(ProcessState.java:164)
at org.jbpm.graph.def.Node.enter(Node.java:319)
at org.jbpm.graph.def.Transition.take(Transition.java:151)
at org.jbpm.graph.def.Node.leave(Node.java:394)
at org.jbpm.graph.node.StartState.leave(StartState.java:70)
at org.jbpm.graph.exe.Token.signal(Token.java:195)
at org.jbpm.graph.exe.Token.signal(Token.java:140)
at org.jbpm.graph.exe.ProcessInstance.signal(ProcessInstance.java:271)
at com.sample.main.testJBPM.main(testJBPM.java:21)
Can anybody give me advices on how to manage sub-process definition and use ?
I eventually found a way-out by myself :
First, only one
instance.signal();
is necessary to impulse the whole processes trail inmain()
.Then, an interface has to be implemented to allow jBPM to search and instanciate the required sub-processes :
public class DbSubProcessResolver implements SubProcessResolver { private static final long serialVersionUID = 1L; public ProcessDefinition findSubProcess(Element subProcessElement) { String name = subProcessElement.attributeValue("name").replaceAll( "\\.", ""); ProcessDefinition subProcessDefinition = ProcessDefinition .parseXmlResource(name + "/processdefinition.xml"); return subProcessDefinition; } }
Eventually, a jBPM configuration file has to be specified to map the jBPM framework resolver to the implemented one :
<jbpm-configuration> <bean name="jbpm.sub.process.resolver" class="com.sample.resolver.DbSubProcessResolver" /> </jbpm-configuration>
... and it works fine !
精彩评论