Producer

Producer<K, V> (Interface)

Package: io.axual.client.producer

The axualClient.buildProducer() call returns an implementation of Producer interface.

AxualClient client = ... (1)

ProducerConfig producerConfig = ProducerConfig.builder()
	.setKeySerializerClassName(...)
	...
	.build(); (2)

Producer<K, V> producer = client.buildProducer(producerConfig); (3)

ProducerMessage<K, V> message = ProducerMessage.<K, V>newBuilder()
	.setStream(...)
	...
	.build(); (4)

producer.produce(...); (5)
1 Build AxualClient object.
2 Build ProducerConfig object.
3 Build Producer object.
4 Build ProducerMessage object.
5 Start producing

Methods

Future<ProducedMessage<K, V>> produce(ProducerMessage<K, V> message)

This method accepts a ProducerMessage object and returns a Future object containing a ProducerMessage object.

Future<ProducedMessage<K, V>> produce(ProducerMessage<K, V> message, ProduceCallback<K, V> callback)

This method also accepts a ProduceCallback object which will be called after a successful/failed produce.

void close()

Closes the underlying producer object.

ProducerMessage<K, V>

A wrapper object containing the key, value and stream among other parameters of the message to be produced.

ProducerMessage<K, V> message = ProducerMessage.<K, V>newBuilder()
    .setStream(...)
    .setKey(...)
    .setValue(...)
    .build();

// produce message
producer.produce(message);

ProducedMessage<K, V>

A wrapper object returned after a successful produce() call. It includes metadata information including the partition of the topic, offset of the message and timestamp of message produced.

It also contains the name of the underlying Axual Kafka Cluster and the logical instance to which the producer is connected.

To access the ProducedMessage object, call the get() method on the Future object.

Future<ProducedMessage<K, V>> future = producer.produce(...);

ProducedMessage<K, V> message = future.get();

ProduceCallback<K, V>

An interface that can be implemented and passed to produce() call for an automatic callback.

Methods

void onComplete(ProducedMessage<K, V> message)

If produce was successful, this method will be called with the produced message. Business logic to process the message can go inside this method.

void onError(ProducerMessage<K, V> message, ExecutionException exception)

If produce was unsuccessful, this method will be called with the original message and an exception object with the error details.

ProducerStrategy

An Enum used for the producers, the strategy is used to manage the produce and retry policy.

AT_MOST_ONCE

This strategy should be used when acknowledgement from Kafka is not needed for every produce. There are no guarantees that every message will be successfully produced.

AT_LEAST_ONCE_LOSING_ORDER

This strategy should be used when acknowledgement from Kafka is required. It will guarantee that every message is produced successfully. Messages may arrive on the stream out of order and there is possibility of duplicate messages (due to retries).

AT_LEAST_ONCE_KEEPING_ORDER

This strategy should be used when acknowledgement from Kafka is required. It will guarantee that every message is produced successfully. Messages will arrive on the stream in order due to synchronous produce and there is possibility of duplicate messages (due to retries).