273.7. 请求到 Camel 的转换
在一些 Camel DSL 中定义的路由可以在被动流框架中使用来执行特定的转换(相同的机制也可用于将数据发送到 http 端点并继续)。
以下代码片段演示了 RxJava 功能代码如何向 Camel 请求加载和汇总文件的任务。
CamelReactiveStreamsService camel = CamelReactiveStreams.get(context);
// Process files starting from their names
Flowable.just(new File("file1.txt"), new File("file2.txt"))
.flatMap(file -> camel.toStream("readAndMarshal", String.class))
// Camel output will be converted to String
// other steps
.subscribe();
为了正常工作,应在 Camel 上下文中定义类似以下内容的路由:
from("reactive-streams:readAndMarshal")
.marshal() // ... other details
273.7.1. 使用直接 API 请求到 Camel 的转换 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
另一种方法包括在被动流中直接使用 URI 端点:
CamelReactiveStreamsService camel = CamelReactiveStreams.get(context);
// Process files starting from their names
Flowable.just(new File("file1.txt"), new File("file2.txt"))
.flatMap(file -> camel.to("direct:process", String.class))
// Camel output will be converted to String
// other steps
.subscribe();
当使用 to () 方法而不是 toStream 时,不需要使用 "reactive-streams:" 端点(尽管它们在 hood 下使用)来定义路由。
在这种情况下,Camel 转换只能:
from("direct:process")
.marshal() // ... other details