gpt4 book ai didi

java - Camel 生产者消费者困惑

转载 作者:搜寻专家 更新时间:2023-10-30 21:25:36 25 4
gpt4 key购买 nike

Camel in Action 一书中生产者和消费者的定义让我有点困惑。我已经阅读了类似问题的其他两个答案,但我仍然觉得不是那样。

A producer is the Camel abstraction that refers to an entity capable of creating and sending a message to an endpoint. Figure 1.10 illustrates where the producer fits in with other Camel concepts. When a message needs to be sent to an endpoint, the producer will create an exchange and populate it with data compatible with that particular endpoint. For example, a FileProducer will write the message body to a file. A JmsProducer, on the other hand, will map the Camel message to a javax.jms.Message before sending it to a JMS destination. This is an important feature in Camel, because it hides the complexity of interacting with particular transports.

A consumer is the service that receives messages produced by a producer, wraps them in an exchange, and sends them to be processed. Consumers are the source of the exchanges being routed in Camel. Looking back at figure 1.10, we can see where the consumer fits in with other Camel concepts. To create a new exchange, a consumer will use the endpoint that wraps the payload being consumed. A processor is then used to initiate the routing of the exchange in Camel using the routing engine.

实际上是谁在创建交易所?生产者和消费者在典型的沟通 channel 的哪一边?从上面的文字我真的不能说谁对此负责。如果有人可以提供图片(我不清楚书中的图片),生产者和消费者的确切位置并以简单的方式解释它们的工作方式,那就太好了。也许一些例子也会有用。

好的,也许最好举个例子,有人可以告诉我它是如何工作的。想象一下,我们想要从文件夹中获取文件并将它们放入 JMS 队列中,然后从发送它们进行进一步处理,最终保存在磁盘上。

根据我的图片,生产者、消费者到底在哪里?我意识到什么是组件和端点。

最佳答案

您的怀疑或多或少是正确的。举个简单的例子:

CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("file:data/inbox?noop=true") // consumer
.to("file:data/outbox"); // producer
}
});
camelContext.start();
Thread.sleep(2000);
camelContext.stop();

在这个例子中,我们使用了一个RouteBuilder来创建一个Route,一旦CamelContext启动,它会执行如下:

  1. 创建两个 FileComponent 来表示这两个位置。
  2. 通过查询前面的组件创建相应的FileEndpoint
  3. 创建一个 FileConsumer 用于从 data/inbox 中读取数据。
  4. 创建一个 GenericFileProducer 以写入 data/outbox
  5. 将控制权交给 FileConsumer 以开始从其目录中轮询文件,该目录指示其 Endpoint 创建一个 Exchange(如正确所示图片)。 GenericFileMessage 绑定(bind)到此 Exchange
  6. 这个Exchange被交给了FileProducer

从这个角度来看,Consumer 没有创建交换。我想,在本书的这个阶段,它还没有意义。这反射(reflect)在文本中。但是,查看实现,当您查看代码时两者是等价的:

Consumer 在发送消息时使用一些 Processor,在这种情况下,由 Consumer 表示,然后由 Consumer 生成一个 ExchangeConsumer 查询它的 Endpoint 然后创建实际的 Exchange.

关于java - Camel 生产者消费者困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27594006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com