✂️Switch component
In actual business, it is often necessary to determine which node should be executed next through dynamic business logic, which leads to the selection node, which can be used in the SWITCH
keyword.
For SWITCH expressions, you can refer to the chapter Switch mode.
Switch node need to inherit NodeSwitchComponent
.
The method processSwitch
needs to be implemented.
# Select based on nodeId
For example the following expression:
<chain name="chain1">
SWITCH(a).to(b, c);
</chain>
This method needs to return the String
type, which is the specific result. The following code example selects the c node.
@LiteflowComponent("a")
public class ACmp extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
System.out.println("Acomp executed!");
return "c";
}
}
# select based on expression-id
For example the following expression:
<chain name="chain1">
SWITCH(a).to(b, WHEN(c,d).id("w1"));
</chain>
If you want to select that expression, you must add id after that expression and assign a string. Then your switch component just returns w1 directly.
@LiteflowComponent("a")
public class ACmp extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
System.out.println("Acomp executed!");
return "w1";
}
}
# select based on tag
For the concept of tags, please refer to the chapter Component tag.
The following expression:
<chain name="chain1">
SWITCH(a).to(
b.tag("cat"),
c.tag("dog")
);
</chain>
LiteFlow supports tag selection since v2.9.0. If you want to select the c component, the a component can return c
or tag:dog
.
@LiteflowComponent("a")
public class ACmp extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
System.out.println("Acomp executed!");
return "tag:dog";
}
}
The above is the simplest usage, and there are the following usages:
<chain name="chain4">
THEN(
a,
SWITCH(g).to(b.tag("td"), d.tag("td"))
);
</chain>
Take advantage of simple representations to quickly select what you want:
public class GSwitchCmp extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
return ":td" // Enter the b node, meaning: select the first node whose tag is td
return "tag:td" // Enter the b node, meaning: select the first node whose tag is td
return "b"; // Enter the b node, meaning: select the node whose targetId is b
return "b:"; // Enter the b node, meaning: select the first node whose targetId is b
return "b:td"; // Enter the b node, meaning: select the node whose targetId is b and whose tag is td
return ":"; // Enter the b node, meaning: select the first node
return "d"; // Enter the d node, meaning: select the node whose targetId is d
return "d:"; // Enter the d node, meaning: select the first node whose targetId is d
return "d:td"; // Enter the d node, meaning: select the node whose targetId is d and the tag is td
return "b:x"; // Error, reason: there is no node whose targetId is b and tag is x
return "x"; // Error, reason: there is no node whose targetId is x
return "::"; // Error, reason: no node with tag ":" found
}
}
See the chapter Common Components for the methods that can be overridden and the methods that can be called by the this keyword.