🥗Asynchronous thread pool
# Default global thread pool
LiteFlow has a global thread pool by default, and parameters such as the size of the thread pool can be set by setting the following parameters:
liteflow.when-max-wait-seconds=15
liteflow.when-max-workers=16
liteflow.when-queue-limit=512
# Custom global thread pool
But if you have special requirements for thread pools, LiteFlow also supports custom thread pool settings.
It should be noted that the custom thread pool is only applicable to parallel components, this parameter has no effect on synchronous components. And once you set your custom thread pool, the above parameters will no longer be useful. The definition of all parameters of the thread pool is up to you.
First you can define a thread pool Builder like this:
public class CustomThreadBuilder implements ExecutorBuilder {
@Override
public ExecutorService buildExecutor() {
return Executors.newCachedThreadPool();
}
}
Then add your Builder to the LiteFlow configuration. For springboot, you can configure it like this:
liteflow.thread-executor-class=com.yomahub.liteflow.test.customThreadPool.CustomThreadBuilder
If it is in spring, you can configure it like this:
<bean id="liteflowConfig" class="com.yomahub.liteflow.property.LiteflowConfig">
<property name="ruleSource" value="customThreadPool/flow.el.xml"/>
<property name="threadExecutorClass" value="com.yomahub.liteflow.test.customThreadPool.CustomThreadExecutor"/>
</bean>
<bean id="flowExecutor" class="com.yomahub.liteflow.core.FlowExecutor">
<property name="liteflowConfig" ref="liteflowConfig"/>
</bean>
In this way, LiteFlow will automatically load the custom thread pool through the Builder you declare when it starts.
# Thread pool for WHEN
In some scenarios, your asynchronous nodes may not need a global thread pool, and you want to set a separate thread pool for each group of asynchronous nodes.
For example, I now want to set up different thread pools for the following two chains:
<chain name="chain1">
WHEN(a, b);
</chain>
<chain name="chain2">
WHEN(c, d);
</chain>
First implement 2 own thread pools:
public class CustomThreadExecutor1 implements ExecutorBuilder {
@Override
public ExecutorService buildExecutor() {
//Construct your custom thread pool
}
}
public class CustomThreadExecutor2 implements ExecutorBuilder {
@Override
public ExecutorService buildExecutor() {
//Construct your custom thread pool
}
}
Then you declare it as follows:
<chain name="chain1">
WHEN(a, b).threadPool("com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor1");
</chain>
<chain name="chain2">
WHEN(c, d).threadPool("com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor2");
</chain>
# Priority
If both the global and WHEN
are configured with a custom thread pool, the thread pool configured on WHEN
is used first.