Class KafkaTransport
- All Implemented Interfaces:
Closeable, AutoCloseable
Messaging over Apache Kafka, with no client library.
cn1.messaging.url=kafka://broker1:9092,broker2:9092
A destination is a topic, created by the broker on first use when it allows
that (auto.create.topics.enable, its default). A send is one record,
acknowledged by every in-sync replica (acks=all); a keyed one goes to the
partition Kafka's own partitioner would choose, so records of one key stay
in order, and an unkeyed one goes round the partitions.
A listener's group is a Kafka consumer group: each group sees every record, and within one the partitions are shared out among its members by range, as the Java client's default does. Records of one partition are handled one at a time, in order, and the group's offset moves past a record only after its handler returned; concurrency beyond the number of partitions a member holds therefore waits. Offsets are committed within a second of a handler finishing, and before the member gives its partitions up.
A record whose handler failed goes to <topic>.retry.<group>, which the
group also consumes: a partition of it waits until its first record is due,
so retries never hold up the topic itself -- what Spring Kafka calls a
non-blocking retry. One that fails every attempt goes to <topic>.dlq.
A new group starts at the earliest offset, so nothing sent before a
listener first started is skipped; cn1.messaging.kafka.auto-offset-reset
(or Spring Boot's spring.kafka.consumer.auto-offset-reset) set to latest
starts at the end instead. Records compressed with gzip can be read;
snappy, lz4 and zstd cannot, and a topic holding them fails its consumer.
Transactions and SASL are not supported.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRegisters this transport withMessaging; the generated wiring does it.Nested classes/interfaces inherited from class PollingTransport
PollingTransport.Delivery -
Field Summary
Fields inherited from class PollingTransport
retry -
Constructor Summary
ConstructorsConstructorDescriptionKafkaTransport(String url, RetryPolicy retry, int timeoutMillis, boolean earliest) A transport to the brokersurllists (kafka://host:port,..., orkafkas://for TLS); it connects on first use. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidack(String destination, String group, PollingTransport.Delivery delivery) Acknowledges a handled delivery.voidclose()Stops every subscription and closes the connection.getName()The scheme this transport was opened for:memory,db,redis,amqp,kafka.booleanWhether the broker answers, for the health endpoint.protected List<PollingTransport.Delivery> Up tomaxmessages ofdestinationforgroup, waiting up towaitMillisfor the first; an empty list when none came.protected voidredeliver(String destination, String group, PollingTransport.Delivery delivery, int attempt, long notBefore) Hasdeliverydelivered again, asattempt, no sooner thannotBefore.voidSendsmessage.subscribe(String destination, String group, int concurrency, MessageHandler handler) Startsconcurrencyconsumers ofdestinationingroup.Methods inherited from class PollingTransport
deadLetter, identified, notYet, stopSubscriptionsMethods inherited from class MessageTransport
isTransactional
-
Constructor Details
-
KafkaTransport
public KafkaTransport(String url, RetryPolicy retry, int timeoutMillis, boolean earliest) throws IOException A transport to the brokersurllists (kafka://host:port,..., orkafkas://for TLS); it connects on first use.- Throws:
IOException
-
-
Method Details
-
getName
Description copied from class:MessageTransportThe scheme this transport was opened for:memory,db,redis,amqp,kafka.- Specified by:
getNamein classMessageTransport
-
send
Description copied from class:MessageTransportSendsmessage. With a database transport inside a transaction the message is written in that transaction and is seen only if it commits.- Specified by:
sendin classMessageTransport- Throws:
IOException
-
redeliver
protected void redeliver(String destination, String group, PollingTransport.Delivery delivery, int attempt, long notBefore) throws IOException Description copied from class:PollingTransportHasdeliverydelivered again, asattempt, no sooner thannotBefore. The default sends a copy carrying the attempt and the time in its headers and acknowledges the original; a consumer that polls the copy early puts it back withPollingTransport.notYet(Message, long).- Overrides:
redeliverin classPollingTransport- Throws:
IOException
-
subscribe
public Subscription subscribe(String destination, String group, int concurrency, MessageHandler handler) throws IOException Description copied from class:MessageTransportStarts
concurrencyconsumers ofdestinationingroup.group: subscriptions of one group share the messages between them, and each group sees every message -- on a transport with groups (Redis Streams, Kafka, AMQP through one queue per group). Onmemory:anddb:a destination is a queue and every subscription competes for it.
- Overrides:
subscribein classPollingTransport- Throws:
IOException
-
poll
protected List<PollingTransport.Delivery> poll(String destination, String group, int max, long waitMillis) throws IOException Description copied from class:PollingTransportUp tomaxmessages ofdestinationforgroup, waiting up towaitMillisfor the first; an empty list when none came.- Specified by:
pollin classPollingTransport- Throws:
IOException
-
ack
Description copied from class:PollingTransportAcknowledges a handled delivery.- Specified by:
ackin classPollingTransport
-
isHealthy
public boolean isHealthy()Description copied from class:MessageTransportWhether the broker answers, for the health endpoint. Never throws.- Overrides:
isHealthyin classMessageTransport
-
close
public void close()Description copied from class:MessageTransportStops every subscription and closes the connection. Never throws.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classMessageTransport
-