如果我将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);
}