174.6. 预定义的表达式

从 Camel 版本 2.13.0 开始,JGroups 组件附带名为 JGroupsExpressions 的预定义表达式工厂。

如果您要创建仅在 Camel 上下文尚未启动时影响路由的延迟程序,请使用 JGroupsExpressions.delayIfContextNotStarted (长延迟) 工厂方法。只有 Camel 上下文处于不同于 启动 的状态时,此工厂方法创建的表达式才会返回给定延迟值。如果您希望使用 JGroups 组件在集群中保持单例(master)路由,则此表达式特别有用。如果 Camel 上下文尚未启动,则 控制总线 启动命令不会初始化单例路由。因此,您需要延迟 master 路由的启动,以确保在 Camel 上下文启动后已初始化它。由于这种情况只在初始化集群期间发生,所以我们不希望延迟从节点的启动成为新主设备 - 为什么我们需要一个条件延迟表达式。

以下代码片段演示了如何将条件延迟与 JGroups 组件一起使用,以延迟集群中 master 节点的初始启动。

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.camel.component.jgroups.JGroupsExpressions.delayIfContextNotStarted;
import static org.apache.camel.component.jgroups.JGroupsFilters.dropNonCoordinatorViews;
...
from("jgroups:clusterName?enableViewMessages=true").
  filter(dropNonCoordinatorViews()).
  threads().delay(delayIfContextNotStarted(SECONDS.toMillis(5))). // run in separated and delayed thread. Delay only if the context hasn't been started already.
  to("controlbus:route?routeId=masterRoute&action=start&async=true");

from("timer://master?repeatCount=1").routeId("masterRoute").autoStartup(false).to(masterMockUri);