Class SingleNodeKafkaResumeStrategy<K,V>
- java.lang.Object
-
- org.apache.camel.processor.resume.kafka.SingleNodeKafkaResumeStrategy<K,V>
-
- Type Parameters:
K
- the type of keyV
- the type of the value
- All Implemented Interfaces:
AutoCloseable
,KafkaResumeStrategy<K,V>
,org.apache.camel.resume.ResumeStrategy
,org.apache.camel.resume.UpdatableConsumerResumeStrategy<K,V,org.apache.camel.resume.Resumable<K,V>>
,org.apache.camel.Service
- Direct Known Subclasses:
MultiNodeKafkaResumeStrategy
public class SingleNodeKafkaResumeStrategy<K,V> extends Object implements KafkaResumeStrategy<K,V>
A resume strategy that publishes offsets to a Kafka topic. This resume strategy is suitable for single node integrations. For multi-node integrations (i.e: using clusters with the master component checkMultiNodeKafkaResumeStrategy
.
-
-
Constructor Summary
Constructors Constructor Description SingleNodeKafkaResumeStrategy(String bootstrapServers, String topic, org.apache.camel.resume.cache.ResumeCache<K,V> resumeCache, org.apache.camel.resume.ResumeAdapter resumeAdapter)
Builds an instance of this classSingleNodeKafkaResumeStrategy(String topic, org.apache.camel.resume.cache.ResumeCache<K,V> resumeCache, org.apache.camel.resume.ResumeAdapter resumeAdapter, Properties producerConfig, Properties consumerConfig)
Builds an instance of this class
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
build()
protected void
checkAndSubscribe(String topic)
Subscribe to the topic if not subscribed yetvoid
checkAndSubscribe(String topic, long remaining)
Subscribe to the topic if not subscribed yetvoid
close()
protected org.apache.kafka.clients.consumer.ConsumerRecords<K,V>
consume()
Consumes message from the topic previously setupprotected org.apache.kafka.clients.consumer.ConsumerRecords<K,V>
consume(int retries)
Consumes message from the topic previously setupstatic Properties
createConsumer(String bootstrapServers)
Creates a basic string-based consumerstatic Properties
createProducer(String bootstrapServers)
Creates a basic string-based producerorg.apache.camel.resume.ResumeAdapter
getAdapter()
protected org.apache.kafka.clients.consumer.Consumer<K,V>
getConsumer()
protected Properties
getConsumerConfig()
protected org.apache.kafka.clients.consumer.ConsumerRebalanceListener
getConsumerRebalanceListener(long remaining)
Creates a new consumer rebalance listener.Duration
getPollDuration()
protected org.apache.kafka.clients.producer.Producer<K,V>
getProducer()
protected Properties
getProducerConfig()
protected Collection<RecordError>
getProducerErrors()
Gets the set record of sent itemsprotected org.apache.camel.resume.cache.ResumeCache<K,V>
getResumeCache()
protected String
getTopic()
void
init()
protected void
loadCache()
Loads the existing data into the cacheprotected void
produce(K key, V message)
Sends data to a topic.void
resetProducerErrors()
Clear the producer errorsvoid
setPollDuration(Duration pollDuration)
void
start()
void
stop()
void
subscribe()
protected void
unsubscribe()
Unsubscribe from the topicvoid
updateLastOffset(org.apache.camel.resume.Resumable<K,V> offset)
-
-
-
Constructor Detail
-
SingleNodeKafkaResumeStrategy
public SingleNodeKafkaResumeStrategy(String bootstrapServers, String topic, org.apache.camel.resume.cache.ResumeCache<K,V> resumeCache, org.apache.camel.resume.ResumeAdapter resumeAdapter)
Builds an instance of this class- Parameters:
bootstrapServers
- the address of the Kafka brokertopic
- the topic where to publish the offsetsresumeCache
- a cache instance where to store the offsets locally for faster accessresumeAdapter
- the component-specific resume adapter
-
SingleNodeKafkaResumeStrategy
public SingleNodeKafkaResumeStrategy(String topic, org.apache.camel.resume.cache.ResumeCache<K,V> resumeCache, org.apache.camel.resume.ResumeAdapter resumeAdapter, Properties producerConfig, Properties consumerConfig)
Builds an instance of this class- Parameters:
topic
- the topic where to publish the offsetsresumeCache
- a cache instance where to store the offsets locally for faster accessresumeAdapter
- the component-specific resume adapterproducerConfig
- the set of properties to be used by the Kafka producer within this classconsumerConfig
- the set of properties to be used by the Kafka consumer within this class
-
-
Method Detail
-
createProducer
public static Properties createProducer(String bootstrapServers)
Creates a basic string-based producer- Parameters:
bootstrapServers
- the Kafka host- Returns:
- A set of default properties for producing string-based key/pair records from Kafka
-
createConsumer
public static Properties createConsumer(String bootstrapServers)
Creates a basic string-based consumer- Parameters:
bootstrapServers
- the Kafka host- Returns:
- A set of default properties for consuming string-based key/pair records from Kafka
-
produce
protected void produce(K key, V message) throws ExecutionException, InterruptedException
Sends data to a topic. The records will always be sent asynchronously. If there's an error, a producer error counter will be increased.- Parameters:
message
- the message to send- Throws:
ExecutionException
InterruptedException
- See Also:
getProducerErrors()
-
updateLastOffset
public void updateLastOffset(org.apache.camel.resume.Resumable<K,V> offset) throws Exception
-
loadCache
protected void loadCache() throws Exception
Loads the existing data into the cache- Throws:
Exception
-
checkAndSubscribe
protected void checkAndSubscribe(String topic)
Subscribe to the topic if not subscribed yet- Parameters:
topic
- the topic to consume the messages from
-
checkAndSubscribe
public void checkAndSubscribe(String topic, long remaining)
Subscribe to the topic if not subscribed yet- Parameters:
topic
- the topic to consume the messages fromremaining
- the number of messages to rewind from the last offset position (used to fill the cache)
-
getConsumerRebalanceListener
protected org.apache.kafka.clients.consumer.ConsumerRebalanceListener getConsumerRebalanceListener(long remaining)
Creates a new consumer rebalance listener. This can be useful for setting the exact Kafka offset when necessary to read a limited amount of messages or customize the resume strategy behavior when a rebalance occurs.- Parameters:
remaining
-- Returns:
-
unsubscribe
protected void unsubscribe()
Unsubscribe from the topic
-
consume
protected org.apache.kafka.clients.consumer.ConsumerRecords<K,V> consume()
Consumes message from the topic previously setup- Returns:
- An instance of the consumer records
-
consume
protected org.apache.kafka.clients.consumer.ConsumerRecords<K,V> consume(int retries)
Consumes message from the topic previously setup- Parameters:
retries
- how many times to retry consuming data from the topic- Returns:
- An instance of the consumer records
-
getAdapter
public org.apache.camel.resume.ResumeAdapter getAdapter()
- Specified by:
getAdapter
in interfaceorg.apache.camel.resume.ResumeStrategy
-
getProducerErrors
protected Collection<RecordError> getProducerErrors()
Gets the set record of sent items- Returns:
-
build
public void build()
- Specified by:
build
in interfaceorg.apache.camel.Service
-
init
public void init()
- Specified by:
init
in interfaceorg.apache.camel.Service
-
stop
public void stop()
- Specified by:
stop
in interfaceorg.apache.camel.Service
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceorg.apache.camel.Service
- Throws:
IOException
-
start
public void start()
- Specified by:
start
in interfaceorg.apache.camel.Service
-
getPollDuration
public Duration getPollDuration()
-
setPollDuration
public void setPollDuration(Duration pollDuration)
-
getProducerConfig
protected Properties getProducerConfig()
-
getConsumerConfig
protected Properties getConsumerConfig()
-
getTopic
protected String getTopic()
-
resetProducerErrors
public void resetProducerErrors()
Clear the producer errors
-
-