Consumer
Consumer<K, V> (Interface)
Package: io.axual.client.consumer
The axualClient.buildConsumer()
call returns an implementation of Consumer
interface.
AxualClient client = ... (1)
ConsumerConfig consumerConfig = ConsumerConfig.builder()
.setKeyDeserializer(...)
...
.build(); (2)
Processor<K, V> processor = ... (3)
Consumer<K, V> consumer = client.buildConsumer(consumerConfig); (4)
consumer.startConsuming(...); (5)
1 | Build AxualClient object. |
2 | Build ConsumerConfig object. |
3 | Build Processor object. |
4 | Build Consumer object. |
5 | Start consuming |
Methods
Future<ConsumeFailedException> startConsuming()
Start consuming process. Returns a Future
object wrapping a
ConsumeFailedException
ConsumerMessage<K, V> (Interface)
A wrapper for the consumed message passed to the Processor
. Includes metadata information
like partition, offset and timestamp.
Processor<K, V> (Interface)
Implement this interface to process each consumed message.
class MyProcessor implements Processor<K, V> {
@Override
public void processMessage(ConsumerMessage<K, V> message) {
log.info("Processing message: Key: {}, Value: {}", message.getKey(), message.getValue());
}
}
If using Java 8, above code can be simplified using Streams API as below:
Processor<K, V> processor = message -> log.info("Processing message: Key: {}, Value: {}", message.getKey(), message.getValue());
DeliveryStrategy
An Enum used used to manage consumer behavior.
AT_MOST_ONCE
This strategy will try to deliver messages once (or less) and continue processing in all failure cases. In case something fails the message will be lost. For Consumers this means skipping to the end of a new PartitionOffset upon subscribing.
AT_LEAST_ONCE
This strategy will retry to deliver messages upon technical failures, potentially leading to message duplication in certain situations. Duplication is considered to be a lesser evil by applications that adopt this strategy. Be sure to be idempotent in your application when adopting this strategy. For Consumers this strategy jumps to the beginning of the stream if the consumer’s group id did not commit any offset beforehand.