Camel - 内容丰富器:enrich() 与 pollEnrich() [英] Camel - content enricher: enrich() vs pollEnrich()

查看:25
本文介绍了Camel - 内容丰富器:enrich() 与 pollEnrich()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题:

我无法真正理解 enrich()pollEnrich() 之间的区别.也许 Camel 使用的术语不太好.

我在这里阅读:http://camel.apache.org/content-enricher.html<块引用>

使用丰富 DSL 元素丰富内容

Camel 在 DSL 中提供了两种内容丰富器

  • 丰富
  • pollEnrich

enrich 使用 Producer 来获取附加数据.通常是用于请求回复消息,例如调用外部网络服务.另一方面, pollEnrich 使用轮询消费者获取附加数据.它通常用于事件消息消息传递,例如读取文件或下载 FTP 文件.

我不明白有什么区别.他们似乎都通过消费来获取额外的数据(Web 服务响应、FTP 文件).那么为什么他们说获取 Web 服务响应是由生产者"完成的呢?

第二个问题:

在Camel in action"一书中72 他们说:

<块引用>

Enrich 和 pollEnrich 无法访问当前交易所的信息

enrich 和 pollEnrich 都不能利用任何信息从目前的交易所.这意味着,例如,您不能在交换机上存储一个文件名头,以供 pollEnrich 使用选择一个特定的文件.如果骆驼在未来,这可能会改变团队可以找到解决方案.

然而,他们给出了一个类似于以下的代码示例,用于实现聚合策略:

public class ExampleAggregationStrategy 实现 AggregationStrategy {公共交换聚合(交换原始,交换资源){对象 originalBody = original.getIn().getBody();对象资源响应 = resource.getIn().getBody();Object mergeResult = ...//合并原始正文和资源响应if (original.getPattern().isOutCapable()) {original.getOut().setBody(mergeResult);} 别的 {original.getIn().setBody(mergeResult);}退回原件;}}

在这个例子中,我看到他们可以访问Exchange original,这不是当前交易所"吗?如果不是,那么原始交换"代表什么交换?他们所说的当前交换"是什么意思?

解决方案

区别如下:

  • enrich 假定您希望使用传入的 Exchange 作为另一个服务请求的参数.例如,您传入的 Exchange 可能是一个 userID,但您确实需要整个 User 对象,因此您可以通过将 userID 传递给返回成为 Exchange 的 User 对象的 REST 服务来enrich它,等

  • pollEnrich 假设传入的 Exchange 是一个简单的触发器,它告诉 PollingConsumer 查找数据并创建一个 Exchange(忽略传入 Exchange 的内容).例如,您可能有一个计时器或其他需要获取文件进行处理的业务流程事件,等等,也就是说,传入的 Exchange 数据不用于动态配置 PollingConsumer...只有 URI 用于此.

也就是说,从 Camel 2.12 开始,可以选择指定聚合策略来组合 enrichpollEnrich 的传入/返回 Exchange

此单元测试 使用聚合策略的 pollEnrich 示例

1ST QUESTION:

I'm not able to really understand the difference between enrich() and pollEnrich(). Maybe the terms Camel uses are not so great.

I read here: http://camel.apache.org/content-enricher.html

Content enrichment using the enrich DSL element

Camel comes with two flavors of content enricher in the DSL

  • enrich
  • pollEnrich

enrich uses a Producer to obtain the additional data. It is usually used for Request Reply messaging, for instance to invoke an external web service. pollEnrich on the other hand uses a Polling Consumer to obtain the additional data. It is usually used for Event Message messaging, for instance to read a file or download a FTP file.

I don't understand what the difference is. They both seem to get the additional data (Web service response, FTP file) by consuming it. So why do they say getting the Web service response is done by a "producer"?

2ND QUESTION:

In the "Camel in action" book p. 72 they say:

Enrich and pollEnrich can’t access information in the current exchange

Neither enrich nor pollEnrich can leverage any information from the current exchange. This means, for example, that you can’t store a filename header on the exchange for pollEnrich to use to select a particular file. This may change in the future if the Camel team can find a solution.

However they give a code example similar to the following, for implementing an aggregation strategy:

public class ExampleAggregationStrategy implements AggregationStrategy {

    public Exchange aggregate(Exchange original, Exchange resource) {
        Object originalBody = original.getIn().getBody();
        Object resourceResponse = resource.getIn().getBody();
        Object mergeResult = ... // combine original body and resource response
        if (original.getPattern().isOutCapable()) {
            original.getOut().setBody(mergeResult);
        } else {
            original.getIn().setBody(mergeResult);
        }
        return original;
    }

}

In this example I see that they have access to the Exchange original, is it not the "current exchange"? If not, then what exchange does the "original exchange" represent? And what do they mean by the "current exchange"?

解决方案

Here is the difference:

  • enrich assumes you want to use an incoming Exchange as a parameter to an another service request. for example, your incoming Exchange could be a userID, but you really need the entire User object, so you could enrich it by passing the userID to a REST service that returns the User object which becomes the Exchange, etc.

  • pollEnrich assumes the incoming Exchange is a simple trigger that tell a PollingConsumer to look for data and create an Exchange (ignoring the contents of the incoming Exchange). For example, you could have a timer or other business process event that requires picking up a file for processing, etc. that said, the incoming Exchange data is NOT used to dynamically configure the PollingConsumer...only the URI is used for this.

That said, as of Camel 2.12, there is an option to specify an aggregation strategy to combine the incoming/returned Exchanges for both enrich and pollEnrich

See this unit test for an example of pollEnrich with an aggregation strategy

这篇关于Camel - 内容丰富器:enrich() 与 pollEnrich()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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