projects
/
demos
/
kafka
/
training
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Die App beendet sich, wenn der SimpleConsumer stolpert
[demos/kafka/training]
/
src
/
main
/
java
/
de
/
juplo
/
kafka
/
SimpleConsumer.java
diff --git
a/src/main/java/de/juplo/kafka/SimpleConsumer.java
b/src/main/java/de/juplo/kafka/SimpleConsumer.java
index
0e37686
..
45f9b94
100644
(file)
--- a/
src/main/java/de/juplo/kafka/SimpleConsumer.java
+++ b/
src/main/java/de/juplo/kafka/SimpleConsumer.java
@@
-9,21
+9,22
@@
import org.apache.kafka.common.errors.WakeupException;
import java.time.Duration;
import java.util.Arrays;
import java.time.Duration;
import java.util.Arrays;
+import java.util.concurrent.Callable;
@Slf4j
@RequiredArgsConstructor
@Slf4j
@RequiredArgsConstructor
-public class SimpleConsumer implements
Runnable
+public class SimpleConsumer implements
Callable<Integer>
{
private final String id;
private final String topic;
{
private final String id;
private final String topic;
- private final Consumer<String,
String
> consumer;
+ private final Consumer<String,
Message
> consumer;
private long consumed = 0;
@Override
private long consumed = 0;
@Override
- public
void run
()
+ public
Integer call
()
{
try
{
{
try
{
@@
-32,33
+33,31
@@
public class SimpleConsumer implements Runnable
while (true)
{
while (true)
{
- ConsumerRecords<String,
String
> records =
+ ConsumerRecords<String,
Message
> records =
consumer.poll(Duration.ofSeconds(1));
log.info("{} - Received {} messages", id, records.count());
consumer.poll(Duration.ofSeconds(1));
log.info("{} - Received {} messages", id, records.count());
- for (ConsumerRecord<String,
String
> record : records)
+ for (ConsumerRecord<String,
Message
> record : records)
{
{
- consumed++;
- log.info(
- "{} - {}: {}/{} - {}={}",
- id,
- record.offset(),
- record.topic(),
- record.partition(),
- record.key(),
- record.value()
- );
+ handleRecord(
+ record.topic(),
+ record.partition(),
+ record.offset(),
+ record.key(),
+ record.value());
}
}
}
catch(WakeupException e)
{
log.info("{} - Consumer was signaled to finish its work", id);
}
}
}
catch(WakeupException e)
{
log.info("{} - Consumer was signaled to finish its work", id);
+ return 0;
}
catch(Exception e)
{
log.error("{} - Unexpected error: {}, unsubscribing!", id, e.toString());
consumer.unsubscribe();
}
catch(Exception e)
{
log.error("{} - Unexpected error: {}, unsubscribing!", id, e.toString());
consumer.unsubscribe();
+ return 1;
}
finally
{
}
finally
{
@@
-67,4
+66,15
@@
public class SimpleConsumer implements Runnable
log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
}
}
log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
}
}
+
+ private void handleRecord(
+ String topic,
+ Integer partition,
+ Long offset,
+ String key,
+ Message value)
+ {
+ consumed++;
+ log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, value);
+ }
}
}