projects
/
demos
/
kafka
/
seek
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
WIP
[demos/kafka/seek]
/
src
/
main
/
java
/
de
/
juplo
/
kafka
/
seek
/
Consumer.java
diff --git
a/src/main/java/de/juplo/kafka/seek/Consumer.java
b/src/main/java/de/juplo/kafka/seek/Consumer.java
index
1fb4cdd
..
77e156b
100644
(file)
--- a/
src/main/java/de/juplo/kafka/seek/Consumer.java
+++ b/
src/main/java/de/juplo/kafka/seek/Consumer.java
@@
-26,6
+26,7
@@
public class Consumer implements Runnable
private final KafkaConsumer<Long, String> consumer;
private boolean running = false;
private final KafkaConsumer<Long, String> consumer;
private boolean running = false;
+ Long offset = null;
Future<?> future = null;
Future<?> future = null;
@@
-45,6
+46,7
@@
public class Consumer implements Runnable
props.put("bootstrap.servers", bootstrapServer);
props.put("group.id", groupId);
props.put("client.id", clientId);
props.put("bootstrap.servers", bootstrapServer);
props.put("group.id", groupId);
props.put("client.id", clientId);
+ props.put("commit.interval.ms", 500);
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
@@
-55,16
+57,25
@@
public class Consumer implements Runnable
@Override
public void run()
{
@Override
public void run()
{
- log.info("{} - Subscribing to topic test", id);
- consumer.subscribe(Arrays.asList(topic));
-
try
{
try
{
-
-
running = true
;
+ log.info("{} - Subscribing to topic test", id);
+
consumer.subscribe(Arrays.asList(topic))
;
while (running)
{
while (running)
{
+ if (offset != null)
+ {
+ log.info("{} - seeking to offset {}", id, offset);
+ consumer
+ .partitionsFor(topic)
+ .forEach(partition ->
+ consumer.seek(
+ new TopicPartition(topic, partition.partition()),
+ offset));
+ offset = null;
+ }
+
ConsumerRecords<Long, String> records = consumer.poll(Duration.ofSeconds(1));
for (ConsumerRecord<Long, String> record : records)
log.info(
ConsumerRecords<Long, String> records = consumer.poll(Duration.ofSeconds(1));
for (ConsumerRecord<Long, String> record : records)
log.info(
@@
-88,20
+99,16
@@
public class Consumer implements Runnable
finally
{
log.info("{} - Unsubscribing...", id);
finally
{
log.info("{} - Unsubscribing...", id);
- consumer.unsubscribe();
running = false;
running = false;
+ consumer.unsubscribe();
+ offset = null;
}
}
public void seek(long offset)
{
}
}
public void seek(long offset)
{
- consumer
- .partitionsFor(topic)
- .forEach(partition ->
- consumer.seek(
- new TopicPartition(topic, partition.partition()),
- offset));
+ this.offset = offset;
}
}
@@
-111,13
+118,14
@@
public class Consumer implements Runnable
throw new RuntimeException("Consumier instance " + id + " is already running!");
log.info("Running {}", id);
throw new RuntimeException("Consumier instance " + id + " is already running!");
log.info("Running {}", id);
+ running = true;
future = executor.submit(this);
}
public synchronized void stop() throws ExecutionException, InterruptedException
{
if (!running)
future = executor.submit(this);
}
public synchronized void stop() throws ExecutionException, InterruptedException
{
if (!running)
- throw new RuntimeException("Consum
i
er instance " + id + " is not running!");
+ throw new RuntimeException("Consumer instance " + id + " is not running!");
log.info("Stopping {}", id);
running = false;
log.info("Stopping {}", id);
running = false;
@@
-125,7
+133,6
@@
public class Consumer implements Runnable
future.get();
}
future.get();
}
-
@PreDestroy
public void destroy() throws ExecutionException, InterruptedException
{
@PreDestroy
public void destroy() throws ExecutionException, InterruptedException
{