简体   繁体   中英

Kafka consumer using Apache Camel

I am new to Apache camel. we are doing POC to develop kafka consumer using Camel. Below is the sample code.

    context.addRoutes(new RouteBuilder(){

      @Override
        public void configure() throws Exception {
            // TODO Auto-generated method stub

         from("kafka:{{consumer.topic}}?brokers={{kafka.host}}:{{kafka.port}}" 
                     + "&consumersCount={{consumer.consumersCount}}" 
                     + "&seekTo={{consumer.seekTo}}" 
                     + "&groupId={{consumer.group}}")
             .process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {

                    Message message = exchange.getIn();
                    Object data = message.getBody();

                    System.out.println(data);
                }
             })
             .to("seda:end");


  });

        context.start();

    ConsumerTemplate template=context.createConsumerTemplate();
    String info=template.receiveBody("seda:end",String.class);

    System.out.println(info);
}

I am having below issues:

  1. Context stops immediately after start.
  2. If I poll To endpoint using consumer template, it does not print anything whereas inside .process(), I am able to print kafka messages when I start context in infinite loop. Why consumer template is not able to print.
  1. As Claus already commented, your Camel Context is shutting down immediately because it is not blocking. See the links in his comment.
  2. I think you missed a template.start(); to start your Consumer. See this link for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM