消费来自骆驼路由器的kafka消息

时间:2019-03-08 13:50:20

标签: apache-kafka apache-camel

我可以从骆驼路由器向Kafka生成消息,但无法使用生成的消息,我的代码如下

我正在使用camel-kafka v2.17.0和Apache Camel v2.22.0

//to Produce the message which is successful    
from("direct:start").process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    System.out.println("Sending to Kafka");
                    exchange.getIn().setHeader(KafkaConstants.PARTITION_KEY, 0);
                    exchange.getIn().setHeader(KafkaConstants.KEY, "1");
                }
            }).to("kafka:localhost:9092?topic=eventTopic").to("stream:out");

//To consume the message which is failing i have doubts with group ID
            from("kafka:localhost:9092?topic=eventTopic&groupId=foo&autoOffsetReset=earliest&consumersCount=1")
            .process(new Processor() {

                        @Override
                        public void process(Exchange exchange) throws Exception {
                            System.out.println("Recieved from Kafka");
                            String messageKey = "";
                            if (exchange.getIn() != null) {
                                Message message = exchange.getIn();
                                Integer partitionId = (Integer) message.getHeader(KafkaConstants.PARTITION);
                                String topicName = (String) message.getHeader(KafkaConstants.TOPIC);
                                if (message.getHeader(KafkaConstants.KEY) != null)
                                    messageKey = (String) message.getHeader(KafkaConstants.KEY);
                                Object data = message.getBody();

                                System.out.println("topicName :: " + topicName + " partitionId :: " + partitionId
                                        + " messageKey :: " + messageKey + " message :: " + data + "\n");
                            }
                        }
                    }).to("log:input");

0 个答案:

没有答案