majialoong commented on code in PR #23137:
URL: https://github.com/apache/kafka/pull/23137#discussion_r4589890220


##########
examples/src/main/java/kafka/examples/KafkaRebalanceListenerDemo.java:
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package kafka.examples;
+
+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.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.clients.consumer.RebalanceConsumer;
+import org.apache.kafka.clients.consumer.RebalanceListener;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.WakeupException;
+import org.apache.kafka.common.serialization.IntegerDeserializer;
+import org.apache.kafka.common.serialization.StringDeserializer;
+
+import java.time.Duration;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Demonstrates manual offset management with a {@link RebalanceListener} and 
the callback-scoped
+ * {@link RebalanceConsumer}. The {@code RebalanceConsumer} passed to a 
callback is valid only while that callback is
+ * executing and must not be retained for later use.
+ *
+ * <p>Expected callback behavior:
+ * <ul>
+ *   <li>{@code onPartitionsAssigned}: reports assigned partitions' starting 
positions,
+ *       initialized from committed offsets or the earliest offsets.</li>
+ *   <li>{@code onPartitionsRevoked}: commits the next offsets for revoked 
partitions
+ *       before giving up ownership.</li>
+ *   <li>{@code onPartitionsLost}: reports lost partitions without committing 
their offsets.</li>
+ * </ul>
+ */
+public class KafkaRebalanceListenerDemo {
+    private static final String TOPIC_NAME = "rebalance-listener-demo";
+    private static final String GROUP_NAME = "rebalance-listener-group";
+    private static final Duration POLL_TIMEOUT = Duration.ofSeconds(1);
+    private static final Duration CALLBACK_OPERATION_TIMEOUT = 
Duration.ofSeconds(10);
+
+    public static void main(String[] args) {
+        String clientId = "rebalance-consumer-" + 
UUID.randomUUID().toString().substring(0, 8);
+        KafkaConsumer<Integer, String> consumer = new 
KafkaConsumer<>(consumerProperties(clientId));

Review Comment:
   I've added an optional `classic` or `consumer` argument so users can compare 
callback behavior. The default remains classic.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to