When using Java activities in jbpm4, We need to input parameters from the outside. The example does not show how to implement them.
The following uses the built-in example of jbpm as an example:
First, modify the configuration file of the example:
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
<ProcessName= "Java"Xmlns= "Http://jbpm.org/4.4/jpdl">
Start G =" 20, 20, 48, 48 " >
transition to =" Greet " />
Start >
<JavaName= "Greet"
Class= "Org. jbpm. Examples. java. johndoe"
Method= "Hello"
VaR= "Answer"
G= "96,16, 83,52">
< Field Name = "State" > < String Value = "Fine" /> </ Field >
< ARG >
< Ref Object = "MSG" />
</ ARG >
<! --
<String value = "Hi, how are you? "/> </Arg>
-->
< Transition To = "Shake hand" />
</ Java >
JAVA name =" shake hand "
expr =" # {hand}"
method =" Shake "
var =" hand "
G =" 215,17, 99,52 " >
Arg > Object expr =" # {joesmoe. handshakes. force} " /> Arg >
Arg > Object expr =" # {joesmoe. handshakes. duration} " /> Arg >
<TransitionTo= "Wait" />
</Java>
<StateName= "Wait"G= "352,17, 88,52"/>
</Process>
We modified the XML file and put <string value = "Hi, how are you? "/> </Arg>
Change <RefObject= "MSG"/>
JavaCodeTo:
Code
Package Org. jbpm. Examples. Java;
ImportJava. util. hashmap;
ImportJava. util. Map;
ImportOrg. jbpm. API. processinstance;
ImportOrg. jbpm. Test. jbpmtestcase;
/**
*@ AuthorTom Baeyens
*/
Public ClassJavainstantiatetestExtendsJbpmtestcase {
String deploymentid;
Protected Void Setup () Throws Exception {
Super . Setup ();
Deploymentid = Repositoryservice. createdeployment ()
. Addresourcefromclasspath ( " Org/jbpm/examples/Java/process. jpdl. xml " )
. Deploy ();
}
Protected VoidTeardown ()ThrowsException {
Repositoryservice. deletedeploymentcascade (deploymentid );
Super. Teardown ();
}
Public Void Testjavainstantiate (){
Map < String, Object > Variables = New Hashmap < String, Object > ();
Variables. Put ( " Hand " , New Hand ());
Variables. Put ( " Joesmoe " , New Joesmoe ());
Variables. Put ( " MSG " , " Hi, how are you? " );
Processinstance = Executionservice. startprocessinstancebykey ( " Java " , Variables );
String PID = Processinstance. GETID ();
String answer = (String) executionservice. getvariable (PID, " Answer " );
Assertequals ( " I'm fine, thank you. " , Answer );
Hand hand=(Hand) executionservice. getvariable (PID,"Hand");
Asserttrue (hand. isshaken ());
}
}
we added variables to the Java code. put ( " MSG " , " hi, how are you? " ); MSG is passed in as a Java parameter.