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, snappy, lz4 or
zstd are read; what this transport sends is uncompressed.
cn1.messaging.kafka.isolation-level (or Spring Boot's
spring.kafka.consumer.isolation-level) set to read_committed hides the
records of aborted transactions and of transactions still open, as the
Java client does; the default, read_uncommitted, is the Java client's
default too. Sending in a transaction is not supported.
Authentication
SASL is configured as the Java client configures it, under
cn1.messaging.kafka.sasl.* or Spring Boot's own keys:
cn1.messaging.url=kafkas://pkc-xxxxx.eu-west-1.aws.confluent.cloud:9092
cn1.messaging.kafka.sasl.mechanism=PLAIN
cn1.messaging.kafka.sasl.username=<api key>
cn1.messaging.kafka.sasl.password=<api secret>
| Key | Spring Boot equivalent |
|---|---|
cn1.messaging.kafka.sasl.mechanism |
spring.kafka.properties.sasl.mechanism |
cn1.messaging.kafka.sasl.username, .password |
the username and password of spring.kafka.properties.sasl.jaas.config |
cn1.messaging.kafka.security.protocol |
spring.kafka.security.protocol |
cn1.messaging.kafka.sasl.aws-region |
none: the region of an MSK host name, or AWS_REGION |
The mechanisms are PLAIN, SCRAM-SHA-256, SCRAM-SHA-512 and
AWS_MSK_IAM. A security protocol of SSL or SASL_SSL turns TLS on as
kafkas:// does. Credentials in the URL, kafka://user:password@host
(percent-encoded), mean PLAIN unless a mechanism is set. Azure Event Hubs
takes PLAIN with the user name $ConnectionString and the connection
string as the password, on port 9093 over TLS; Amazon MSK takes
SCRAM-SHA-512 or AWS_MSK_IAM, whose credentials come from the
environment, the container or the instance, as for S3.
-
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), without SASL; it connects on first use.KafkaTransport(String url, RetryPolicy retry, Config config) A transport to the brokersurllists, configured byconfigas the class description says: the timeout, the offset reset, the isolation level, TLS and SASL. -
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), without SASL; it connects on first use.- Throws:
IOException
-
KafkaTransport
A transport to the brokersurllists, configured byconfigas the class description says: the timeout, the offset reset, the isolation level, TLS and SASL. 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
-