在camel路由中,代码控制无法进入process()方法

时间:2016-07-14 10:45:25

标签: apache-camel apache-kafka kafka-consumer-api

我正在尝试使用camel消费Kafka消息。

以下代码:

public class Main {

private static CamelContext context = new DefaultCamelContext();

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
        context.start();
        context.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {

                System.out.println("Configuring Routes");

                from("kafka:172.16.30.5:9093?topic=reddy&groupId=testing&autoOffsetReset=earliest&consumersCount=1")                
                .process(new Processor(){

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


                        /*System.out.println("Processing..");
                        System.out.println("Messages:");
                        System.out.println(exchange.getIn());*/

                        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");
                        }

                        }

                });

            }
        });
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

输出:

java -jar camelKafka-0.0.1-SNAPSHOT-jar-with-dependencies.jar 
log4j:WARN No appenders could be found for logger     (org.apache.camel.impl.DefaultCamelContext).
log4j:WARN Please initialize the log4j system properly.
Configuring Routes

也就是说,代码控制根本不会进入流程方法。

1 个答案:

答案 0 :(得分:2)

您需要让Camel保持运行,请参阅此文档:http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html