1. Overview
1.概述
Apache Camel is a Java framework that makes it easy to implement various Enterprise Integration Patterns (EIPs) for providing solutions to enterprise integration.
Apache Camel 是一个 Java 框架,可轻松实现各种企业集成模式 (EIP),为企业集成提供解决方案。
One of the common tasks in an integration pattern is to determine the message route at a runtime based on specific rules and conditions. Apache Camel simplifies this process by providing a method to implement Dynamic Router EIP.
集成模式的常见任务之一是在运行时根据特定规则和条件确定消息路由。Apache Camel 提供了一种实现动态路由器 EIP 的方法,从而简化了这一过程。
In this tutorial, we’ll delve into details of how to implement dynamic routing in Apache Camel and walk through an example.
在本教程中,我们将详细介绍如何在 Apache Camel 中实现动态路由,并通过一个示例进行说明。
2. Understanding the Dynamic Router
2.了解动态路由器
Occasionally, there are cases where we want to send messages to different routes based on specific rules and conditions at runtime. Solutions like the Routing Slip EIP can help solve the problem, but it is less efficient because it use trial-and-error.
有时,我们希望在运行时根据特定的规则和条件将信息发送到不同的路由。路由选择片段 EIP 等解决方案可以帮助解决这个问题,但效率较低,因为它需要反复试验。
In Routing Slip EIP, a message contains the list of endpoints to route to in a defined order. This requires pre-configuring the endpoint list and uses a trial-and-error approach to send messages through each endpoint.
在 Routing Slip EIP 中,报文包含要按规定顺序路由到的端点列表。这需要预先配置端点列表,并使用试错法通过每个端点发送报文。
The Dynamic Router EIP provides better implementation to add a route at runtime, especially in a case where we have multiple recipients or none at all. It provides flexibility to route messages without pre-configured strict endpoints.
动态路由器 EIP 为在运行时添加路由提供了更好的实现方式,尤其是在有多个收件人或根本没有收件人的情况下。它提供了在没有预先配置严格端点的情况下路由信息的灵活性。
Additionally, it knows every destination and rules for routing a message to a specific destination. Also, it has a control channel that potential destinations communicate to by announcing their presence and rule of engagement on startup.
此外,它还知道每个目的地以及将信息路由到特定目的地的规则。此外,它还拥有一个控制通道,潜在目的地可以通过在启动时宣布自己的存在和参与规则来与该通道进行通信。
Furthermore, it stores the rules for all possible destinations in a rule base. Once a message arrives, it checks the rule base and fulfills the request of a recipient.
此外,它还将所有可能目的地的规则存储在规则库中。信息到达后,它会检查规则库,并满足收件人的请求。
Here’s an image showing the internals of Dynamic Router EIP:
下面是显示动态路由器 EIP 内部结构的 图像:
Moreover, a common use case is dynamic service discovery, where a client application can access a service by sending a message containing the name of the service.
此外,一个常见的使用案例是动态服务发现,客户应用程序可以通过发送包含服务名称的消息来访问服务。
The core goal of Dynamic Router EIP is to defer the routing decision to runtime rather than building static rules upfront.
动态路由器 EIP 的核心目标是将路由决策推迟到运行时,而不是预先建立静态规则。
3. Maven Dependencies
3.Maven 依赖项
Let’s bootstrap a simple Apache Camel project by adding the camel-core and camel-test-junit5 to the pom.xml:
让我们通过在 pom.xml 中添加 camel-core 和 camel-test-junit5 来引导一个简单的 Apache Camel 项目:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
<version>4.3.0</version>
</dependency>
The Camel Core provides the Dynamic Router EIP along with other routing capabilities, and Camel Test JUnit5 helps make testing message routes easier with the CamelSupport interface.
Camel Core 提供了动态路由器 EIP 和其他路由功能,而 Camel Test JUnit5 则通过 CamelSupport 接口帮助用户更轻松地测试消息路由。
Notably, we can also bootstrap the Camel project as a Spring Boot project.
值得注意的是,我们还可以将 Camel 项目引导为 Spring Boot 项目。
4. Add Route at Runtime Using the Dynamic Router
4.使用动态路由器在运行时添加路由
The Dynamic Router EIP ensures that we specify rules for an integration system to aid right matching to a specific route at runtime. It checks for the incoming message’s body and matches it to a route.
动态路由器 EIP 确保我们为集成系统指定规则,以帮助在运行时正确匹配特定路由。它检查传入信息的正文,并将其与路由匹配。
4.1. Configuration
4.1.配置
First, let’s create a class named DynamicRouteBean and add a method to define rules and conditions:
首先,让我们创建一个名为 DynamicRouteBean 的类,并添加一个方法来定义规则和条件:
class DynamicRouterBean {
public String route(String body, @ExchangeProperties Map<String, Object> properties) {
int invoked = (int) properties.getOrDefault("invoked", 0) + 1;
properties.put("invoked", invoked);
if (invoked == 1) {
switch (body.toLowerCase()) {
case "mock":
return "mock:dynamicRouter";
case "direct":
return "mock:directDynamicRouter";
case "seda":
return "mock:sedaDynamicRouter";
case "file":
return "mock:fileDynamicRouter";
default:
break;
}
}
return null;
}
}
In the code above, we dynamically determine the appropriate route based on the incoming message body and current invocation count. The route() method checks if the message body matches any of the predefined keyword rules and returns the corresponding route.
在上面的代码中,我们会根据传入的消息体和当前调用次数动态确定合适的路由。route() 方法会检查消息体是否与任何预定义的关键字规则相匹配,并返回相应的路由。
Furthermore, we use the @ExchangeProperties annotation on a Map object. This map serves as a container to store and retrieve the current state of the exchange and update the invocation count.
此外,我们还在 Map 对象上使用了 @ExchangeProperties 注解。该映射可用作容器,用于存储和检索 exchange 的当前状态,并更新调用次数。
Also, the invoked variable represents the number of times the route() method has been invoked. If the message matches a predefined condition and it’s the first invocation, the corresponding route is returned. The invoked == 1 check helps to execute the dynamic routing logic on the first invocation. This simplifies the code in this specific case and prevents unnecessary re-execution.
此外,invoked变量表示调用 route() 方法的次数。如果消息符合预定义条件,并且是第一次调用,则会返回相应的路由。invoked == 1 检查有助于在第一次调用时执行动态路由逻辑。这简化了这种特定情况下的代码,并避免了不必要的重复执行。
Also, to prevent endless execution of the Dynamic Router, the route() method returns null after routing to the appropriate endpoint. This ensures dynamic routing concludes after a route is identified based on the message.
此外,为防止无休止地执行动态路由器,route()方法在路由到适当的端点后会返回null。这将确保动态路由在根据消息确定路由后结束。
Simply put, the route() method is called for every exchange until a null is returned.
简单地说,每次交换都会调用 route() 方法,直到返回 null 为止。
Finally, let’s configure the Dynamic Router EIP in our Camel route builder:
最后,让我们在 Camel 路由生成器中配置动态路由器 EIP:
class DynamicRouterRoute extends RouteBuilder {
@Override
void configure() {
from("direct:dynamicRouter")
.dynamicRouter(method(DynamicRouterBean.class, "route"));
}
}
In the code above, we create the DynamicRouterRoute class that extends RouteBuilder. Next, we override the configure method and add the dynamic route bean by invoking the dyamicRouter() method to wire the Dynamic Router call to our custom route().
在上面的代码中,我们创建了扩展 RouteBuilder 的 DynamicRouterRoute 类。接下来,我们覆盖 configure 方法,并通过调用 dyamicRouter() 方法将动态路由器调用连接到自定义 route() 来添加动态路由 bean。
Notably, we can use @DynamicRouter annotation on the method that defines our rules:
值得注意的是,我们可以在定义规则的方法上使用 @DynamicRouter 注解:
class DynamicRouterBean {
@DynamicRouter
String route(String body, @ExchangeProperties Map<String, Object> properties) {
// ...
}
}
The annotation eliminates the need to explicitly configure dynamicRouter() method in the Camel route:
该注解消除了在 Camel 路由中显式配置 dynamicRouter() 方法的需要:
// ...
@Override
void configure() {
from("direct:dynamicRouter").bean(DynamicRouterBean.class, "route");
}
// ...
In the code above, we specify the class that contains the routing logic using the bean() method. The dynamicRouter() method is no longer needed because the route() method is annotated with @DynamicRouter.
在上面的代码中,我们使用 bean() 方法指定了包含路由逻辑的类。不再需要 dynamicRouter() 方法,因为 route() 方法已注释为 @DynamicRouter 。
4.2. Unit Test
4.2.单元测试
Let’s write a unit test to assert if some of the conditions are true. First, let’s ensure our test class extends CamelTestSupport:
让我们编写一个单元测试,断言某些条件是否为真。首先,确保我们的测试类扩展了 CamelTestSupport:
class DynamicRouterRouteUnitTest extends CamelTestSupport {
@Override
protected RoutesBuilder createRouteBuilder() {
return new DynamicRouterRoute();
}
}
Here, provide an instance of DynamicRouterRoute to be used as a route builder for testing.
在此,提供一个 DynamicRouterRoute 的实例,作为路由生成器用于测试。
Next, let’s see an incoming message body named mock:
接下来,让我们看看名为 mock 的传入消息正文:
@Test
void whenMockEndpointExpectedMessageCountOneAndMockAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
MockEndpoint mockDynamicEndpoint = getMockEndpoint("mock:dynamicRouter");
mockDynamicEndpoint.expectedMessageCount(1);
template.send("direct:dynamicRouter", exchange -> exchange.getIn().setBody("mock"));
MockEndpoint.assertIsSatisfied(context);
}
Here, we mock the dynamic endpoint and set the expected message route as its value. Next, we set the expected message count to one. Finally, we set an incoming message route with an expected body message and assert the MockEndpoint route is satisfied.
在这里,我们模拟动态端点,并将预期消息路由设为其值。接着,我们将预期消息计数设为 1。最后,我们用预期的正文消息设置一个传入消息路由,并断言MockEndpoint路由已满足.。
Also, let’s mock the “mock:directDynamicRouter” message route:
另外,让我们模拟”mock:directDynamicRouter“消息路由:
@Test
void whenMockEndpointExpectedMessageCountOneAndDirectAsMessageBody_thenMessageSentToDynamicRouter() throws InterruptedException {
MockEndpoint mockDynamicEndpoint = context.getEndpoint("mock:directDynamicRouter", MockEndpoint.class);
mockDynamicEndpoint.expectedMessageCount(1);
template.send("direct:dynamicRouter", exchange -> exchange.getIn().setBody("direct"));
MockEndpoint.assertIsSatisfied(context);
}
This test validates that when “direct” is sent as the message body, it gets routed dynamically to the “mock:directDynamicRouter” endpoint. Also, we set the expected message count to one, indicating the number of message exchanges that the endpoint should receive.
该测试将验证当”direct“作为消息体发送时,它是否会被动态路由到”mock:directDynamicRouter“端点。此外,我们将预期消息计数设为 1,表示端点应接收的消息交换次数。
5. Conclusion
5.结论
In this article, we learned how to add routes at runtime in Apache Camel using the Dynamic Router EIP. Unlike Routing Slip which uses a try-and-error approach to send a message to a route, Dynamic Router EIP provides a solid implementation to route to an endpoint based on specific rules and conditions.
在本文中,我们学习了如何在运行时使用动态路由器 EIP 在 Apache Camel 中添加路由。Routing Slip 使用尝试和错误的方法将消息发送到路由,而动态路由器 EIP 则不同,它提供了一种基于特定规则和条件将路由发送到端点的可靠实现。
As always, the complete source code for the examples is available over on GitHub.
与往常一样,这些示例的完整源代码可在 GitHub 上获取。