package openwfe.org.embed;
import openwfe.org.engine.control.ControlSession;
import openwfe.org.engine.workitem.WorkItem;
import openwfe.org.engine.workitem.CancelItem;
import openwfe.org.engine.workitem.LaunchItem;
import openwfe.org.engine.workitem.InFlowWorkItem;
import openwfe.org.embed.engine.Engine;
//import openwfe.org.embed.impl.engine.InMemoryEngine;
import openwfe.org.embed.impl.engine.FsPersistedEngine;
import openwfe.org.embed.impl.engine.AbstractEmbeddedParticipant;
public class TestEngine
{
public static void main (String[] args)
throws Exception
{
//
// prepare engine
//Engine engine = new InMemoryEngine();
Engine engine = new FsPersistedEngine();
/*
* The inMemoryEngine only stores its rundata in RAM,
* the FsPersistedEngine stores its rundata in a temp folder.
*
* From one run to the other, the FsPersistedEngine will be able
* to retrieve and reuse its rundate (if it has been deleted).
*/
// register participants
engine.registerParticipant
(new AbstractEmbeddedParticipant ("a")
{
public void consume (WorkItem wi)
throws Exception
{
System.out.println("~~~ a");
wi.getAttributes().puts("value", "CHF 10.-");
replyToEngine((InFlowWorkItem)wi);
}
public void cancel (CancelItem ci)
{
System.out.println("~~~ a (got cancelled)");
}
});
/*
* This is an easy (and very swinggy) way of adding participants
* to an engine.
*
* Please note the presence at the end of 'consume' of the method
* 'replyToEngine()' which hands back the [modified] workitem
* to the engine.
*/
engine.registerParticipant
(new AbstractEmbeddedParticipant ("b")
{
public void consume (WorkItem wi)
throws Exception
{
System.out.println("~~~ b");
System.out.println(" value : "+wi.getAttributes().sget("value"));
replyToEngine((InFlowWorkItem)wi);
}
});
//
// launch flow
LaunchItem li = new LaunchItem();
li.setWorkflowDefinitionUrl("field:__definition__");
String flowDef =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<process-definition name=\"test\" revision=\"0.0\">\n"+
" <sequence>\n"+
" <participant ref=\"a\" />\n"+
" <participant ref=\"b\" />\n"+
" </sequence>\n"+
"</process-definition>";
li.getAttributes().puts("__definition__", flowDef);
/*
* This launch item workflowDefinitionUrl is pointing to
* 'field:__definition__' : the flow definition will not be
* fetch from a URL but directly from a launch item field,
* here '__definition__'.
*/
engine.launch(li, true);
//
// the second parameter is a boolean.
// its name is 'async'. When set to true, the method
// will immediately come back and the launch is performed
// asynchronously in the background (in another thread of
// execution).
//
// testing the control stuff
ControlSession cs = engine.getControlSession();
System.out.println("cs is "+cs);
java.util.Iterator it = cs.listExpressions().iterator();
while (it.hasNext())
System.out.println(it.next());
}
}
This piece of code is taken directly from the embed module and it shows how an InMemoryEngine or a FsPersistedEngine may be directly instantiated from Java.
engine.registerParticipant
(new AbstractEmbeddedParticipant ("a")
{
public void consume (WorkItem wi)
throws Exception
{
System.out.println("~~~ a");
wi.getAttributes().puts("value", "CHF 10.-");
replyToEngine((InFlowWorkItem)wi);
}
public void cancel (CancelItem ci)
{
System.out.println("~~~ a (got cancelled)");
}
});
This participant 'a' shows a new feature of OpenWFE 1.7.2 : embedded participants may override the cancel() method of AbstractEmbeddedParticipants to react differently upon receiving a CancelItem instance. There's no need to reply to the engine at the end of cancel(). Embedded participnants for flows that expect to have cancel cases should imperatively provide a new implementation for this method (the default one just pipes the CancelItem to consume()).