如何在Apache骆驼DSL中动态从端点返回

时间:2020-05-31 12:43:55

标签: apache-camel

这是我的代码

            from("google-pubsub:123:subscription1?maxMessagesPerPoll=3 & concurrentConsumers=5" ).routeId("myroute")
            .process(new ProducerProcessor())
        to("google-pubsub:123:topic1")
            ;

在上面的代码中,我想将from通道设为通用。基本上,它应该能够从good-pubsub中使用数据,或者可能来自文件或JMS队列。因此,取决于我想要的参数返回 与频道不同。如下所示

private RouteDefinition fromChannel(String parameter) {
        if (parameter is "google" then
            return  from("google-pubsub:123:subscription1?maxMessagesPerPoll=3 & concurrentConsumers=5" )


        if (parameter is "file" then
           return   from(/my/fileFolder/)).split(body().tokenize("\n")).streaming().parallelProcessing();



    }

我尝试了这个,但是在fromChannel方法中出现了空指针异常。如果您有更好的主意,请告诉我。

1 个答案:

答案 0 :(得分:0)

根据评论重写

例如,您可以为每种输入类型创建(静态)模板路由,并根据已配置的端点列表生成路由。

我在this answer中描述了这样的端点配置和路由生成方案。

像这样,您可以为每个文件路径以及其他路径类型的任何其他特殊路径生成分割部分。

所有这些输入路径均路由到公共处理路径

.from(pubsubEndpoint)
    .to("direct:genericProcessingRoute")

.from(fileEndpoint)
    .split(body()
    .tokenize("\n"))
    .streaming()
    .parallelProcessing()
    .to("direct:genericProcessingRoute")

.from("direct:genericProcessingRoute")
    ... [generic processing]
    .to("google-pubsub:123:topic1")

围绕公共核心路由的多个输入(和输出)路由称为hexagonal architecture,而Camel非常适合此。