package com.cosmos.kafka.client.consumer;

import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static org.apache.kafka.clients.consumer.ConsumerConfig.*;

public class SimpleHLConsumer implements Runnable {

    private final ConsumerConnector consumer;
    private final String topic;

    public SimpleHLConsumer(String topic) {
        consumer = Consumer.createJavaConsumerConnector(createConsumerConfig());
        this.topic = topic;
    }

    private static ConsumerConfig createConsumerConfig() {
        Properties props = new Properties();
        props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(GROUP_ID_CONFIG, "test-consumer-group");
        props.put(AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");

        return new ConsumerConfig(props);
    }

    @Override
    public void run() {
        System.out.println("Listening...");
        Map<String, Integer> topicCountMap = new HashMap<>();
        topicCountMap.put(topic, 1);
        Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
        List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
        for (final KafkaStream<byte[], byte[]> stream : streams) {
            ConsumerIterator<byte[], byte[]> it = stream.iterator();
            while (it.hasNext()) {
                System.out.printf("Receive: %s\n", new String(it.next().message()));
                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        consumer.shutdown();
    }
}

@@ -975,16 +975,240 @@
 %221000%22);
+%0A        props.put(%22zookeeper.connect%22, %22localhost:2181%22);%0A        props.put(%22zookeeper.session.timeout.ms%22, %2240000%22);%0A        props.put(%22zookeeper.sync.time.ms%22, %22200%22);%0A        props.put(%22auto.commit.interval.ms%22, %221000%22);
 %0A%0A      

add missing properties