kafka 官方示例代码--消费者
2016-05-12 10:44
706 查看
kafka 0.9.0添加了一套新的Java 消费者API,用以替换之前的high-level API (基于ZK) 和low-level API。新的Java消费者API目前为测试版。另外kafka 0.9暂时还支持0.8的Client。
View Code
1、High Level Consumer(0.8)
public class KafkaConsumerRunner implements Runnable { private final AtomicBoolean closed = new AtomicBoolean(false); private final KafkaConsumer consumer; public void run() { try { consumer.subscribe(Arrays.asList("topic")); while (!closed.get()) { ConsumerRecords records = consumer.poll(10000); // Handle new records } } catch (WakeupException e) { // Ignore exception if closing if (!closed.get()) throw e; } finally { consumer.close(); } } // Shutdown hook which can be called from a separate thread public void shutdown() { closed.set(true); consumer.wakeup(); } } Then in a separate thread, the consumer can be shutdown by setting the closed flag and waking up the consumer. closed.set(true); consumer.wakeup();
View Code
相关文章推荐
- JAVA并发实现五(生产者和消费者模式Condition方式实现)
- asp.net控件DataList分页用法
- 用java实现二叉树相关操作(前序建树,前中后递归非递归遍历,层序遍历)
- 使用jni实现在C语言中打印log信息
- 堆排序
- Google 的开源技术protobuf
- python list
- java去空格
- iOS OC语言: Block底层实现原理
- Java正则表达式匹配URL中的host
- 无插件Vim编程技巧
- 在ASP.NET 2.0中操作数据之三十八:处理BLL和DAL的异常
- Asp.Net请求处理过程
- java生成订单编号工具
- Spring (依赖注入)
- Java知识图谱收集整理(学习路线等问题)
- C语言实现去除字符串中空格的简单实例
- java集合学习
- STM32F4板子使用LWIP进行组播收发数据的完整过程,附代码
- Spring (依赖注入)