我正在尝试使用我知道会产生消息的主题中的消息(kafka生产者已经通过设计存在,我要做的就是编写使用消息的代码)。
这是我的代码:
public class ConsumerManager extends ShutdownableThread {
// private final KafkaConsumer<String, String> kafkaConsumer;
// static String brokers = Configuration.prop.getProperty("suite.pusher.defaultParams.kafkaBrokers");
static String brokers = "<kafka_server_ip>:9092";
static private Producer<String, String> m_kafkaProducers;
static private int m_CurrentProducerIndex;
static private int maxKafkaMessageSizeInMb = 8;
static private String kafkaCompressionCodec = "lz4";
static private String maxKafkaAcks = "1";
static private int maxKafkaRetries;
static private int kafkaBatchSize = 16384;
static private int kafkaBufferMemory = 33554432;
static private int kafkaConnectionsMaxIdleMs = 540000;
static private int kafkaLingerMs = 0;
static private int kafkaMaxBlocks = 100;
static private int kafkaMaxRequestSize = 1048576;
static private int kafkaRequestTimeoutMs = 300000;
static private int kafkaMaxInFlightRequestsPerConnection = 5;
static private int kafkaBatchNumMessages = 10000;
static private int kafkeQueueBufferingMaxMs = 10;
static private int kafkaFetchWaitMaxMs = 10;
static private int kafkaSocketBlockingMaxMs = 10;
static private boolean kafkaSocketNagleDisable = false;
String kafkaPrefix = Configuration.prop.getProperty("suite.pusher.defaultParams.kafkaTopicPrefix");
private final Boolean isAsync = false;
static String TOPIC = "proc_422_26952";//"proc_69686_61";//" "qa_69313_2";
private final KafkaConsumer<String, String> consumer;
private final String topics;
public ConsumerManager(String topic) {
super("KafkaConsumerExample", false);
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "CageConsumerGroup");
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");
props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "30000");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
consumer = new KafkaConsumer<String, String>(props);
this.topics = topic;
}
@Override
public void doWork() {
consumer.subscribe(Collections.singletonList(this.topics));
ConsumerRecords<String, String> records = consumer.poll(10000);
for (ConsumerRecord<String, String> record : records) {
System.out.println("Received message: (" + record.key() + ", " + record.value() + ") at offset " + record.offset());
}
}
@Override
public String name() {
return null;
}
@Override
public boolean isInterruptible() {
return false;
}
}
但是,当我到达以下行:ConsumerRecords<String, String> records = consumer.poll(10000);
时,什么也没有发生,并且测试完成,并且我看不到记录或知道该主题收到了消息,然后可以将它们显示在控制台上。
我想念什么?
答案 0 :(得分:0)
好,
我从头开始更改了所有消费者类,现在看来可以使用,这是我的代码:
public class ConsumerManager {
static String brokers = "<kafka_host>:9092";
private ExecutorService executor;
String kafkaPrefix = "proc_422_26952";
private final Boolean isAsync = false;
static String TOPIC; //"proc_69686_61";//" "qa_69313_2";
public ConsumerManager(String topicName) {
TOPIC = topicName;
Properties props = new Properties();
props.put("bootstrap.servers", brokers);
props.put("group.id", "CageConsumerGroupIO");
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("session.timeout.ms", "30000");
props.put("max.poll.records", "500");
props.put("key.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList(TOPIC));
System.out.println("Subscribed to topic " + TOPIC);
int i = 0;
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
System.out.printf("offset = %d, key = %s, value = %s\n",
record.offset(), record.key(), record.value());
System.out.println(record.value());
}
}
}