我们不能将java可序列化对象发布到kafka吗?

时间:2016-05-11 04:54:21

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

如果我将java对象转换为byte [],则kafka会消耗。但是如果我直接发布java对象,那么消费者迭代器就不会监听。我的迭代器是空的。我们不能这样做吗?

POJO

public class Test implements Serializable{
}

生产者

public void sendTest(String topic, Test test) throws CoreException {

        Producer<String,  Test> producer = null;
        try {
            producer = new Producer<String,  Test>(producerConfig);
            KeyedMessage<String, Test> msg = new KeyedMessage<String, Test>(topic,  Test);

            producer.send(msg);
            System.out.println("producing ..");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {
            if (producer != null)
                producer.close();
        }
    }

消费

ConsumerIterator<byte[], byte[]> itr = m_stream.iterator();
            while (itr.hasNext()) {
byte[] data = itr.next().message();
    System.out.println("Message received :" +data);
}

0 个答案:

没有答案