kafka-clients 0.10 消息消费者

package tuyou.kafka.consumer;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

/**
 * 消息消费者
 * 
 * @author:涂有
 * @date 2017年6月1日 上午11:27:10
 */
public class MsgConsumer {
	private String group = "MsgConsumer";
	private final int timeSection = 3000;
	private boolean autoCommit;
	private List topics = Arrays.asList("test");

	public MsgConsumer(String topic, String group, boolean autoCommit) {
		this.topics = Arrays.asList(topic);
		this.group = group;
		this.autoCommit = autoCommit;
	}

	public void consumer() {
		Properties properties = new Properties();
		properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getCanonicalName());// key反序列化方式
		properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getCanonicalName());// value反系列化方式
		properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, autoCommit);// 提交方式
		properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
				"192.168.100.115:2092,192.168.100.115:2093,192.168.100.115:2094");// 指定broker地址,来找到group的coordinator
		properties.put(ConsumerConfig.GROUP_ID_CONFIG, group);// 指定用户组

		KafkaConsumer consumer = new KafkaConsumer(properties);
		consumer.subscribe(topics);// 指定topic消费

		while (true) {
			System.out.println("开始poll---------------");
			ConsumerRecords records = consumer.poll(timeSection);// 拉取一次数据
			for (ConsumerRecord record : records) {
				System.out.println("topic: " + record.topic() + " key: " + record.key() + " value: " + record.value()
						+ " partition: " + record.partition());
			}
			if (!autoCommit) {
				consumer.commitAsync();// 手动commit
			}
			System.out.println("完成一次poll---------------");
		}
	}

	public static void main(String[] args) {
		new MsgConsumer("test", "MsgConsumer", true).consumer();
	}
}

你可能感兴趣的:(消息队列,kafka)