Package com.networknt.kafka.producer
Class TransactionalProducer
- java.lang.Object
-
- com.networknt.kafka.producer.TransactionalProducer
-
- All Implemented Interfaces:
LightProducer,QueuedLightProducer,Runnable
public class TransactionalProducer extends Object implements Runnable, QueuedLightProducer
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTransactionalProducer.NextTransactionalIdHintKeep information required to deduce next safe to use transactional id.static classTransactionalProducer.TransactionHolder<KafkaTransactionState>Adds metadata (currently only the start time of the transaction) to the transaction object.
-
Constructor Summary
Constructors Constructor Description TransactionalProducer()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidabort(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)static intaddressToPartition(String address)com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionStatebeginTransaction()voidclose()This method is called by a shutdown hook to close the producer if it is not null.protected voidcommit(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionStatecurrentTransaction()static <E> intdrain(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit)Drains the queue asBlockingQueue.drainTo(Collection, int), but if the requestednumElementselements are not available, it will wait for them up to the specified timeout.BlockingQueue<org.apache.kafka.clients.producer.ProducerRecord<byte[],byte[]>>getTxQueue()Get the blocking queue that is used to cache messages so that they can be sent in a batch.voidinvoke(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction, String topic, org.apache.kafka.clients.producer.ProducerRecord<byte[],byte[]> record)voidopen()Initializes the connection to Kafka.protected voidpreCommit(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)voidpropagateHeaders(org.apache.kafka.clients.producer.ProducerRecord record, io.undertow.server.HttpServerExchange exchange)For each producer, it is responsible for populate the header of the ProducerRecord.protected voidrecoverAndAbort(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)protected voidrecoverAndCommit(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)voidrun()
-
-
-
Method Detail
-
getTxQueue
public BlockingQueue<org.apache.kafka.clients.producer.ProducerRecord<byte[],byte[]>> getTxQueue()
Description copied from interface:QueuedLightProducerGet the blocking queue that is used to cache messages so that they can be sent in a batch.- Specified by:
getTxQueuein interfaceQueuedLightProducer- Returns:
- the queue that contains the cached messages
-
drain
public static <E> int drain(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit) throws InterruptedException
Drains the queue asBlockingQueue.drainTo(Collection, int), but if the requestednumElementselements are not available, it will wait for them up to the specified timeout.- Type Parameters:
E- element- Parameters:
q- the blocking queue to be drainedbuffer- where to add the transferred elementsnumElements- the number of elements to be waited fortimeout- how long to wait before giving up, in units ofunitunit- aTimeUnitdetermining how to interpret the timeout parameter- Returns:
- the number of elements transferred
- Throws:
InterruptedException- if interrupted while waiting
-
invoke
public void invoke(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction, String topic, org.apache.kafka.clients.producer.ProducerRecord<byte[],byte[]> record) throws TransactionalKafkaException- Throws:
TransactionalKafkaException
-
open
public void open()
Initializes the connection to Kafka.- Specified by:
openin interfaceLightProducer- Specified by:
openin interfaceQueuedLightProducer
-
preCommit
protected void preCommit(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction) throws TransactionalKafkaException- Throws:
TransactionalKafkaException
-
commit
protected void commit(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)
-
recoverAndCommit
protected void recoverAndCommit(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)
-
abort
protected void abort(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)
-
recoverAndAbort
protected void recoverAndAbort(com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState transaction)
-
close
public void close()
Description copied from interface:LightProducerThis method is called by a shutdown hook to close the producer if it is not null.- Specified by:
closein interfaceLightProducer
-
currentTransaction
public com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState currentTransaction()
-
beginTransaction
public com.networknt.kafka.producer.TransactionalProducer.KafkaTransactionState beginTransaction() throws TransactionalKafkaException- Throws:
TransactionalKafkaException
-
propagateHeaders
public void propagateHeaders(org.apache.kafka.clients.producer.ProducerRecord record, io.undertow.server.HttpServerExchange exchange)Description copied from interface:QueuedLightProducerFor each producer, it is responsible for populate the header of the ProducerRecord.- Specified by:
propagateHeadersin interfaceQueuedLightProducer- Parameters:
record- the ProducerRecordexchange- the Undertow exchange that carries HTTP headers
-
addressToPartition
public static int addressToPartition(String address)
-
-