在驼峰中简化多播并聚合REST端点

时间:2018-02-02 18:32:25

标签: java apache-camel

尝试在camel中实现分散 - 聚集模式,因此我向3个不同的Web服务发送消息,并希望聚合它们的响应。我得到它的工作,但它看起来真的很复杂。试图清理它引发的混乱和异常。

这有效:

@Component
public class ProductBestPriceRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("timer://foo?repeatCount=10")
                .multicast()
                .to("direct:a")
                .to("direct:b")
                .to("direct:c");

        from("direct:a")
                .to("http4:localhost:9090/simple/product/foo")
                .to("direct:aggregate");

        from("direct:b")
                .to("http4:localhost:9091/simple/product/foo")
                .to("direct:aggregate");

        from("direct:c")
                .to("http4:localhost:9092/simple/product/foo")
                .to("direct:aggregate");

        from("direct:aggregate").log("${body}");
    }

这不会引发异常:

  

UnsupportedOperationException:无法从http端点消耗

  @Component
    public class ProductBestPriceRoute extends RouteBuilder {

        @Override
        public void configure() throws Exception {
            from("timer://foo?repeatCount=10")
                    .multicast()
                    .to("http4:localhost:9090/simple/product/foo")
                    .to("http4:localhost:9091/simple/product/foo")
                    .to("http4:localhost:9092/simple/product/foo");

            from("http4:localhost:9090/simple/product/foo").to("direct:aggregate");
            from("http4:localhost:9091/simple/product/foo").to("direct:aggregate");
            from("http4:localhost:9092/simple/product/foo").to("direct:aggregate");

            from("direct:aggregate").log("${body}");
        }
    }

Camel 2.20.1

Camel-http4 2.20.1

Spring boot 1.5.9.RELEASE

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-dependencies</artifactId>
            <version>2.20.1</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-zookeeper</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-gson</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
    </dependencies>

0 个答案:

没有答案