What is a 'custom action'?Actions are SCXML elements that "do" something. Actions can be used where "executable content" is permissible, for example, within <onentry>, <onexit> and <transition> elements. The SCXML specification (currently a Working Draft) defines a set of "standard actions". These include <var>, <assign>, <log>, <send>, <cancel>, <if>, <elseif> and <else>. The specification also allows implementations to define "custom actions" in addition to the standard actions. What such actions "do" is upto the author of these actions, and these are therefore called "custom" since they are tied to a specific implementation of the SCXML specification. Custom actions with Commons SCXMLCommons SCXML makes authoring custom actions fairly straightforward. What can be done via a custom actionA custom action in the Commons SCXML implementation has access to:
Walkthrough - Adding a 'hello world' custom actionLets walk through the development of a simple, custom "hello world" action. IdeaWe need a <hello> action in our (fictitious) namespace "http://my.custom-actions.domain/CUSTOM". The action "tag" will have one attribute "name". The action simply logs a hello to the value of the name attribute when it executes. A simple example is here . Custom action implementationA custom action must extend the Commons SCXML Action abstract base class. Here is the Java source for our custom Hello action. The execute() method contains the logging statement. Using a custom SCXML readerWith the custom action(s) implemented, the document may be parsed using a SCXMLReader that is made aware of these actions through a custom Configuration like so: // (1) Create a list of custom actions, add as many as are needed List<CustomAction> customActions = new ArrayList<CustomAction>(); CustomAction ca = new CustomAction("http://my.custom-actions.domain/CUSTOM", "hello", Hello.class); customActions.add(ca); // (2) Read the SCXML document containing the custom action(s) SCXML scxml = null; try { scxml = SCXMLReader.read(url, new SCXMLReader.Configuration(null, null, customActions)); // Also see other methods in SCXMLReader API } catch (Exception e) { // bad document, take necessary action } This approach can only be used if the custom rule has no body content (child "tags") or if the custom action implements the ExternalContent interface, in which case, any body content gets read into a list of DOM nodes. . Read in the 'custom' SCXML documentFor documents with or without custom actions, several utility methods of the SCXMLReader can be used. More information is here. Launching the engineHaving obtained the SCXML object beyond step (2) above, proceed as usual, see the section on the Commons SCXML engine for details. |