【问题标题】:Use exchange message inside the .to() method in apache camel在 apache camel 的 .to() 方法中使用交换消息
【发布时间】:2019-04-03 12:34:28
【问题描述】:

我是骆驼的新手,想根据事先执行的一些逻辑动态改变我的路线

camelContext.addRoutes(new RouteBuilder() {
        public void configure() {
            PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
            pc.setLocation("classpath:application.properties");

            log.info("About to start route: Kafka Server -> Log ");

            from("kafka:{{consumer.topic}}?brokers={{kafka.host}}:{{kafka.port}}"
                    + "&maxPollRecords={{consumer.maxPollRecords}}"
                    + "&consumersCount={{consumer.consumersCount}}"
                    + "&seekTo={{consumer.seekTo}}"
                    + "&groupId={{consumer.group}}"
                    + "&valueDeserializer=" + BytesDeserializer.class.getName())
                    .routeId("FromKafka")
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            System.out.println(" message: " + exchange.getIn().getBody());
                            Bytes body = exchange.getIn().getBody(Bytes.class);
                            HashMap data = (HashMap)SerializationUtils.deserialize(body.get());
                            // do some work on data;
                            Map messageBusDetails = new HashMap();
                            messageBusDetails.put("topicName", "someTopic");
                            messageBusDetails.put("producerOption", "bla");
                            exchange.getOut().setHeader("kafka", messageBusDetails);
                            exchange.getOut().setBody(SerializationUtils.serialize(data));
                        }
                    }).choice()
                        .when(header("kafka"))
                            .to("kafka:"+ **getHeader("kafka").get("topicName")** )
                .log("${body}");
        }
    });

getHeader("kafka").get("topicName")

这就是我想要达到的目标。

但我不知道如何访问 .to() 中的标头值(这是一个映射 - 导致 kafka 生产者可能有更多配置)

我知道我可能完全错误地使用它......但这就是我到现在为止所理解的......

主要目标是拥有多个消息总线作为 .from() 以及 .to() 中的多个消息总线选项,这些选项将通过外部源(如配置文件)决定,这样相同的路由将适用于许多逻辑场景 我认为choice() 方法是最好的答案 谢谢!

【问题讨论】:

    标签: apache-kafka apache-camel


    【解决方案1】:

    您可以使用 toD() 代替 to(),即“动态至” See this了解详情

    关于用于拉入各种标题等的语法,请参阅Simple expression page

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多