LiteFlow LiteFlow
💒Home
📚Documents
💖Donation
🚀Plugin
  • About LiteFlow
  • Update Record
Community
Users
Gitee (opens new window)
Github (opens new window)
  • 简体中文 (opens new window)
  • English (opens new window)
💒Home
📚Documents
💖Donation
🚀Plugin
  • About LiteFlow
  • Update Record
Community
Users
Gitee (opens new window)
Github (opens new window)
  • 简体中文 (opens new window)
  • English (opens new window)
  • 🍤Introduction
  • 🍓Features
  • 🍟Quick Start(Hello world)

    • 🍄Notice
    • 🌿Springboot environment

      • 🧬Dependency
      • ⚙️Config
      • 🛫Execute
    • 🌱Spring environment

      • 🧬Dependency
      • ⚙️Config
      • 🛫Execute
    • 🌵Other environment

      • 🍄Notice
      • 🧬Dependency
      • ⚙️Config
      • 🛫Execute
  • 🍢Configuration item

    • 🍄Notice
    • 🌿Springboot environment
    • 🌱Spring environment
    • 🌵Other environment
  • 🗂Rules file

    • 📔Rules file format
    • 📕Local rule file
    • 📗Zookeeper source
    • 📘SQL database source
    • 📋Nacos source
    • 🗄Etcd source
    • 📙Custom source
  • 🔗General components

    • 📎Common component
    • ✂️Switch component
      • Select based on nodeId
      • select based on expression-id
      • select based on tag
    • 📌If component
    • 🧬For component
    • ⛓While component
    • 🧿Break component
  • 🧩How to write EL rules

    • 🍄Notice
    • 🌴Serial mode
    • 🎋Parallel mode
    • 🌾Switch mode
    • 🌵If mode
    • 🌳Loop mode
    • 🍁Use sub chain
    • 🍂Use sub variables
    • 💐Complex example
    • 🌻About semicolons
    • 🌰About comments
    • 🐚Component Name Package
  • 🌮Data context

    • 🍄Notice
    • 🌯Definition and use
    • 🪶Pass initialized context
  • 🛩Flow executor

    • 🍄Notice
    • 🎡Executor method
    • 🎢Chain input parameter
    • 🎈LiteflowResponse object
  • 🍋Script component

    • 🍫Choose a script language
    • 🍕Define script component
    • 🌯Define file script
    • 🍣Interact with java
    • 🍘Dynamic refresh script
  • 🍇Declarative component

    • 🥭What is a declarative component
    • 🧅Class level declaration
    • 🥥Method level declaration
  • 🎲Dynamic rules

    • 🍄Notice
    • 🎯How to build
  • 🎨Advanced features

    • 🍒Pre & Finally components
    • 🥠Substitute component
    • 🍉Component Parameters
    • 🍑Component alias
    • 🍍Component tag
    • 🥝Component event
    • 🥑Implicit chain
    • 🍕Private delivery
    • 🍣Component retry
    • 🍖Smooth hot refresh
    • 🍪Component aspect
    • 🍡Step information
    • 🧊Exceptions
    • 🧇Printing details
    • 🧁Custom request id
    • 🌭Multiple type rules
    • 🥗Asynchronous thread pool
    • 🍿Custom component executor
    • 🍥Simple monitor
    • 🧉DTD in Xml
  • ⛱Test cases and demo

    • 🪁Test cases
    • 🪀Demo
  • 🪂Performance
  • v2.9.X文档
  • 🔗General components
铂赛东
2022-06-29
Contents

✂️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.

Help us improve this document (opens new window)
last update: 2022/11/30, 22:42:09
📎Common component
📌If component

← 📎Common component 📌If component→

Theme by Vdoing | Copyright © 2020-2022 铂赛东 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式