如何在OSGI框架中配置Kafka 0.9.0.0使用者?

时间:2016-05-11 14:18:32

标签: java osgi apache-kafka osgi-bundle kafka-consumer-api

我有OSGI框架,在其中,我接受一个捆绑中的REST调用,其余调用中收到的数据被发送到KAFKA brocker。还有另一个包将消耗来自brocker的消息。

如果我在REST捆绑包之前初始化KAFKA Consumer捆绑包,则永远不会调用REST bundleActivator,因为代码在KAFKA Consumer代码的while循环中运行。如果我在消费者捆绑之前初始化REST捆绑包,则消费者捆绑包永远不会启动。

以下是KAFKA Bundle的Activator代码:

public class KafkaConsumerActivator implements BundleActivator {

    private static final String ZOOKEEPER_CONNECT = "zookeeper.connect";
    private static final String GROUP_ID = "group.id";
    private static final String BOOTSTRAP_SERVERS = "bootstrap.servers";
    private static final String KEY_DESERIALIZER = "key.deserializer";

    private ConsumerConnector consumerConnector;

    private KafkaConsumer<String, String> consumer;

    private static final String VALUE_DESERIALIZER = "value.deserializer";
    public void start(BundleContext context) throws Exception {

        Properties properties = new Properties();
        properties.put(ZOOKEEPER_CONNECT,
            MosaicThingsConstant.KAFKA_BROCKER_IP + ":" + MosaicThingsConstant.ZOOKEEPER_PORT);
        properties.put(GROUP_ID, MosaicThingsConstant.KAFKA_GROUP_ID);
        properties.put(BOOTSTRAP_SERVERS,
            MosaicThingsConstant.KAFKA_BROCKER_IP + ":" + MosaicThingsConstant.KAFKA_BROCKER_PORT);
        properties.put(KEY_DESERIALIZER, StringDeserializer.class.getName());
        properties.put(VALUE_DESERIALIZER, StringDeserializer.class.getName());
        consumer = new KafkaConsumer<>(properties);

        try {
            consumer.subscribe(Arrays.asList(MosaicThingsConstant.KAFKA_TOPIC_NAME));

            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(Long.MAX_VALUE);
                for (ConsumerRecord<String, String> record : records) {
                    Map<String, Object> data = new HashMap<>();
                    data.put("partition", record.partition());
                    data.put("offset", record.offset());
                    data.put("value", record.value());
                    System.out.println(": " + data);
                }
            }
        } catch (WakeupException e) {
            // ignore for shutdown
        } finally {
            consumer.close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你永远不应该在Activator的start方法中做一些需要很长时间的事情。它将阻止整个OSGi框架。

您最好在额外的线程中执行整个连接和循环。在stop方法中,您可以告诉该线程退出。