Kafka
Since Camel 2.13
Both producer and consumer are supported
The Kafka component is used for communicating with Apache Kafka message broker.
Maven users will need to add the following
dependency to their pom.xml
for this component.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-kafka</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Kafka component supports 117 options, which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
|
Sets additional properties for either kafka consumer or kafka producer in case they can’t be set directly on the camel configurations (e.g.: new Kafka properties that are not reflected yet in Camel configurations), the properties have to be prefixed with additionalProperties.., e.g.: additionalProperties.transactional.id=12345&additionalProperties.schema.registry.url=http://localhost:8811/avro. If the properties are set in the application.properties file, they must be prefixed with camel.component.kafka.additional-properties and the property enclosed in square brackets, like this example: camel.component.kafka.additional-propertiesdelivery.timeout.ms=15000. |
Map |
||
|
URL of the Kafka brokers to use. The format is host1:port1,host2:port2, and the list can be a subset of brokers or a VIP pointing to a subset of brokers. This option is known as bootstrap.servers in the Kafka documentation. |
String |
||
|
The client id is a user-specified string sent in each request to help trace calls. It should logically identify the application making the request. |
String |
||
|
Allows to pre-configure the Kafka component with common options that the endpoints will reuse. |
KafkaConfiguration |
||
|
To use a custom HeaderFilterStrategy to filter header to and from Camel message. |
HeaderFilterStrategy |
||
|
The maximum amount of time in milliseconds to wait when reconnecting to a broker that has repeatedly failed to connect. If provided, the backoff per host will increase exponentially for each consecutive connection failure, up to this maximum. After calculating the backoff increase, 20% random jitter is added to avoid connection storms. |
1000 |
Integer |
|
|
The maximum amount of time in milliseconds to wait when retrying a request to the broker that has repeatedly failed. If provided, the backoff per client will increase exponentially for each failed request, up to this maximum. To prevent all clients from being synchronized upon retry, a randomized jitter with a factor of 0.2 will be applied to the backoff, resulting in the backoff falling within a range between 20% below and 20% above the computed value. If retry.backoff.ms is set to be higher than retry.backoff.max.ms, then retry.backoff.max.ms will be used as a constant backoff from the beginning without any exponential increase. |
1000 |
Integer |
|
|
The amount of time to wait before attempting to retry a failed request to a given topic partition. This avoids repeatedly sending requests in a tight loop under some failure scenarios. This value is the initial backoff value and will increase exponentially for each failed request, up to the retry.backoff.max.ms value. |
100 |
Integer |
|
|
Timeout in milliseconds to wait gracefully for the consumer or producer to shut down and terminate its worker threads. |
30000 |
int |
|
|
Whether to allow doing manual commits via KafkaManualCommit. If this option is enabled then an instance of KafkaManualCommit is stored on the Exchange message header, which allows end users to access this API and perform manual offset commits via the Kafka consumer. |
false |
boolean |
|
|
If true, periodically commit to ZooKeeper the offset of messages already fetched by the consumer. This committed offset will be used when the process fails as the position from which the new consumer will begin. |
true |
boolean |
|
|
The frequency in ms that the consumer offsets are committed to zookeeper. |
5000 |
Integer |
|
|
What to do when there is no initial offset in ZooKeeper or if an offset is out of range: earliest : automatically reset the offset to the earliest offset latest: automatically reset the offset to the latest offset fail: throw exception to the consumer. Enum values:
|
latest |
String |
|
|
Whether to use batching for processing or streaming. The default is false, which uses streaming. In streaming mode, then a single kafka record is processed per Camel exchange in the message body. In batching mode, then Camel groups many kafka records together as a List objects in the message body. The option maxPollRecords is used to define the number of records to group together in batching mode. |
false |
boolean |
|
|
In consumer batching mode, then this option is specifying a time in millis, to trigger batch completion eager when the current batch size has not reached the maximum size defined by maxPollRecords. Notice the trigger is not exact at the given interval, as this can only happen between kafka polls (see pollTimeoutMs option). So for example setting this to 10000, then the trigger happens in the interval 10000 pollTimeoutMs. The default value for pollTimeoutMs is 5000, so this would mean a trigger interval at about every 15 seconds. |
Integer |
||
|
This options controls what happens when a consumer is processing an exchange and it fails. If the option is false then the consumer continues to the next message and processes it. If the option is true then the consumer breaks out. Using the default NoopCommitManager will cause the consumer to not commit the offset so that the message is re-attempted. The consumer should use the KafkaManualCommit to determine the best way to handle the message. Using either the SyncCommitManager or the AsyncCommitManager, the consumer will seek back to the offset of the message that caused a failure, and then re-attempt to process this message. However, this can lead to endless processing of the same message if it’s bound to fail every time, e.g., a poison message. Therefore, it’s recommended to deal with that, for example, by using Camel’s error handler. |
false |
boolean |
|
|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
|
|
Automatically check the CRC32 of the records consumed. This ensures no on-the-wire or on-disk corruption to the messages occurred. This check adds some overhead, so it may be disabled in cases seeking extreme performance. |
true |
Boolean |
|
|
The maximum time, in milliseconds, that the code will wait for a synchronous commit to complete. |
5000 |
Long |
|
|
The configuration controls the maximum amount of time the client will wait for the response of a request. If the response is not received before the timeout elapsed, the client will resend the request if necessary or fail the request if retries are exhausted. |
30000 |
Integer |
|
|
The number of consumers that connect to kafka server. Each consumer is run on a separate thread that retrieves and process the incoming data. |
1 |
int |
|
|
The maximum amount of data the server should return for a fetch request. This is not an absolute maximum, if the first message in the first non-empty partition of the fetch is larger than this value, the message will still be returned to ensure that the consumer can make progress. The maximum message size accepted by the broker is defined via message.max.bytes (broker config) or max.message.bytes (topic config). Note that the consumer performs multiple fetches in parallel. |
52428800 |
Integer |
|
|
The minimum amount of data the server should return for a fetch request. If insufficient data is available, the request will wait for that much data to accumulate before answering the request. |
1 |
Integer |
|
|
The maximum amount of time the server will block before answering the fetch request if there isn’t enough data to immediately satisfy fetch.min.bytes. |
500 |
Integer |
|
|
A string that uniquely identifies the group of consumer processes to which this consumer belongs. By setting the same group id, multiple processes can indicate that they are all part of the same consumer group. This option is required for consumers. |
String |
||
|
A unique identifier of the consumer instance provided by the end user. Only non-empty strings are permitted. If set, the consumer is treated as a static member, which means that only one instance with this ID is allowed in the consumer group at any time. This can be used in combination with a larger session timeout to avoid group rebalances caused by transient unavailability (e.g., process restarts). If not set, the consumer will join the group as a dynamic member, which is the traditional behavior. |
String |
||
|
To use a custom KafkaHeaderDeserializer to deserialize kafka headers values. |
KafkaHeaderDeserializer |
||
|
The expected time between heartbeats to the consumer coordinator when using Kafka’s group management facilities. Heartbeats are used to ensure that the consumer’s session stays active and to facilitate rebalancing when new consumers join or leave the group. The value must be set lower than session.timeout.ms, but typically should be set no higher than 1/3 of that value. It can be adjusted even lower to control the expected time for normal rebalances. |
3000 |
Integer |
|
|
Deserializer class for the key that implements the Deserializer interface. |
org.apache.kafka.common.serialization.StringDeserializer |
String |
|
|
The maximum amount of data per-partition the server will return. The maximum total memory used for a request will be #partitions max.partition.fetch.bytes. This size must be at least as large as the maximum message size the server allows or else it is possible for the producer to send messages larger than the consumer can fetch. If that happens, the consumer can get stuck trying to fetch a large message on a certain partition. |
1048576 |
Integer |
|
|
The maximum delay between invocations of poll() when using consumer group management. This places an upper bound on the amount of time that the consumer can be idle before fetching more records. If poll() is not called before expiration of this timeout, then the consumer is considered failed, and the group will re-balance to reassign the partitions to another member. |
Integer |
||
|
The maximum number of records returned in a single call to poll(). |
500 |
Integer |
|
|
The offset repository to use to locally store the offset of each partition of the topic. Defining one will disable the autocommit. |
StateRepository |
||
|
The class name of the partition assignment strategy that the client will use to distribute partition ownership amongst consumer instances when group management is used. |
org.apache.kafka.clients.consumer.RangeAssignor |
String |
|
|
What to do if kafka threw an exception while polling for new messages. Will by default use the value from the component configuration unless an explicit value has been configured on the endpoint level. DISCARD will discard the message and continue to poll the next message. ERROR_HANDLER will use Camel’s error handler to process the exception, and afterwards continue to poll the next message. RECONNECT will re-connect the consumer and try polling the message again. RETRY will let the consumer retry poll the same message again. STOP will stop the consumer (it has to be manually started/restarted if the consumer should be able to consume messages again). Enum values:
|
ERROR_HANDLER |
PollOnError |
|
|
The timeout used when polling the KafkaConsumer. |
5000 |
Long |
|
|
Whether to eager validate that broker host:port is valid and can be DNS resolved to known host during starting this consumer. If the validation fails, then an exception is thrown, which makes Camel fail fast. Disabling this will postpone the validation after the consumer is started, and Camel will keep re-connecting in case of validation or DNS resolution error. |
true |
boolean |
|
|
Set if KafkaConsumer should read from the beginning or the end on startup: SeekPolicy.BEGINNING: read from the beginning. SeekPolicy.END: read from the end. Enum values:
|
SeekPolicy |
||
|
The timeout used to detect failures when using Kafka’s group management facilities. |
45000 |
Integer |
|
|
This enables the use of a specific Avro reader for use with the in multiple Schema registries documentation with Avro Deserializers implementation. This option is only available externally (not standard Apache Kafka). |
false |
boolean |
|
|
Whether the topic is a pattern (regular expression). This can be used to subscribe to dynamic number of topics matching the pattern. |
false |
boolean |
|
|
Deserializer class for value that implements the Deserializer interface. |
org.apache.kafka.common.serialization.StringDeserializer |
String |
|
|
The delay in millis seconds to wait before trying again to create the kafka consumer (kafka-client). |
5000 |
long |
|
|
Maximum attempts to create the kafka consumer (kafka-client), before eventually giving up and failing. Error during creating the consumer may be fatal due to invalid configuration and as such recovery is not possible. However, one part of the validation is DNS resolution of the bootstrap broker hostnames. This may be a temporary networking problem, and could potentially be recoverable. While other errors are fatal, such as some invalid kafka configurations. Unfortunately, kafka-client does not separate this kind of errors. Camel will by default retry forever, and therefore never give up. If you want to give up after many attempts then set this option and Camel will then when giving up terminate the consumer. To try again, you can manually restart the consumer by stopping, and starting the route. |
int |
||
|
Controls how to read messages written transactionally. If set to read_committed, consumer.poll() will only return transactional messages which have been committed. If set to read_uncommitted (the default), consumer.poll() will return all messages, even transactional messages which have been aborted. Non-transactional messages will be returned unconditionally in either mode. Messages will always be returned in offset order. Hence, in read_committed mode, consumer.poll() will only return messages up to the last stable offset (LSO), which is the one less than the offset of the first open transaction. In particular, any messages appearing after messages belonging to ongoing transactions will be withheld until the relevant transaction has been completed. As a result, read_committed consumers will not be able to read up to the high watermark when there are in flight transactions. Further, when in read_committed the seekToEnd method will return the LSO. Enum values:
|
read_uncommitted |
String |
|
|
Autowired Factory to use for creating KafkaManualCommit instances. This allows to plugin a custom factory to create custom KafkaManualCommit instances in case special logic is needed when doing manual commits that deviates from the default implementation that comes out of the box. |
KafkaManualCommitFactory |
||
|
Autowired To use a custom strategy with the consumer to control how to handle exceptions thrown from the Kafka broker while pooling messages. |
PollExceptionStrategy |
||
|
The delay in millis seconds to wait before trying again to subscribe to the kafka broker. |
5000 |
long |
|
|
Maximum number the kafka consumer will attempt to subscribe to the kafka broker, before eventually giving up and failing. Error during subscribing the consumer to the kafka topic could be temporary errors due to network issues, and could potentially be recoverable. Camel will by default retry forever, and therefore never give up. If you want to give up after many attempts, then set this option and Camel will then when giving up terminate the consumer. You can manually restart the consumer by stopping and starting the route, to try again. |
int |
||
|
If this feature is enabled and a single element of a batch is an Exchange or Message, the producer will generate individual kafka header values for it by using the batch Message to determine the values. Normal behavior consists of always using the same header values (which are determined by the parent Exchange which contains the Iterable or Iterator). |
false |
boolean |
|
|
The total bytes of memory the producer can use to buffer records waiting to be sent to the server. If records are sent faster than they can be delivered to the server, the producer will either block or throw an exception based on the preference specified by block.on.buffer.full.This setting should correspond roughly to the total memory the producer will use, but is not a hard bound since not all memory the producer uses is used for buffering. Some additional memory will be used for compression (if compression is enabled) as well as for maintaining in-flight requests. |
33554432 |
Integer |
|
|
This parameter allows you to specify the compression codec for all data generated by this producer. Valid values are none, gzip, snappy, lz4 and zstd. Enum values:
|
none |
String |
|
|
Close idle connections after the number of milliseconds specified by this config. |
540000 |
Integer |
|
|
An upper bound on the time to report success or failure after a call to send() returns. This limits the total time that a record will be delayed prior to sending, the time to await acknowledgement from the broker (if expected), and the time allowed for retriable send failures. |
120000 |
Integer |
|
|
When set to 'true', the producer will ensure that exactly one copy of each message is written in the stream. If 'false', producer retries due to broker failures, etc., may write duplicates of the retried message in the stream. Note that enabling idempotence requires max.in.flight.requests.per.connection to be less than or equal to 5 (with message ordering preserved for any allowable value), retries to be greater than 0, and acks must be 'all'. Idempotence is enabled by default if no conflicting configurations are set. If conflicting configurations are set and idempotence is not explicitly enabled, idempotence is disabled. If idempotence is explicitly enabled and conflicting configurations are set, a ConfigException is thrown. |
true |
boolean |
|
|
To use a custom KafkaHeaderSerializer to serialize kafka headers values. |
KafkaHeaderSerializer |
||
|
The record key (or null if no key is specified). If this option has been configured then it take precedence over header KafkaConstants#KEY. |
String |
||
|
The serializer class for keys (defaults to the same as for messages if nothing is given). |
org.apache.kafka.common.serialization.StringSerializer |
String |
|
|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
|
|
The producer groups together any records that arrive in between request transmissions into a single, batched, request. Normally, this occurs only under load when records arrive faster than they can be sent out. However, in some circumstances, the client may want to reduce the number of requests even under a moderate load. This setting achieves this by adding a small amount of artificial delay. That is, rather than immediately sending out a record, the producer will wait for up to the given delay to allow other records to be sent so that they can be batched together. This can be thought of as analogous to Nagle’s algorithm in TCP. This setting gives the upper bound on the delay for batching: once we get batch.size worth of records for a partition, it will be sent immediately regardless of this setting, however, if we have fewer than this many bytes accumulated for this partition, we will 'linger' for the specified time waiting for more records to show up. This setting defaults to 0 (i.e., no delay). Setting linger.ms=5, for example, would have the effect of reducing the number of requests sent but would add up to 5ms of latency to records sent in the absence of load. |
0 |
Integer |
|
|
The configuration controls how long the KafkaProducer’s send(), partitionsFor(), initTransactions(), sendOffsetsToTransaction(), commitTransaction() and abortTransaction() methods will block. For send() this timeout bounds the total time waiting for both metadata fetch and buffer allocation (blocking in the user-supplied serializers or partitioner is not counted against this timeout). For partitionsFor() this time out bounds the time spent waiting for metadata if it is unavailable. The transaction-related methods always block, but may time out if the transaction coordinator could not be discovered or did not respond within the timeout. |
60000 |
Integer |
|
|
The maximum number of unacknowledged requests the client will send on a single connection before blocking. Note that if this setting is set to be greater than 1 and there are failed sends, there is a risk of message re-ordering due to retries (i.e., if retries are enabled). |
5 |
Integer |
|
|
The maximum size of a request. This is also effectively a cap on the maximum record size. Note that the server has its own cap on record size which may be different from this. This setting will limit the number of record batches the producer will send in a single request to avoid sending huge requests. |
1048576 |
Integer |
|
|
The period of time in milliseconds after which we force a refresh of metadata even if we haven’t seen any partition leadership changes to proactively discover any new brokers or partitions. |
300000 |
Integer |
|
|
A list of classes to use as metrics reporters. Implementing the MetricReporter interface allows plugging in classes that will be notified of new metric creation. The JmxReporter is always included to register JMX statistics. |
String |
||
|
The window of time a metrics sample is computed over. |
30000 |
Integer |
|
|
The number of samples maintained to compute metrics. |
2 |
Integer |
|
|
The partitioner class for partitioning messages amongst sub-topics. The default partitioner is based on the hash of the key. |
String |
||
|
Whether the message keys should be ignored when computing the partition. This setting has effect only when partitioner is not set. |
false |
boolean |
|
|
The partition to which the record will be sent (or null if no partition was specified). If this option has been configured then it take precedence over header KafkaConstants#PARTITION_KEY. |
Integer |
||
|
The producer will attempt to batch records together into fewer requests whenever multiple records are being sent to the same partition. This helps performance on both the client and the server. This configuration controls the default batch size in bytes. No attempt will be made to batch records larger than this size. Requests sent to brokers will contain multiple batches, one for each partition with data available to be sent. A small batch size will make batching less common and may reduce throughput (a batch size of zero will disable batching entirely). A very large batch size may use memory a bit more wastefully as we will always allocate a buffer of the specified batch size in anticipation of additional records. |
16384 |
Integer |
|
|
The maximum number of unsent messages that can be queued up the producer when using async mode before either the producer must be blocked or data must be dropped. |
10000 |
Integer |
|
|
The size of the TCP receive buffer (SO_RCVBUF) to use when reading data. |
65536 |
Integer |
|
|
The amount of time to wait before attempting to reconnect to a given host. This avoids repeatedly connecting to a host in a tight loop. This backoff applies to all requests sent by the consumer to the broker. |
50 |
Integer |
|
|
The number of acknowledgments the producer requires the leader to have received before considering a request complete. This controls the durability of records that are sent. The following settings are allowed: acks=0 If set to zero, then the producer will not wait for any acknowledgment from the server at all. The record will be immediately added to the socket buffer and considered sent. No guarantee can be made that the server has received the record in this case, and the retry configuration will not take effect (as the client won’t generally know of any failures). The offset given back for each record will always be set to -1. acks=1 This will mean the leader will write the record to its local log but will respond without awaiting full acknowledgment from all followers. In this case should the leader fail immediately after acknowledging the record, but before the followers have replicated it, then the record will be lost. acks=all This means the leader will wait for the full set of in-sync replicas to acknowledge the record. This guarantees that the record will not be lost as long as at least one in-sync replica remains alive. This is the strongest available guarantee. This is equivalent to the acks=-1 setting. Note that enabling idempotence requires this config value to be 'all'. If conflicting configurations are set and idempotence is not explicitly enabled, idempotence is disabled. Enum values:
|
all |
String |
|
|
The amount of time the broker will wait trying to meet the request.required.acks requirement before sending back an error to the client. |
30000 |
Integer |
|
|
Setting a value greater than zero will cause the client to resend any record that has failed to be sent due to a potentially transient error. Note that this retry is no different from if the client re-sending the record upon receiving the error. Produce requests will be failed before the number of retries has been exhausted if the timeout configured by delivery.timeout.ms expires first before successful acknowledgement. Users should generally prefer to leave this config unset and instead use delivery.timeout.ms to control retry behavior. Enabling idempotence requires this config value to be greater than 0. If conflicting configurations are set and idempotence is not explicitly enabled, idempotence is disabled. Allowing retries while setting enable.idempotence to false and max.in.flight.requests.per.connection to 1 will potentially change the ordering of records, because if two batches are sent to a single partition, and the first fails and is retried but the second succeeds; then the records in the second batch may appear first. |
Integer |
||
|
Socket write buffer size. |
131072 |
Integer |
|
|
Sets whether sending to kafka should send the message body as a single record, or use a java.util.Iterator to send multiple records to kafka (if the message body can be iterated). |
true |
boolean |
|
|
The serializer class for messages. |
org.apache.kafka.common.serialization.StringSerializer |
String |
|
|
To use a custom worker pool for continue routing Exchange after kafka server has acknowledged the message that was sent to it from KafkaProducer using asynchronous non-blocking processing. If using this option, then you must handle the lifecycle of the thread pool to shut the pool down when no longer needed. |
ExecutorService |
||
|
Number of core threads for the worker pool for continue routing Exchange after kafka server has acknowledged the message that was sent to it from KafkaProducer using asynchronous non-blocking processing. |
10 |
Integer |
|
|
Maximum number of threads for the worker pool for continue routing Exchange after kafka server has acknowledged the message that was sent to it from KafkaProducer using asynchronous non-blocking processing. |
20 |
Integer |
|
|
Whether the producer should store the RecordMetadata results from sending to Kafka. The results are stored in a List containing the RecordMetadata metadata’s. The list is stored on a header with the key KafkaConstants#KAFKA_RECORDMETA. |
false |
boolean |
|
|
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
boolean |
|
|
Autowired Factory to use for creating org.apache.kafka.clients.consumer.KafkaConsumer and org.apache.kafka.clients.producer.KafkaProducer instances. This allows configuring a custom factory to create instances with logic that extends the vanilla Kafka clients. |
KafkaClientFactory |
||
|
Sets whether synchronous processing should be strictly used. |
false |
boolean |
|
|
Used for enabling or disabling all consumer based health checks from this component. |
true |
boolean |
|
|
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. |
true |
boolean |
|
|
Sets interceptors for producer or consumers. Producer interceptors have to be classes implementing org.apache.kafka.clients.producer.ProducerInterceptor Consumer interceptors have to be classes implementing org.apache.kafka.clients.consumer.ConsumerInterceptor Note that if you use Producer interceptor on a consumer it will throw a class cast exception in runtime. |
String |
||
|
URL of the schema registry servers to use. The format is host1:port1,host2:port2. This is known as schema.registry.url in multiple Schema registries documentation. This option is only available externally (not standard Apache Kafka). |
String |
||
|
Login thread sleep time between refresh attempts. |
60000 |
Integer |
|
|
Location of the kerberos config file. |
String |
||
|
Kerberos kinit command path. Default is /usr/bin/kinit. |
/usr/bin/kinit |
String |
|
|
A list of rules for mapping from principal names to short names (typically operating system usernames). The rules are evaluated in order, and the first rule that matches a principal name is used to map it to a short name. Any later rules in the list are ignored. By default, principal names of the form {username}/{hostname}{REALM} are mapped to {username}. For more details on the format, please see the Security Authorization and ACLs documentation (at the Apache Kafka project website). Multiple values can be separated by comma. |
DEFAULT |
String |
|
|
Percentage of random jitter added to the renewal time. |
0.05 |
Double |
|
|
Login thread will sleep until the specified window factor of time from last refresh to ticket’s expiry has been reached, at which time it will try to renew the ticket. |
0.8 |
Double |
|
|
Expose the kafka sasl.jaas.config parameter Example: org.apache.kafka.common.security.plain.PlainLoginModule required username=USERNAME password=PASSWORD;. |
String |
||
|
The Kerberos principal name that Kafka runs as. This can be defined either in Kafka’s JAAS config or in Kafka’s config. |
String |
||
|
The Simple Authentication and Security Layer (SASL) Mechanism used. For the valid values see http://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml. |
GSSAPI |
String |
|
|
Protocol used to communicate with brokers. SASL_PLAINTEXT, PLAINTEXT, SASL_SSL and SSL are supported. |
PLAINTEXT |
String |
|
|
A list of cipher suites. This is a named combination of authentication, encryption, MAC and key exchange algorithm used to negotiate the security settings for a network connection using TLS or SSL network protocol. By default, all the available cipher suites are supported. |
String |
||
|
SSL configuration using a Camel SSLContextParameters object. If configured, it’s applied before the other SSL endpoint parameters. NOTE: Kafka only supports loading keystore from file locations, so prefix the location with file: in the KeyStoreParameters.resource option. |
SSLContextParameters |
||
|
The list of protocols enabled for SSL connections. The default is TLSv1.2,TLSv1.3 when running with Java 11 or newer, TLSv1.2 otherwise. With the default value for Java 11, clients and servers will prefer TLSv1.3 if both support it and fallback to TLSv1.2 otherwise (assuming both support at least TLSv1.2). This default should be fine for most cases. Also see the config documentation for SslProtocol. |
String |
||
|
The endpoint identification algorithm to validate server hostname using server certificate. Use none or false to disable server hostname verification. |
https |
String |
|
|
The algorithm used by key manager factory for SSL connections. Default value is the key manager factory algorithm configured for the Java Virtual Machine. |
SunX509 |
String |
|
|
The password of the private key in the key store file or the PEM key specified in sslKeystoreKey. This is required for clients only if two-way authentication is configured. |
String |
||
|
The location of the key store file. This is optional for the client and can be used for two-way authentication for the client. |
String |
||
|
The store password for the key store file. This is optional for the client and only needed if sslKeystoreLocation is configured. Key store password is not supported for PEM format. |
String |
||
|
The file format of the key store file. This is optional for the client. The default value is JKS. |
JKS |
String |
|
|
The SSL protocol used to generate the SSLContext. The default is TLSv1.3 when running with Java 11 or newer, TLSv1.2 otherwise. This value should be fine for most use cases. Allowed values in recent JVMs are TLSv1.2 and TLSv1.3. TLS, TLSv1.1, SSL, SSLv2 and SSLv3 may be supported in older JVMs, but their usage is discouraged due to known security vulnerabilities. With the default value for this config and sslEnabledProtocols, clients will downgrade to TLSv1.2 if the server does not support TLSv1.3. If this config is set to TLSv1.2, clients will not use TLSv1.3 even if it is one of the values in sslEnabledProtocols and the server only supports TLSv1.3. |
String |
||
|
The name of the security provider used for SSL connections. Default value is the default security provider of the JVM. |
String |
||
|
The algorithm used by trust manager factory for SSL connections. Default value is the trust manager factory algorithm configured for the Java Virtual Machine. |
PKIX |
String |
|
|
The location of the trust store file. |
String |
||
|
The password for the trust store file. If a password is not set, trust store file configured will still be used, but integrity checking is disabled. Trust store password is not supported for PEM format. |
String |
||
|
The file format of the trust store file. The default value is JKS. |
JKS |
String |
|
|
Enable usage of global SSL context parameters. |
false |
boolean |
Endpoint Options
The Kafka endpoint is configured using URI syntax:
kafka:topic
With the following path and query parameters:
Path Parameters (1 parameters)
| Name | Description | Default | Type |
|---|---|---|---|
|
Required Name of the topic to use. On the consumer you can use comma to separate multiple topics. A producer can only send a message to a single topic. |
String |
Query Parameters (109 parameters)
| Name | Description | Default | Type |
|---|---|---|---|
|
Sets additional properties for either kafka consumer or kafka producer in case they can’t be set directly on the camel configurations (e.g.: new Kafka properties that are not reflected yet in Camel configurations), the properties have to be prefixed with additionalProperties.., e.g.: additionalProperties.transactional.id=12345&additionalProperties.schema.registry.url=http://localhost:8811/avro. If the properties are set in the application.properties file, they must be prefixed with camel.component.kafka.additional-properties and the property enclosed in square brackets, like this example: camel.component.kafka.additional-propertiesdelivery.timeout.ms=15000. |
Map |
||
|
URL of the Kafka brokers to use. The format is host1:port1,host2:port2, and the list can be a subset of brokers or a VIP pointing to a subset of brokers. This option is known as bootstrap.servers in the Kafka documentation. |
String |
||
|
The client id is a user-specified string sent in each request to help trace calls. It should logically identify the application making the request. |
String |
||
|
To use a custom HeaderFilterStrategy to filter header to and from Camel message. |
HeaderFilterStrategy |
||
|
The maximum amount of time in milliseconds to wait when reconnecting to a broker that has repeatedly failed to connect. If provided, the backoff per host will increase exponentially for each consecutive connection failure, up to this maximum. After calculating the backoff increase, 20% random jitter is added to avoid connection storms. |
1000 |
Integer |
|
|
The maximum amount of time in milliseconds to wait when retrying a request to the broker that has repeatedly failed. If provided, the backoff per client will increase exponentially for each failed request, up to this maximum. To prevent all clients from being synchronized upon retry, a randomized jitter with a factor of 0.2 will be applied to the backoff, resulting in the backoff falling within a range between 20% below and 20% above the computed value. If retry.backoff.ms is set to be higher than retry.backoff.max.ms, then retry.backoff.max.ms will be used as a constant backoff from the beginning without any exponential increase. |
1000 |
Integer |
|
|
The amount of time to wait before attempting to retry a failed request to a given topic partition. This avoids repeatedly sending requests in a tight loop under some failure scenarios. This value is the initial backoff value and will increase exponentially for each failed request, up to the retry.backoff.max.ms value. |
100 |
Integer |
|
|
Timeout in milliseconds to wait gracefully for the consumer or producer to shut down and terminate its worker threads. |
30000 |
int |
|
|
Whether to allow doing manual commits via KafkaManualCommit. If this option is enabled then an instance of KafkaManualCommit is stored on the Exchange message header, which allows end users to access this API and perform manual offset commits via the Kafka consumer. |
false |
boolean |
|
|
If true, periodically commit to ZooKeeper the offset of messages already fetched by the consumer. This committed offset will be used when the process fails as the position from which the new consumer will begin. |
true |
boolean |
|
|
The frequency in ms that the consumer offsets are committed to zookeeper. |
5000 |
Integer |
|
|
What to do when there is no initial offset in ZooKeeper or if an offset is out of range: earliest : automatically reset the offset to the earliest offset latest: automatically reset the offset to the latest offset fail: throw exception to the consumer. Enum values:
|
latest |
String |
|
|
Whether to use batching for processing or streaming. The default is false, which uses streaming. In streaming mode, then a single kafka record is processed per Camel exchange in the message body. In batching mode, then Camel groups many kafka records together as a List objects in the message body. The option maxPollRecords is used to define the number of records to group together in batching mode. |
false |
boolean |
|
|
In consumer batching mode, then this option is specifying a time in millis, to trigger batch completion eager when the current batch size has not reached the maximum size defined by maxPollRecords. Notice the trigger is not exact at the given interval, as this can only happen between kafka polls (see pollTimeoutMs option). So for example setting this to 10000, then the trigger happens in the interval 10000 pollTimeoutMs. The default value for pollTimeoutMs is 5000, so this would mean a trigger interval at about every 15 seconds. |
Integer |
||
|
This options controls what happens when a consumer is processing an exchange and it fails. If the option is false then the consumer continues to the next message and processes it. If the option is true then the consumer breaks out. Using the default NoopCommitManager will cause the consumer to not commit the offset so that the message is re-attempted. The consumer should use the KafkaManualCommit to determine the best way to handle the message. Using either the SyncCommitManager or the AsyncCommitManager, the consumer will seek back to the offset of the message that caused a failure, and then re-attempt to process this message. However, this can lead to endless processing of the same message if it’s bound to fail every time, e.g., a poison message. Therefore, it’s recommended to deal with that, for example, by using Camel’s error handler. |
false |
boolean |
|
|
Automatically check the CRC32 of the records consumed. This ensures no on-the-wire or on-disk corruption to the messages occurred. This check adds some overhead, so it may be disabled in cases seeking extreme performance. |
true |
Boolean |
|
|
The maximum time, in milliseconds, that the code will wait for a synchronous commit to complete. |
5000 |
Long |
|
|
The configuration controls the maximum amount of time the client will wait for the response of a request. If the response is not received before the timeout elapsed, the client will resend the request if necessary or fail the request if retries are exhausted. |
30000 |
Integer |
|
|
The number of consumers that connect to kafka server. Each consumer is run on a separate thread that retrieves and process the incoming data. |
1 |
int |
|
|
The maximum amount of data the server should return for a fetch request. This is not an absolute maximum, if the first message in the first non-empty partition of the fetch is larger than this value, the message will still be returned to ensure that the consumer can make progress. The maximum message size accepted by the broker is defined via message.max.bytes (broker config) or max.message.bytes (topic config). Note that the consumer performs multiple fetches in parallel. |
52428800 |
Integer |
|
|
The minimum amount of data the server should return for a fetch request. If insufficient data is available, the request will wait for that much data to accumulate before answering the request. |
1 |
Integer |
|
|
The maximum amount of time the server will block before answering the fetch request if there isn’t enough data to immediately satisfy fetch.min.bytes. |
500 |
Integer |
|
|
A string that uniquely identifies the group of consumer processes to which this consumer belongs. By setting the same group id, multiple processes can indicate that they are all part of the same consumer group. This option is required for consumers. |
String |
||
|
A unique identifier of the consumer instance provided by the end user. Only non-empty strings are permitted. If set, the consumer is treated as a static member, which means that only one instance with this ID is allowed in the consumer group at any time. This can be used in combination with a larger session timeout to avoid group rebalances caused by transient unavailability (e.g., process restarts). If not set, the consumer will join the group as a dynamic member, which is the traditional behavior. |
String |
||
|
To use a custom KafkaHeaderDeserializer to deserialize kafka headers values. |
KafkaHeaderDeserializer |
||
|
The expected time between heartbeats to the consumer coordinator when using Kafka’s group management facilities. Heartbeats are used to ensure that the consumer’s session stays active and to facilitate rebalancing when new consumers join or leave the group. The value must be set lower than session.timeout.ms, but typically should be set no higher than 1/3 of that value. It can be adjusted even lower to control the expected time for normal rebalances. |
3000 |
Integer |
|
|
Deserializer class for the key that implements the Deserializer interface. |
org.apache.kafka.common.serialization.StringDeserializer |
String |
|
|
The maximum amount of data per-partition the server will return. The maximum total memory used for a request will be #partitions max.partition.fetch.bytes. This size must be at least as large as the maximum message size the server allows or else it is possible for the producer to send messages larger than the consumer can fetch. If that happens, the consumer can get stuck trying to fetch a large message on a certain partition. |
1048576 |
Integer |
|
|
The maximum delay between invocations of poll() when using consumer group management. This places an upper bound on the amount of time that the consumer can be idle before fetching more records. If poll() is not called before expiration of this timeout, then the consumer is considered failed, and the group will re-balance to reassign the partitions to another member. |
Integer |
||
|
The maximum number of records returned in a single call to poll(). |
500 |
Integer |
|
|
The offset repository to use to locally store the offset of each partition of the topic. Defining one will disable the autocommit. |
StateRepository |
||
|
The class name of the partition assignment strategy that the client will use to distribute partition ownership amongst consumer instances when group management is used. |
org.apache.kafka.clients.consumer.RangeAssignor |
String |
|
|
What to do if kafka threw an exception while polling for new messages. Will by default use the value from the component configuration unless an explicit value has been configured on the endpoint level. DISCARD will discard the message and continue to poll the next message. ERROR_HANDLER will use Camel’s error handler to process the exception, and afterwards continue to poll the next message. RECONNECT will re-connect the consumer and try polling the message again. RETRY will let the consumer retry poll the same message again. STOP will stop the consumer (it has to be manually started/restarted if the consumer should be able to consume messages again). Enum values:
|
ERROR_HANDLER |
PollOnError |
|
|
The timeout used when polling the KafkaConsumer. |
5000 |
Long |
|
|
Whether to eager validate that broker host:port is valid and can be DNS resolved to known host during starting this consumer. If the validation fails, then an exception is thrown, which makes Camel fail fast. Disabling this will postpone the validation after the consumer is started, and Camel will keep re-connecting in case of validation or DNS resolution error. |
true |
boolean |
|
|
Set if KafkaConsumer should read from the beginning or the end on startup: SeekPolicy.BEGINNING: read from the beginning. SeekPolicy.END: read from the end. Enum values:
|
SeekPolicy |
||
|
The timeout used to detect failures when using Kafka’s group management facilities. |
45000 |
Integer |
|
|
This enables the use of a specific Avro reader for use with the in multiple Schema registries documentation with Avro Deserializers implementation. This option is only available externally (not standard Apache Kafka). |
false |
boolean |
|
|
Whether the topic is a pattern (regular expression). This can be used to subscribe to dynamic number of topics matching the pattern. |
false |
boolean |
|
|
Deserializer class for value that implements the Deserializer interface. |
org.apache.kafka.common.serialization.StringDeserializer |
String |
|
|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
|
|
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
ExceptionHandler |
||
|
Sets the exchange pattern when the consumer creates an exchange. Enum values:
|
ExchangePattern |
||
|
Controls how to read messages written transactionally. If set to read_committed, consumer.poll() will only return transactional messages which have been committed. If set to read_uncommitted (the default), consumer.poll() will return all messages, even transactional messages which have been aborted. Non-transactional messages will be returned unconditionally in either mode. Messages will always be returned in offset order. Hence, in read_committed mode, consumer.poll() will only return messages up to the last stable offset (LSO), which is the one less than the offset of the first open transaction. In particular, any messages appearing after messages belonging to ongoing transactions will be withheld until the relevant transaction has been completed. As a result, read_committed consumers will not be able to read up to the high watermark when there are in flight transactions. Further, when in read_committed the seekToEnd method will return the LSO. Enum values:
|
read_uncommitted |
String |
|
|
Factory to use for creating KafkaManualCommit instances. This allows to plugin a custom factory to create custom KafkaManualCommit instances in case special logic is needed when doing manual commits that deviates from the default implementation that comes out of the box. |
KafkaManualCommitFactory |
||
|
If this feature is enabled and a single element of a batch is an Exchange or Message, the producer will generate individual kafka header values for it by using the batch Message to determine the values. Normal behavior consists of always using the same header values (which are determined by the parent Exchange which contains the Iterable or Iterator). |
false |
boolean |
|
|
The total bytes of memory the producer can use to buffer records waiting to be sent to the server. If records are sent faster than they can be delivered to the server, the producer will either block or throw an exception based on the preference specified by block.on.buffer.full.This setting should correspond roughly to the total memory the producer will use, but is not a hard bound since not all memory the producer uses is used for buffering. Some additional memory will be used for compression (if compression is enabled) as well as for maintaining in-flight requests. |
33554432 |
Integer |
|
|
This parameter allows you to specify the compression codec for all data generated by this producer. Valid values are none, gzip, snappy, lz4 and zstd. Enum values:
|
none |
String |
|
|
Close idle connections after the number of milliseconds specified by this config. |
540000 |
Integer |
|
|
An upper bound on the time to report success or failure after a call to send() returns. This limits the total time that a record will be delayed prior to sending, the time to await acknowledgement from the broker (if expected), and the time allowed for retriable send failures. |
120000 |
Integer |
|
|
When set to 'true', the producer will ensure that exactly one copy of each message is written in the stream. If 'false', producer retries due to broker failures, etc., may write duplicates of the retried message in the stream. Note that enabling idempotence requires max.in.flight.requests.per.connection to be less than or equal to 5 (with message ordering preserved for any allowable value), retries to be greater than 0, and acks must be 'all'. Idempotence is enabled by default if no conflicting configurations are set. If conflicting configurations are set and idempotence is not explicitly enabled, idempotence is disabled. If idempotence is explicitly enabled and conflicting configurations are set, a ConfigException is thrown. |
true |
boolean |
|
|
To use a custom KafkaHeaderSerializer to serialize kafka headers values. |
KafkaHeaderSerializer |
||
|
The record key (or null if no key is specified). If this option has been configured then it take precedence over header KafkaConstants#KEY. |
String |
||
|
The serializer class for keys (defaults to the same as for messages if nothing is given). |
org.apache.kafka.common.serialization.StringSerializer |
String |
|
|
The producer groups together any records that arrive in between request transmissions into a single, batched, request. Normally, this occurs only under load when records arrive faster than they can be sent out. However, in some circumstances, the client may want to reduce the number of requests even under a moderate load. This setting achieves this by adding a small amount of artificial delay. That is, rather than immediately sending out a record, the producer will wait for up to the given delay to allow other records to be sent so that they can be batched together. This can be thought of as analogous to Nagle’s algorithm in TCP. This setting gives the upper bound on the delay for batching: once we get batch.size worth of records for a partition, it will be sent immediately regardless of this setting, however, if we have fewer than this many bytes accumulated for this partition, we will 'linger' for the specified time waiting for more records to show up. This setting defaults to 0 (i.e., no delay). Setting linger.ms=5, for example, would have the effect of reducing the number of requests sent but would add up to 5ms of latency to records sent in the absence of load. |
0 |
Integer |
|
|
The configuration controls how long the KafkaProducer’s send(), partitionsFor(), initTransactions(), sendOffsetsToTransaction(), commitTransaction() and abortTransaction() methods will block. For send() this timeout bounds the total time waiting for both metadata fetch and buffer allocation (blocking in the user-supplied serializers or partitioner is not counted against this timeout). For partitionsFor() this time out bounds the time spent waiting for metadata if it is unavailable. The transaction-related methods always block, but may time out if the transaction coordinator could not be discovered or did not respond within the timeout. |
60000 |
Integer |
|
|
The maximum number of unacknowledged requests the client will send on a single connection before blocking. Note that if this setting is set to be greater than 1 and there are failed sends, there is a risk of message re-ordering due to retries (i.e., if retries are enabled). |
5 |
Integer |
|
|
The maximum size of a request. This is also effectively a cap on the maximum record size. Note that the server has its own cap on record size which may be different from this. This setting will limit the number of record batches the producer will send in a single request to avoid sending huge requests. |
1048576 |
Integer |
|
|
The period of time in milliseconds after which we force a refresh of metadata even if we haven’t seen any partition leadership changes to proactively discover any new brokers or partitions. |
300000 |
Integer |
|
|
A list of classes to use as metrics reporters. Implementing the MetricReporter interface allows plugging in classes that will be notified of new metric creation. The JmxReporter is always included to register JMX statistics. |
String |
||
|
The window of time a metrics sample is computed over. |
30000 |
Integer |
|
|
The number of samples maintained to compute metrics. |
2 |
Integer |
|
|
The partitioner class for partitioning messages amongst sub-topics. The default partitioner is based on the hash of the key. |
String |
||
|
Whether the message keys should be ignored when computing the partition. This setting has effect only when partitioner is not set. |
false |
boolean |
|
|
The partition to which the record will be sent (or null if no partition was specified). If this option has been configured then it take precedence over header KafkaConstants#PARTITION_KEY. |
Integer |
||
|
The producer will attempt to batch records together into fewer requests whenever multiple records are being sent to the same partition. This helps performance on both the client and the server. This configuration controls the default batch size in bytes. No attempt will be made to batch records larger than this size. Requests sent to brokers will contain multiple batches, one for each partition with data available to be sent. A small batch size will make batching less common and may reduce throughput (a batch size of zero will disable batching entirely). A very large batch size may use memory a bit more wastefully as we will always allocate a buffer of the specified batch size in anticipation of additional records. |
16384 |
Integer |
|
|
The maximum number of unsent messages that can be queued up the producer when using async mode before either the producer must be blocked or data must be dropped. |
10000 |
Integer |
|
|
The size of the TCP receive buffer (SO_RCVBUF) to use when reading data. |
65536 |
Integer |
|
|
The amount of time to wait before attempting to reconnect to a given host. This avoids repeatedly connecting to a host in a tight loop. This backoff applies to all requests sent by the consumer to the broker. |
50 |
Integer |
|
|
The number of acknowledgments the producer requires the leader to have received before considering a request complete. This controls the durability of records that are sent. The following settings are allowed: acks=0 If set to zero, then the producer will not wait for any acknowledgment from the server at all. The record will be immediately added to the socket buffer and considered sent. No guarantee can be made that the server has received the record in this case, and the retry configuration will not take effect (as the client won’t generally know of any failures). The offset given back for each record will always be set to -1. acks=1 This will mean the leader will write the record to its local log but will respond without awaiting full acknowledgment from all followers. In this case should the leader fail immediately after acknowledging the record, but before the followers have replicated it, then the record will be lost. acks=all This means the leader will wait for the full set of in-sync replicas to acknowledge the record. This guarantees that the record will not be lost as long as at least one in-sync replica remains alive. This is the strongest available guarantee. This is equivalent to the acks=-1 setting. Note that enabling idempotence requires this config value to be 'all'. If conflicting configurations are set and idempotence is not explicitly enabled, idempotence is disabled. Enum values:
|
all |
String |
|
|
The amount of time the broker will wait trying to meet the request.required.acks requirement before sending back an error to the client. |
30000 |
Integer |
|
|
Setting a value greater than zero will cause the client to resend any record that has failed to be sent due to a potentially transient error. Note that this retry is no different from if the client re-sending the record upon receiving the error. Produce requests will be failed before the number of retries has been exhausted if the timeout configured by delivery.timeout.ms expires first before successful acknowledgement. Users should generally prefer to leave this config unset and instead use delivery.timeout.ms to control retry behavior. Enabling idempotence requires this config value to be greater than 0. If conflicting configurations are set and idempotence is not explicitly enabled, idempotence is disabled. Allowing retries while setting enable.idempotence to false and max.in.flight.requests.per.connection to 1 will potentially change the ordering of records, because if two batches are sent to a single partition, and the first fails and is retried but the second succeeds; then the records in the second batch may appear first. |
Integer |
||
|
Socket write buffer size. |
131072 |
Integer |
|
|
Sets whether sending to kafka should send the message body as a single record, or use a java.util.Iterator to send multiple records to kafka (if the message body can be iterated). |
true |
boolean |
|
|
The serializer class for messages. |
org.apache.kafka.common.serialization.StringSerializer |
String |
|
|
To use a custom worker pool for continue routing Exchange after kafka server has acknowledged the message that was sent to it from KafkaProducer using asynchronous non-blocking processing. If using this option, then you must handle the lifecycle of the thread pool to shut the pool down when no longer needed. |
ExecutorService |
||
|
Number of core threads for the worker pool for continue routing Exchange after kafka server has acknowledged the message that was sent to it from KafkaProducer using asynchronous non-blocking processing. |
10 |
Integer |
|
|
Maximum number of threads for the worker pool for continue routing Exchange after kafka server has acknowledged the message that was sent to it from KafkaProducer using asynchronous non-blocking processing. |
20 |
Integer |
|
|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
|
|
Whether the producer should store the RecordMetadata results from sending to Kafka. The results are stored in a List containing the RecordMetadata metadata’s. The list is stored on a header with the key KafkaConstants#KAFKA_RECORDMETA. |
false |
boolean |
|
|
Factory to use for creating org.apache.kafka.clients.consumer.KafkaConsumer and org.apache.kafka.clients.producer.KafkaProducer instances. This allows to configure a custom factory to create instances with logic that extends the vanilla Kafka clients. |
KafkaClientFactory |
||
|
Sets whether synchronous processing should be strictly used. |
false |
boolean |
|
|
Sets interceptors for producer or consumers. Producer interceptors have to be classes implementing org.apache.kafka.clients.producer.ProducerInterceptor Consumer interceptors have to be classes implementing org.apache.kafka.clients.consumer.ConsumerInterceptor Note that if you use Producer interceptor on a consumer it will throw a class cast exception in runtime. |
String |
||
|
URL of the schema registry servers to use. The format is host1:port1,host2:port2. This is known as schema.registry.url in multiple Schema registries documentation. This option is only available externally (not standard Apache Kafka). |
String |
||
|
Login thread sleep time between refresh attempts. |
60000 |
Integer |
|
|
Location of the kerberos config file. |
String |
||
|
Kerberos kinit command path. Default is /usr/bin/kinit. |
/usr/bin/kinit |
String |
|
|
A list of rules for mapping from principal names to short names (typically operating system usernames). The rules are evaluated in order, and the first rule that matches a principal name is used to map it to a short name. Any later rules in the list are ignored. By default, principal names of the form {username}/{hostname}{REALM} are mapped to {username}. For more details on the format, please see the Security Authorization and ACLs documentation (at the Apache Kafka project website). Multiple values can be separated by comma. |
DEFAULT |
String |
|
|
Percentage of random jitter added to the renewal time. |
0.05 |
Double |
|
|
Login thread will sleep until the specified window factor of time from last refresh to ticket’s expiry has been reached, at which time it will try to renew the ticket. |
0.8 |
Double |
|
|
Expose the kafka sasl.jaas.config parameter Example: org.apache.kafka.common.security.plain.PlainLoginModule required username=USERNAME password=PASSWORD;. |
String |
||
|
The Kerberos principal name that Kafka runs as. This can be defined either in Kafka’s JAAS config or in Kafka’s config. |
String |
||
|
The Simple Authentication and Security Layer (SASL) Mechanism used. For the valid values see http://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml. |
GSSAPI |
String |
|
|
Protocol used to communicate with brokers. SASL_PLAINTEXT, PLAINTEXT, SASL_SSL and SSL are supported. |
PLAINTEXT |
String |
|
|
A list of cipher suites. This is a named combination of authentication, encryption, MAC and key exchange algorithm used to negotiate the security settings for a network connection using TLS or SSL network protocol. By default, all the available cipher suites are supported. |
String |
||
|
SSL configuration using a Camel SSLContextParameters object. If configured, it’s applied before the other SSL endpoint parameters. NOTE: Kafka only supports loading keystore from file locations, so prefix the location with file: in the KeyStoreParameters.resource option. |
SSLContextParameters |
||
|
The list of protocols enabled for SSL connections. The default is TLSv1.2,TLSv1.3 when running with Java 11 or newer, TLSv1.2 otherwise. With the default value for Java 11, clients and servers will prefer TLSv1.3 if both support it and fallback to TLSv1.2 otherwise (assuming both support at least TLSv1.2). This default should be fine for most cases. Also see the config documentation for SslProtocol. |
String |
||
|
The endpoint identification algorithm to validate server hostname using server certificate. Use none or false to disable server hostname verification. |
https |
String |
|
|
The algorithm used by key manager factory for SSL connections. Default value is the key manager factory algorithm configured for the Java Virtual Machine. |
SunX509 |
String |
|
|
The password of the private key in the key store file or the PEM key specified in sslKeystoreKey. This is required for clients only if two-way authentication is configured. |
String |
||
|
The location of the key store file. This is optional for the client and can be used for two-way authentication for the client. |
String |
||
|
The store password for the key store file. This is optional for the client and only needed if sslKeystoreLocation is configured. Key store password is not supported for PEM format. |
String |
||
|
The file format of the key store file. This is optional for the client. The default value is JKS. |
JKS |
String |
|
|
The SSL protocol used to generate the SSLContext. The default is TLSv1.3 when running with Java 11 or newer, TLSv1.2 otherwise. This value should be fine for most use cases. Allowed values in recent JVMs are TLSv1.2 and TLSv1.3. TLS, TLSv1.1, SSL, SSLv2 and SSLv3 may be supported in older JVMs, but their usage is discouraged due to known security vulnerabilities. With the default value for this config and sslEnabledProtocols, clients will downgrade to TLSv1.2 if the server does not support TLSv1.3. If this config is set to TLSv1.2, clients will not use TLSv1.3 even if it is one of the values in sslEnabledProtocols and the server only supports TLSv1.3. |
String |
||
|
The name of the security provider used for SSL connections. Default value is the default security provider of the JVM. |
String |
||
|
The algorithm used by trust manager factory for SSL connections. Default value is the trust manager factory algorithm configured for the Java Virtual Machine. |
PKIX |
String |
|
|
The location of the trust store file. |
String |
||
|
The password for the trust store file. If a password is not set, trust store file configured will still be used, but integrity checking is disabled. Trust store password is not supported for PEM format. |
String |
||
|
The file format of the trust store file. The default value is JKS. |
JKS |
String |
For more information about Producer/Consumer configuration:
Message Headers
The Kafka component supports 13 message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
kafka.PARTITION_KEY (producer) Constant: |
Explicitly specify the partition. |
Integer |
|
|
Constant: |
The partition where the message was stored. |
Integer |
|
|
Constant: |
Required Producer: The key of the message in order to ensure that all related message goes in the same partition. Consumer: The key of the message if configured. |
Object |
|
|
Constant: |
The topic from where the message originated. |
String |
|
kafka.OVERRIDE_TOPIC (producer) Constant: |
The topic to which send the message (override and takes precedence), and the header is not preserved. |
String |
|
|
Constant: |
The offset of the message. |
Long |
|
|
Constant: |
The record headers. |
Headers |
|
kafka.LAST_RECORD_BEFORE_COMMIT (consumer) Constant: |
Whether or not it’s the last record before commit (only available if autoCommitEnable endpoint parameter is false). |
Boolean |
|
kafka.LAST_POLL_RECORD (consumer) Constant: |
Indicates the last record within the current poll request (only available if autoCommitEnable endpoint parameter is false or allowManualCommit is true). |
Boolean |
|
|
Constant: |
The timestamp of the message. |
Long |
|
kafka.OVERRIDE_TIMESTAMP (producer) Constant: |
The ProducerRecord also has an associated timestamp. If the user did provide a timestamp, the producer will stamp the record with the provided timestamp and the header is not preserved. |
Long |
|
|
Constant: |
The metadata (only configured if recordMetadata endpoint parameter is true). |
List |
|
CamelKafkaManualCommit (consumer) Constant: |
Can be used for forcing manual offset commit when using Kafka consumer. |
KafkaManualCommit |
If you want to send a message to a dynamic topic
then use
KafkaConstants.OVERRIDE_TOPIC as it
is used as a one-time header that is not sent
along the message, and actually is removed in
the producer.
Usage
Consumer error handling
While kafka consumer is polling messages from the kafka broker, then errors can happen. This section describes what happens and what you can configure.
The consumer may throw exception when
invoking the Kafka poll API.
For example, if the message cannot be
deserialized due to invalid data,
and many other kinds of errors. Those errors
are in the form of
KafkaException which are either
retriable or not. The exceptions
which can be retried (RetriableException)
will be retried again (with a poll timeout
in between). All other kinds of exceptions
are
handled according to the
pollOnError configuration. This
configuration has the following values:
-
DISCARD will discard the message and continue to poll the next message.
-
ERROR_HANDLER will use Camel’s error handler to process the exception, and afterwards continue to poll the next message.
-
RECONNECT will re-connect the consumer and try to poll the message again.
-
RETRY will let the consumer retry polling the same message again
-
STOP will stop the consumer (it has to be manually started/restarted if the consumer should be able to consume messages again).
The default is ERROR_HANDLER, which will let Camel’s error handler (if any configured) process the caused exception. Afterwards continue to poll the next message. This behavior is similar to the bridgeErrorHandler option that Camel components have.
For advanced control a custom implementation
of org.apache.camel.component.kafka.PollExceptionStrategy
can be configured
on the component level, which allows
controlling which of the strategies to use
for each exception.
Consumer error handling (advanced)
By default, Camel will poll using the
ERROR_HANDLER to process
exceptions.
How Camel handles a message that results in
an exception can be altered using the breakOnFirstError
attribute in the configuration.
Instead of continuing to poll the next
message, Camel will instead commit the
offset so that the message that caused the
exception will be retried.
This is similar to the
RETRY polling strategy
above.
KafkaComponent kafka = new KafkaComponent();
kafka.setBreakOnFirstError(true);
...
camelContext.addComponent("kafka", kafka);
It is recommended that you read the section
below "Using manual commit with Kafka
consumer" to understand how breakOnFirstError
will work based on the
CommitManager that is
configured.
The Kafka idempotent repository
The camel-kafka library provides
a Kafka topic-based idempotent repository.
This repository stores broadcasts all
changes to idempotent state (add/remove) in
a Kafka topic, and populates a local
in-memory cache for each repository’s
process instance through event sourcing.
The topic used must be unique per idempotent
repository instance. The mechanism does not
have any requirements about the number of
topic partitions, as the repository consumes
from all partitions at the same time. It
also does not have any requirements about
the replication factor of the topic.
Each repository instance that uses the
topic, (e.g., typically on different
machines running in parallel) controls its
own consumer group, so in a cluster of 10
Camel processes using the same topic, each
will control its own offset.
On startup, the instance subscribes to the
topic, rewinds the offset to the beginning
and rebuilds the cache to the latest state.
The cache will not be considered warmed up
until one poll of
pollDurationMs in length
returns 0 records. Startup will not be
completed until either the cache has warmed
up, or 30 seconds go by; if the latter
happens, the idempotent repository may be in
an inconsistent state until its consumer
catches up to the end of the topic.
Be mindful of the format of the header used
for the uniqueness check. By default, it
uses Strings as the data types. When using
primitive numeric formats, the header must
be deserialized accordingly. Check the
samples below for examples.
A KafkaIdempotentRepository has
the following properties:
| Property | Default | Description |
|---|---|---|
|
|
Required The name of the Kafka topic to use to broadcast changes. (required) |
|
|
|
Required The |
|
|
|
The groupId to assign to the idempotent consumer. |
|
|
|
|
Whether to sync on startup only, or to continue syncing while Camel is running. |
|
|
|
How many of the most recently used keys should be stored in memory (default 1000). |
|
|
|
The poll duration of the Kafka consumer. The local caches are updated immediately. This value will affect how far behind other peers that update their caches from the topic are relative to the idempotent consumer instance that sent the cache action message. The default value of this is 100 ms. If setting this value explicitly, be aware that there is a tradeoff between the remote cache liveness and the volume of network traffic between this repository’s consumer and the Kafka brokers. The cache warmup process also depends on there being one poll that fetches nothing - this indicates that the stream has been consumed up to the current point. If the poll duration is excessively long for the rate at which messages are sent on the topic, there exists a possibility that the cache cannot be warmed up and will operate in an inconsistent state relative to its peers until it catches up. |
|
|
Sets the
properties that will be used by the
Kafka producer that broadcasts
changes. Overrides |
|
|
|
Sets the
properties that will be used by the
Kafka consumer that populates the
cache from the topic. Overrides
|
The repository can be instantiated by
defining the topic and bootstrapServers,
or the producerConfig and
consumerConfig property sets
can be explicitly defined to enable features
such as SSL/SASL.
To use, this repository must be placed in
the Camel registry, either manually or by
registration as a bean in Spring, as it is
CamelContext aware.
Sample usage is as follows:
KafkaIdempotentRepository kafkaIdempotentRepository = new KafkaIdempotentRepository("idempotent-db-inserts", "localhost:9091");
SimpleRegistry registry = new SimpleRegistry();
registry.put("insertDbIdemRepo", kafkaIdempotentRepository); // must be registered in the registry, to enable access to the CamelContext
CamelContext context = new CamelContext(registry);
// later in RouteBuilder...
from("direct:performInsert")
.idempotentConsumer(header("id")).idempotentRepository("insertDbIdemRepo")
// once-only insert into the database
.end()
In XML:
<!-- simple -->
<bean id="insertDbIdemRepo"
class="org.apache.camel.processor.idempotent.kafka.KafkaIdempotentRepository">
<property name="topic" value="idempotent-db-inserts"/>
<property name="bootstrapServers" value="localhost:9091"/>
</bean>
<!-- complex -->
<bean id="insertDbIdemRepo"
class="org.apache.camel.processor.idempotent.kafka.KafkaIdempotentRepository">
<property name="topic" value="idempotent-db-inserts"/>
<property name="maxCacheSize" value="10000"/>
<property name="consumerConfig">
<props>
<prop key="bootstrap.servers">localhost:9091</prop>
</props>
</property>
<property name="producerConfig">
<props>
<prop key="bootstrap.servers">localhost:9091</prop>
</props>
</property>
</bean>
There are 3 alternatives to choose from when
using idempotency with numeric identifiers.
The first one is to use the static method
numericHeader method from
org.apache.camel.component.kafka.serde.KafkaSerdeHelper
to perform the conversion for you:
from("direct:performInsert")
.idempotentConsumer(numericHeader("id")).idempotentRepository("insertDbIdemRepo")
// once-only insert into the database
.end()
Alternatively, it is possible to use a custom serializer configured via the route URL to perform the conversion:
public class CustomHeaderDeserializer extends DefaultKafkaHeaderDeserializer {
private static final Logger LOG = LoggerFactory.getLogger(CustomHeaderDeserializer.class);
@Override
public Object deserialize(String key, byte[] value) {
if (key.equals("id")) {
BigInteger bi = new BigInteger(value);
return String.valueOf(bi.longValue());
} else {
return super.deserialize(key, value);
}
}
}
Lastly, it is also possible to do so in a processor:
from(from).routeId("foo")
.process(exchange -> {
byte[] id = exchange.getIn().getHeader("id", byte[].class);
BigInteger bi = new BigInteger(id);
exchange.getIn().setHeader("id", String.valueOf(bi.longValue()));
})
.idempotentConsumer(header("id"))
.idempotentRepository("kafkaIdempotentRepository")
.to(to);
Manual commits with the Kafka consumer
By default, the Kafka consumer will use auto commit, where the offset will be committed automatically in the background using a given interval.
In case you want to force manual commits, you
can use KafkaManualCommit API
from the Camel Exchange, stored on the
message header.
This requires turning on manual commits by
either setting the option allowManualCommit
to true on the KafkaComponent
or on the endpoint, for example:
KafkaComponent kafka = new KafkaComponent();
kafka.setAutoCommitEnable(false);
kafka.setAllowManualCommit(true);
// ...
camelContext.addComponent("kafka", kafka);
By default, it uses the NoopCommitManager
behind the scenes. To commit an offset, you
will
require you to use the KafkaManualCommit
from Java code such as a Camel Processor:
public void process(Exchange exchange) {
KafkaManualCommit manual =
exchange.getIn().getHeader(KafkaConstants.MANUAL_COMMIT, KafkaManualCommit.class);
manual.commit();
}
The KafkaManualCommit will force
a synchronous commit which will block until
the commit is acknowledged on Kafka, or if
it fails an exception is thrown.
You can use an asynchronous commit as well
by configuring the KafkaManualCommitFactory
with the DefaultKafkaManualAsyncCommitFactory
implementation.
Then the commit will be done in the next consumer loop using the kafka asynchronous commit api.
If you want to use a custom implementation of
KafkaManualCommit then you can
configure a custom KafkaManualCommitFactory
on the KafkaComponent that
creates instances of your custom
implementation.
When configuring a consumer to use manual
commit and a specific
CommitManager it is important
to understand how these influence the
behavior
of breakOnFirstError
KafkaComponent kafka = new KafkaComponent();
kafka.setAutoCommitEnable(false);
kafka.setAllowManualCommit(true);
kafka.setBreakOnFirstError(true);
kafka.setKafkaManualCommitFactory(new DefaultKafkaManualCommitFactory());
...
camelContext.addComponent("kafka", kafka);
When the CommitManager is left
to the default
NoopCommitManager then breakOnFirstError
will not automatically commit the offset so
that the
message with an error is retried. The
consumer must manage that in the route using
KafkaManualCommit.
When the CommitManager is
changed to either the synchronous or
asynchronous manager then breakOnFirstError
will automatically commit the offset so that
the
message with an error is retried. This
message will be continually retried until it
can be processed without an error.
Note 1: records from a
partition must be processed and committed by
the same thread as the consumer. This means
that certain EIPs, async or concurrent
operations
in the DSL may cause the commit to fail. In
such circumstances, trying to commit the
transaction will cause the Kafka client to
throw a java.util.ConcurrentModificationException
exception with the message KafkaConsumer
is not safe for multi-threaded
access. To prevent this from
happening, redesign your route to avoid
those operations.
*Note 2: this is mostly useful with aggregation’s completion timeout strategies.
Pausable Consumers
The Kafka component supports pausable consumers. This type of consumer can pause consuming data based on conditions external to the component itself, such as an external system being unavailable or other transient conditions.
from("kafka:topic")
.pausable(new KafkaConsumerListener(), () -> canContinue()) // the pausable check gets called if the exchange fails to be processed ...
.routeId("pausable-route")
.process(this::process) // Kafka consumer will be paused if this one throws an exception ...
.to("some:destination"); // or this one
In this example, consuming messages can pause
(by calling the Kafka’s Consumer pause
method) if the result from
canContinue is false.
The pausable EIP is meant to be used
as a support mechanism when there
is an exception somewhere
in the route that prevents the
exchange from being processed. More
specifically,
the check called by the pausable
EIP should be used to test for
transient conditions preventing the
exchange from being processed.
|
| most users should prefer using the RoutePolicy, which offers better control of the route. |
Kafka Headers propagation
When consuming messages from Kafka, headers will be propagated to camel exchange headers automatically. Producing flow backed by same behaviour - camel headers of particular exchange will be propagated to kafka message headers.
Since kafka headers allow only
byte[] values, in order camel
exchange header to be propagated its value
should be serialized to bytes[],
otherwise header will be skipped.
The following header value types are
supported: String, Integer,
Long, Double,
Boolean, byte[].
Note: all headers propagated
from kafka
to camel exchange will
contain byte[] value by
default.
To override default functionality, these uri
parameters can be set: headerDeserializer
for from route and headerSerializer
for to route. For example:
from("kafka:my_topic?headerDeserializer=#myDeserializer")
...
.to("kafka:my_topic?headerSerializer=#mySerializer")
By default, all headers are being filtered by
KafkaHeaderFilterStrategy.
Strategy filters out headers which start
with Camel or org.apache.camel
prefixes.
Default strategy can be overridden by using
headerFilterStrategy uri
parameter in both to and from
routes:
from("kafka:my_topic?headerFilterStrategy=#myStrategy")
...
.to("kafka:my_topic?headerFilterStrategy=#myStrategy")
myStrategy object should be a
subclass of
HeaderFilterStrategy and must
be placed in the Camel registry, either
manually or by registration as a bean in
Spring, as it is CamelContext
aware.
Kafka Transaction
You need to add transactional.id,
enable.idempotence and retries
in additional-properties to
enable kafka transaction with the producer.
from("direct:transaction")
.to("kafka:my_topic?additional-properties[transactional.id]=1234&additional-properties[enable.idempotence]=true&additional-properties[retries]=5");
At the end of exchange routing, the kafka
producer would commit the transaction or
abort it if there is an Exception throwing
or the exchange is RollbackOnly.
Since Kafka does not support transactions in
multi threads, it will throw ProducerFencedException
if there is another producer with the same
transaction.id to make the
transactional request.
It would work with JTA camel-jta
by using transacted() and if it
involves some resources (SQL or JMS), which
supports XA, then they would work in tandem,
where they both will either commit or
rollback at the end of the exchange routing.
In some cases, if the JTA transaction
manager fails to commit (during the 2PC
processing), but kafka transaction has been
committed before and there is no chance to
roll back the changes since the kafka
transaction does not support JTA/XA spec.
There is still a risk with the data
consistency.
Setting Kerberos config file
Configure the 'krb5.conf' file directly through the API:
static {
KafkaComponent.setKerberosConfigLocation("path/to/config/file");
}
Authentication to Kafka
Kafka supports several ways to authenticate the clients to the server, including plain text, PKI (certificates) over TLS, you can refer to the Kafka documentation for a detailed view of the supported mechanisms. The kafka authentication and authorization is based on JAAS, so you must use a JAAS Login Module implementation on the client side.
This section will outline the main points for
the authentication using Strimzi
JAAS Login Module and plain text.
The Strimzi OAuth contains several
properties to fine tune, the authentication
mechanism, so you can set them into the
OAuthBearerLoginModule section.
The most basic way to authenticate is using the plain text login module with username and password. Beware that this is unsafe and and we suggest the OAuth over TLS for a fully secure mechanism.
Username and Password over TLS
camel.component.kafka.security-protocol = SASL_SSL
camel.component.kafka.sasl-mechanism=PLAIN
camel.component.kafka.sasl-jaas-config=org.apache.kafka.common.security.plain.PlainLoginModule required \
username="my_username" \
password="my_password";
There is the Strimzi OAuth Login Module that supports the more secure OAuth mechanisms, where you can set a refresh token, username/password and client secret. You must understand the Kafka Broker security settings to adequately configure the client security configuration.
OAuth Bearer Token with client secret
camel.component.kafka.security-protocol = SASL_PLAINTEXT
camel.component.kafka.sasl-mechanism = OAUTHBEARER
camel.component.kafka.sasl-jaas-config = org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required \
oauth.client.id="kafka-producer-client" \
oauth.client.secret="kafka-producer-client-secret" \
oauth.username.claim="preferred_username" \
oauth.ssl.truststore.location="docker/certificates/ca-truststore.p12" \
oauth.ssl.truststore.type="pkcs12" \
oauth.ssl.truststore.password="changeit" \
oauth.token.endpoint.uri="https://keycloak:8443/realms/demo/protocol/openid-connect/token" ;
camel.component.kafka.additional-properties[sasl.login.callback.handler.class]=io.strimzi.kafka.oauth.client.JaasClientOauthLoginCallbackHandler
OAuth Bearer Token with refresh token
camel.component.kafka.security-protocol = SASL_PLAINTEXT
camel.component.kafka.sasl-mechanism = OAUTHBEARER
camel.component.kafka.sasl-jaas-config = org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required \
oauth.client.id="kafka-producer-client" \
oauth.refresh.token="my_refresh_token"
oauth.username.claim="preferred_username" \
oauth.ssl.truststore.location="docker/certificates/ca-truststore.p12" \
oauth.ssl.truststore.type="pkcs12" \
oauth.ssl.truststore.password="changeit" \
oauth.token.endpoint.uri="https://keycloak:8443/realms/demo/protocol/openid-connect/token" ;
camel.component.kafka.additional-properties[sasl.login.callback.handler.class]=io.strimzi.kafka.oauth.client.JaasClientOauthLoginCallbackHandler
Batching Consumer
To use a Kafka batching consumer with Camel,
an application has to set the configuration
batching to true.
The received records are stored in a list in the exchange used in the pipeline. As such, it is possible to commit individually every record or the whole batch at once by committing the last exchange on the list.
The size of the batch is controlled by the
option maxPollRecords.
Camel will wait until the number of records
has been received per batch size (maxPollRecords).
However, this can take a long time, if there
are too few records and no new records is
being received.
To avoid blocking for too long, waiting for
the whole set of records to fill the batch,
it is possible to use the pollTimeoutMs
option
to set a timeout for the polling. The
timeout is only triggered if there has not
received any new messages for the given
timeout period.
So for example if
pollTimeoutMs=10000 then the
timeout is 10 seconds, and this will only
trigger if Camel did not receive any new
messages
from Kafka for 10 seconds. The timeout
trigger will reset if a message has been
received, so if you continuously and slowly
receive new messages,
then this timeout may not trigger for a long
time. Therefore, the option batchingIntervalMs
can therefore be used to specify an interval
(in mills)
to trigger the batch completion, to avoid
waiting for more messages to be received to
reach the batch size.
For example setting batchingIntervalMs=20000
would let Camel wait at most pollTimeoutMs
+ batchingIntervalMs before
triggering a batch completion. In this
example that would be 30 seconds.
Notice the pollTimeoutMs should
not be set to a high value, as it’s
used directly by Kafka while
receiving new messages from the broker.
Camel is not active during this processing,
and if the option
has been configured with a high value, then
Camel cannot trigger batch timeout or
interval completion ahead
of time. Therefore, it’s recommended
to keep this value as default.
Automatic Commits
By default, Camel uses automatic commits when using batch processing. In this case, Camel automatically commits the records after they have been successfully processed by the application.
In case of failures, the records will not be processed.
The code below provides an example of this approach:
public void configure() {
from("kafka:topic?groupId=myGroup&pollTimeoutMs=1000&batching=true&maxPollRecords=10&autoOffsetReset=earliest").process(e -> {
// The received records are stored as exchanges in a list. This gets the list of those exchanges
final List<?> exchanges = e.getMessage().getBody(List.class);
// Ensure we are actually receiving what we are asking for
if (exchanges == null || exchanges.isEmpty()) {
return;
}
// The records from the batch are stored in a list of exchanges in the original exchange. To process, we iterate over that list
for (Object obj : exchanges) {
if (obj instanceof Exchange exchange) {
LOG.info("Processing exchange with body {}", exchange.getMessage().getBody(String.class));
}
}
}).to(KafkaTestUtil.MOCK_RESULT);
}
Handling Errors with Automatic Commits
When using automatic commits, Camel will not commit records if there is a failure in processing. Because of this, there is a risk that records could be reprocessed multiple times.
It is recommended to implement appropriate error handling mechanisms and patterns (i.e.; such as dead-letter queues), to prevent failed records from blocking processing progress.
The code below provides an example of handling errors with automatic commits:
public void configure() {
/*
We want to use continued here, so that Camel auto-commits the batch even though part of it has failed. In a
production scenario, applications should probably send these records to a separate topic or fix the condition
that lead to the failure
*/
onException(IllegalArgumentException.class).process(exchange -> {
LOG.warn("Failed to process batch {}", exchange.getMessage().getBody());
LOG.warn("Failed to process due to {}", exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class).getMessage());
}).continued(true);
from("kafka:topic?groupId=myGroup&pollTimeoutMs=1000&batching=true&maxPollRecords=10&autoOffsetReset=earliest").process(e -> {
// The received records are stored as exchanges in a list. This gets the list of those exchanges
final List<?> exchanges = e.getMessage().getBody(List.class);
// Ensure we are actually receiving what we are asking for
if (exchanges == null || exchanges.isEmpty()) {
return;
}
// The records from the batch are stored in a list of exchanges in the original exchange.
int i = 0;
for (Object o : exchanges) {
if (o instanceof Exchange exchange) {
i++;
LOG.info("Processing exchange with body {}", exchange.getMessage().getBody(String.class));
if (i == 4) {
throw new IllegalArgumentException("Failed to process record");
}
}
}
}).to(KafkaTestUtil.MOCK_RESULT);
}
Manual Commits
When working with batch processing with manual commits, it’s up to the application to commit the records, and handle the outcome of potentially invalid records.
The code below provides an example of this approach:
public void configure() {
from("kafka:topic?batching=true&allowManualCommit=true&maxPollRecords=100&kafkaManualCommitFactory=#class:org.apache.camel.component.kafka.consumer.DefaultKafkaManualCommitFactory")
.process(e -> {
// The received records are stored as exchanges in a list. This gets the list of those exchanges
final List<?> exchanges = e.getMessage().getBody(List.class);
// Ensure we are actually receiving what we are asking for
if (exchanges == null || exchanges.isEmpty()) {
return;
}
/*
Every exchange in that list should contain a reference to the manual commit object. We use the reference
for the last exchange in the list to commit the whole batch
*/
final Object tmp = exchanges.getLast();
if (tmp instanceof Exchange exchange) {
KafkaManualCommit manual =
exchange.getMessage().getHeader(KafkaConstants.MANUAL_COMMIT, KafkaManualCommit.class);
LOG.debug("Performing manual commit");
manual.commit();
LOG.debug("Done performing manual commit");
}
});
}
Dealing with long polling timeouts
In some cases, applications may want the
polling process to have a long timeout
(see: pollTimeoutMs).
To properly do so, first make sure to
have a max polling interval that is
higher than the polling timeout (see:
maxPollIntervalMs).
Then, increase the shutdown timeout to ensure that committing, closing and other Kafka operations are not abruptly aborted. For instance:
public void configure() {
// Note that this can be configured in other ways
getCamelContext().getShutdownStrategy().setTimeout(10000);
// route setup ...
}
Custom Subscription Adapters
Applications with complex subscription logic
may provide a custom bean to handle the
subscription process. To so, it is
necessary to implement the interface SubscribeAdapter.
public class CustomSubscribeAdapter implements SubscribeAdapter {
@Override
public void subscribe(Consumer<?, ?> consumer, ConsumerRebalanceListener reBalanceListener, TopicInfo topicInfo) {
if (topicInfo.isPattern()) {
consumer.subscribe(topicInfo.getPattern(), reBalanceListener);
} else {
consumer.subscribe(topicInfo.getTopics(), reBalanceListener);
}
}
}
Then, it is necessary to add it as named bean instance to the registry:
context.getRegistry().bind(KafkaConstants.KAFKA_SUBSCRIBE_ADAPTER, new CustomSubscribeAdapter());
Interoperability
JMS
When interoperating Kafka and JMS, it may be necessary to coerce the JMS headers into their expected type.
For instance, when consuming messages
from Kafka carrying JMS headers and then
sending them to a JMS broker, those
headers are
first deserialized into a byte array.
Then, the camel-jms
component tries to coerce this byte
array into the
specific type used by.
However, both the origin endpoint as
well as how this was setup on the code
itself may affect how the data is
serialized and
deserialized. As such, it is not
feasible to naively assume the data type
of the byte array.
To address this issue, we provide a custom header deserializer to force Kafka to de-serialize the JMS data according to the JMS specification. This approach ensures that the headers are properly interpreted and processed by the camel-jms component.
Due to the inherent complexity of each possible system and endpoint, it may not be possible for this deserializer to cover all possible scenarios. As such, it is provided as model that can be modified and/or adapted for the specific needs of each application.
To utilize this solution, you need to
modify the route URI on the consumer end
of the pipeline by including the
headerDeserializer option.
For example:
from("kafka:topic?headerDeserializer=#class:org.apache.camel.component.kafka.consumer.support.interop.JMSDeserializer")
.to("...");
Producer Performance
If the producer is performing too slowly for your needs, you may want to aggregate the exchanges before sending.
from("source")
// .other route stuff
.aggregate(constant(true), new GroupedExchangeAggregationStrategy())
.to("kafka:topic");
The reason for this is related to how the producer handles the two different cases:
-
with the
aggregrateit should process the messages in a "batch-sized chunk" semi-asynchronously (that is, send all messages in the batch and then wait for their acknowledgements) -
without that, it sends synchronously, eventually blocking on the record metadata fetch per exchange.
| the downside of this approach is an increased number of in-flight exchanges and the potential risks (even though small and rare) associated with that. |
Examples
Consuming messages from Kafka
Here is the minimal route you need to read messages from Kafka.
from("kafka:test?brokers=localhost:9092")
.log("Message received from Kafka : ${body}")
.log(" on the topic ${headers[kafka.TOPIC]}")
.log(" on the partition ${headers[kafka.PARTITION]}")
.log(" with the offset ${headers[kafka.OFFSET]}")
.log(" with the key ${headers[kafka.KEY]}")
If you need to consume messages from multiple topics, you can use a comma separated list of topic names.
from("kafka:test,test1,test2?brokers=localhost:9092")
.log("Message received from Kafka : ${body}")
.log(" on the topic ${headers[kafka.TOPIC]}")
.log(" on the partition ${headers[kafka.PARTITION]}")
.log(" with the offset ${headers[kafka.OFFSET]}")
.log(" with the key ${headers[kafka.KEY]}")
It’s also possible to subscribe to
multiple topics giving a pattern as the
topic name and using the topicIsPattern
option.
from("kafka:test.*?brokers=localhost:9092&topicIsPattern=true")
.log("Message received from Kafka : ${body}")
.log(" on the topic ${headers[kafka.TOPIC]}")
.log(" on the partition ${headers[kafka.PARTITION]}")
.log(" with the offset ${headers[kafka.OFFSET]}")
.log(" with the key ${headers[kafka.KEY]}")
When consuming messages from Kafka, you can
use your own offset management and not
delegate this management to Kafka.
To keep the offsets, the component needs a
StateRepository implementation
such as FileStateRepository.
This bean should be available in the
registry.
Here how to use it :
// Create the repository in which the Kafka offsets will be persisted
FileStateRepository repository = FileStateRepository.fileStateRepository(new File("/path/to/repo.dat"));
// Bind this repository into the Camel registry
Registry registry = createCamelRegistry();
registry.bind("offsetRepo", repository);
// Configure the camel context
DefaultCamelContext camelContext = new DefaultCamelContext(registry);
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
fromF("kafka:%s?brokers=localhost:{{kafkaPort}}" +
// Set up the topic and broker address
"&groupId=A" +
// The consumer processor group ID
"&autoOffsetReset=earliest" +
// Ask to start from the beginning if we have unknown offset
"&offsetRepository=#offsetRepo", TOPIC)
// Keep the offsets in the previously configured repository
.to("mock:result");
}
});
Producing messages to Kafka
Here is the minimal route you need to produce messages to Kafka.
from("direct:start")
.setBody(constant("Message from Camel")) // Message to send
.setHeader(KafkaConstants.KEY, constant("Camel")) // Key of the message
.to("kafka:test?brokers=localhost:9092");
SSL configuration
You have two different ways to configure the SSL communication on the Kafka component.
The first way is through the many SSL endpoint parameters:
from("kafka:" + TOPIC + "?brokers=localhost:{{kafkaPort}}" +
"&groupId=A" +
"&sslKeystoreLocation=/path/to/keystore.jks" +
"&sslKeystorePassword=changeit" +
"&sslKeyPassword=changeit" +
"&securityProtocol=SSL")
.to("mock:result");
The second way is to use the sslContextParameters
endpoint parameter:
// Configure the SSLContextParameters object
KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource("/path/to/keystore.jks");
ksp.setPassword("changeit");
KeyManagersParameters kmp = new KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword("changeit");
SSLContextParameters scp = new SSLContextParameters();
scp.setKeyManagers(kmp);
// Bind this SSLContextParameters into the Camel registry
Registry registry = createCamelRegistry();
registry.bind("ssl", scp);
// Configure the camel context
DefaultCamelContext camelContext = new DefaultCamelContext(registry);
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("kafka:" + TOPIC + "?brokers=localhost:{{kafkaPort}}" +
// Set up the topic and broker address
"&groupId=A" +
// The consumer processor group ID
"&sslContextParameters=#ssl" +
// The security protocol
"&securityProtocol=SSL)
// Reference the SSL configuration
.to("mock:result");
}
});