简体   繁体   English

Camel-设计可扩展的动态数据流

[英]Camel - Designing scalable dynamic dataflow

Use case: I receive a message from a source endpoint. 用例:我收到来自源端点的消息。 Depending on the type of message, it gets handled by completely different business logic (eg http request, tcp message, db call). 根据消息的类型,它可以通过完全不同的业务逻辑(例如,http请求,tcp消息,数据库调用)来处理。 Afterwards, the outcome and passed downstream (assume uniform standard for output message) 之后,结果传递到下游(假设输出消息的标准统一)

The route would look something like this: 路线如下所示:

from(_source_)
...
// handle data (this is dynamic)
...
process(_logger_)
to(_receiver_)

A straight forward solution would be use choice(): 一个简单的解决方案是使用choice():

... // Upstream
.choice()
.when(someCondition).process(sendHTTP)
.when(anotherCondition).process(getToken).process(sendTCP)
.otherwise().process(sendToDB)
... // Downstream

But that's not very scalable. 但这不是很可扩展。

Another solution would be to just put everything in a single processor which in turn calls a client that has polymorphic behaviour: 另一个解决方案是将所有内容都放在单个处理器中,该处理器依次调用具有多态行为的客户端:

... // Upstream
.process(messageSwitch)
... // Downstream

public class MessageSwitch implements Processor {

    public void process(Exchange ex) {
         RequestClient client = this.resolveClient(ex);
         client.sendRequest(ex.getIn());
     }
}

However, this also makes us lose sight of the route. 但是,这也使我们看不到路线。 In that case, my question is whether or not it is common practice to execute a new route that takes place inside a processor. 在这种情况下,我的问题是执行处理器内部发生的新路由是否是常见的做法。 For example, I may want to execute a http call through the http4 library ( http://camel.apache.org/http4.html ). 例如,我可能想通过http4库( http://camel.apache.org/http4.html )执行http调用。

Or maybe I am approaching this problem incorrectly. 也许我是错误地解决了这个问题。

One way you could approach this is to have a processor set a header on the received message with a destination route for each type then use the camel recipient list to send the message to those destination routes. 您可以采用的一种方法是让处理器在接收到的消息上设置每种类型的目标路由的标头,然后使用骆驼收件人列表将消息发送到那些目标路由。

from(_source_)
.process(_processorcodebelow_)
.recipientList(simple("direct:${header.MessageRouterByType}"));

Processor would look something like: 处理器看起来像:

public void doProcees(Message message){
Message message = exchange.getIn();
... //logic to check type 
message.setHeader("MessageRouterByType", "messagetype1");
...//etc for different types

Then you can have different routes to perform your business logic for each of the different message types. 然后,您可以具有不同的路线来针对每种不同的消息类型执行业务逻辑。

from("direct:messagetype1").routeId("messagetype1")
.process(_messagetype1processor_)
.to(_receiver_)

You can use the route id or logging to keep track of which route the message took. 您可以使用路由ID或日志记录来跟踪消息采用的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM