回归基础:Apache Camel 路由和直接组件 [英] Back to Basics : Apache Camel Routes and Direct Component

查看:32
本文介绍了回归基础:Apache Camel 路由和直接组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Camel 路线及其两个端点感到有些困惑:Direct 和 Seda.好吧,假设我有这样的路线:

I am bit confused about Camel routes and its two endpoints : Direct and Seda. Well let's say i have a route like this :

public void configure()
{
 from("direct:services")
  .process(//Some processing here)
  .to("http://ThirdPartyServers")
}

最重要的是,我有一个 rest web 服务,它接收多个请求,进行一些处理,然后将消息移交给此路由以从某些第三方服务器获得响应.我已经通过 Spring 框架实例化了 Camel Context,如下所示:

On top of this I have a rest web service which receives several requests, does some processing and then hands over the message to this route to get response from some third party servers. I have instantiated Camel Context through Spring framework like this :

<camelContext id="appCamelContext" xmlns="http://camel.apache.org/schema/spring"
        trace="true" streamCache="true">
        <propertyPlaceholder id="properties"
            location="classpath:camel.properties" />
        <camel:routeBuilder ref="oneRouteBuilder" />
        <camel:routeBuilder ref="photosRouteBuilder" />
</camelContext>

现在的问题是,我立即向这条路由发送了多个不同的消息.现在 Camel 文档说直接组件在单线程中调用并且是同步的.那么所有的消息是并发处理还是一一处理呢?

Now the question is that in a instant I send multiple different messages to this route. Now Camel documentation says that direct component is invoked in single thread and is synchronous. So will all the messages be processed concurrently or one by one processing will happen ?

此外,如果我将直接组件更改为 seda,它会有所不同吗?

Also if i change the direct component to seda, will it make any difference ?

TIA

更新 [在 Petter 的回答之后]:虽然 Petter 的回答已经澄清,但我对相同的 Direct 和 Seda 组件有新的疑问.假设我的路线现在是这样的:

Update [after Petter's answer]: Although Petter's answer has clarified but i have new doubt regarding the same Direct and Seda components. Lets say my route is now like this :

public void configure(){
from("direct:services")
 .choice()
 .when("some predicate here-Predicate1")
 .to("seda:predicate1")
 .otherwise()
 .to("seda:fallback")
 .end();

 from("seda:predicate1")
 .process("some processing")
 .to("http://ThirdPartyServers");

 from("seda:fallback")
 .process("some processing")
 .to("jms:fallbackqueue");
}

现在,如果我从不同的线程向直接组件发送 5 条消息,那么这些消息将被并发处理.正如你在上面的路由中看到的,直接组件将消息发送到 seda 组件.那么现在是否只有一个 seda 组件线程来处理所有不同的 5 条消息?意思到底是不是所有的消息都会一一处理?

Now if i send 5 messages to direct component from different threads, so these messages would be processed concurrently. As you can see in the above route, direct component sends the message to seda component. So now will there be only one thread of seda component which will process all the different 5 messages? Meaning in the end all the messages will be processed one by one ?

推荐答案

直接组件在调用者的线程中运行.简而言之,它就像一个常规的 java 方法调用.只要有多个线程调用直接端点,就可以在路由中运行多个消息.

The direct component runs in the thread of the caller. Simplified, it's as a regular java method invocation. Multiple messages can run through the route as long as multiple threads are calling the direct endpoint.

当调用 SEDA(或 VM)端点时,您的消息被放入一个队列(在内存中).另一个线程(在路由中)从队列中一个一个地挑选消息并处理它们.您可以通过设置 concurrentConsumers 选项来配置 seda 使用者应该拥有的线程数.默认情况下,无论有多少线程正在为此路由生成消息,使用消息的单线程确保一次只处理一条消息.

When invoking the SEDA (or VM) endpoint, you message is put on a queue (in memory). Another thread (in the route) picks messages from the queue one by one and processes them. You can configure how many threads the seda consumer should have by setting the concurrentConsumers option. By default, the one thread consuming messages makes sure only one message at a time is processed no matter how many threads are producing to this route.

归结为你的陈述

我立刻向这条路线发送了多条不同的消息

in a instant I send multiple different messages to this route

如果这意味着不同的线程,或者只是一个序列中的一个线程,

If this means different threads, or just from one single thread in a sequence,

这篇关于回归基础:Apache Camel 路由和直接组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆