如何使用 Apache Camel 处理 Long-运行 任务
How to handle Long-Running Task with Apache Camel
我有一个 Camel 根,它在根中间执行 HTTP 调用以丰富交换中的主要对象:
<route>
<from uri="http:somerestcall"/>
<bean ref="jsonUtils" method="someJsonConversion"/>
<split>
<simple>body</simple>
<!-- this is the one -->
<bean ref="enrichmentBean" method="someAdditionalHTTPCall"/>
</split>
<to uri="writetosomequeue"/>
</route>
我知道 Camel 提供了很多 asynchronous functionality - 我如何最好地利用 Camel 来处理路由中间可能很长的 运行 HTTP 调用,没有让线程陷入睡眠模式?
这个问题在某种程度上是特定于应用程序的,如果您的线程正在等待 HTTP 请求的响应并且需要该信息才能继续,我只会让进程更加并行并且让线程什么都不做。如果线程不工作,它不会浪费真正的 cpu。但是,如果您的进程在等待 http 调用时可以继续工作,那么我建议将其发送到 seda 路由以进行查找调用,然后将结果存储在您以后可以访问的地方,就像您可以参考结果的并发映射一样基于某种密钥。
<route>
<from uri="http:somerestcall"/>
<bean ref="jsonUtils" method="someJsonConversion"/>
<split>
<simple>body</simple>
<!-- this is the one -->
<to uri="seda:concurrentFun" />
</split>
<to uri="Lots of other work" />
<bean ref="StorageBean" method="enrichFromStoreAndRemove" />
<to uri="writetosomequeue"/>
</route>
<route>
<From uri="seda:concurrentFun" />
<bean ref="enrichmentBean" method="someAdditionalHTTPCall"/>
<bean ref="StorageBean" method="store" />
</route>
我有一个 Camel 根,它在根中间执行 HTTP 调用以丰富交换中的主要对象:
<route>
<from uri="http:somerestcall"/>
<bean ref="jsonUtils" method="someJsonConversion"/>
<split>
<simple>body</simple>
<!-- this is the one -->
<bean ref="enrichmentBean" method="someAdditionalHTTPCall"/>
</split>
<to uri="writetosomequeue"/>
</route>
我知道 Camel 提供了很多 asynchronous functionality - 我如何最好地利用 Camel 来处理路由中间可能很长的 运行 HTTP 调用,没有让线程陷入睡眠模式?
这个问题在某种程度上是特定于应用程序的,如果您的线程正在等待 HTTP 请求的响应并且需要该信息才能继续,我只会让进程更加并行并且让线程什么都不做。如果线程不工作,它不会浪费真正的 cpu。但是,如果您的进程在等待 http 调用时可以继续工作,那么我建议将其发送到 seda 路由以进行查找调用,然后将结果存储在您以后可以访问的地方,就像您可以参考结果的并发映射一样基于某种密钥。
<route>
<from uri="http:somerestcall"/>
<bean ref="jsonUtils" method="someJsonConversion"/>
<split>
<simple>body</simple>
<!-- this is the one -->
<to uri="seda:concurrentFun" />
</split>
<to uri="Lots of other work" />
<bean ref="StorageBean" method="enrichFromStoreAndRemove" />
<to uri="writetosomequeue"/>
</route>
<route>
<From uri="seda:concurrentFun" />
<bean ref="enrichmentBean" method="someAdditionalHTTPCall"/>
<bean ref="StorageBean" method="store" />
</route>