blob: 30ae74596ae73e34a5b0a37d7525910b3038a195 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Generated by javadoc (17) -->
<title>KafkaProducer (kafka 3.6.1 API)</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="description" content="declaration: package: org.apache.kafka.clients.producer, class: KafkaProducer">
<meta name="generator" content="javadoc/ClassWriterImpl">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
<link rel="stylesheet" type="text/css" href="../../../../../script-dir/jquery-ui.min.css" title="Style">
<link rel="stylesheet" type="text/css" href="../../../../../jquery-ui.overrides.css" title="Style">
<script type="text/javascript" src="../../../../../script.js"></script>
<script type="text/javascript" src="../../../../../script-dir/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="../../../../../script-dir/jquery-ui.min.js"></script>
</head>
<body class="class-declaration-page">
<script type="text/javascript">var evenRowColor = "even-row-color";
var oddRowColor = "odd-row-color";
var tableTab = "table-tab";
var activeTableTab = "active-table-tab";
var pathtoroot = "../../../../../";
loadScripts(document, 'script');</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<div class="flex-box">
<header role="banner" class="flex-header">
<nav role="navigation">
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="top-nav" id="navbar-top">
<div class="skip-nav"><a href="#skip-navbar-top" title="Skip navigation links">Skip navigation links</a></div>
<ul id="navbar-top-firstrow" class="nav-list" title="Navigation">
<li><a href="../../../../../index.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="nav-bar-cell1-rev">Class</li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../../../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../../../../index-all.html">Index</a></li>
<li><a href="../../../../../help-doc.html#class">Help</a></li>
</ul>
</div>
<div class="sub-nav">
<div>
<ul class="sub-nav-list">
<li>Summary:&nbsp;</li>
<li>Nested&nbsp;|&nbsp;</li>
<li><a href="#field-summary">Field</a>&nbsp;|&nbsp;</li>
<li><a href="#constructor-summary">Constr</a>&nbsp;|&nbsp;</li>
<li><a href="#method-summary">Method</a></li>
</ul>
<ul class="sub-nav-list">
<li>Detail:&nbsp;</li>
<li><a href="#field-detail">Field</a>&nbsp;|&nbsp;</li>
<li><a href="#constructor-detail">Constr</a>&nbsp;|&nbsp;</li>
<li><a href="#method-detail">Method</a></li>
</ul>
</div>
<div class="nav-list-search"><label for="search-input">SEARCH:</label>
<input type="text" id="search-input" value="search" disabled="disabled">
<input type="reset" id="reset-button" value="reset" disabled="disabled">
</div>
</div>
<!-- ========= END OF TOP NAVBAR ========= -->
<span class="skip-nav" id="skip-navbar-top"></span></nav>
</header>
<div class="flex-content">
<main role="main">
<!-- ======== START OF CLASS DATA ======== -->
<div class="header">
<div class="sub-title"><span class="package-label-in-type">Package</span>&nbsp;<a href="package-summary.html">org.apache.kafka.clients.producer</a></div>
<h1 title="Class KafkaProducer" class="title">Class KafkaProducer&lt;K,<wbr>V&gt;</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.apache.kafka.clients.producer.KafkaProducer&lt;K,<wbr>V&gt;</div>
</div>
<section class="class-description" id="class-description">
<dl class="notes">
<dt>All Implemented Interfaces:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/Closeable.html" title="class or interface in java.io" class="external-link">Closeable</a></code>, <code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/AutoCloseable.html" title="class or interface in java.lang" class="external-link">AutoCloseable</a></code>, <code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;K,<wbr>V&gt;</code></dd>
</dl>
<hr>
<div class="type-signature"><span class="modifiers">public class </span><span class="element-name type-name-label">KafkaProducer&lt;K,<wbr>V&gt;</span>
<span class="extends-implements">extends <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">Object</a>
implements <a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;K,<wbr>V&gt;</span></div>
<div class="block">A Kafka client that publishes records to the Kafka cluster.
<P>
The producer is <i>thread safe</i> and sharing a single producer instance across threads will generally be faster than
having multiple instances.
<p>
Here is a simple example of using the producer to send records with strings containing sequential numbers as the key/value
pairs.
<pre>
<code>
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("linger.ms", 1);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);
for (int i = 0; i &lt; 100; i++)
producer.send(new ProducerRecord&lt;String, String&gt;("my-topic", Integer.toString(i), Integer.toString(i)));
producer.close();
</code></pre>
<p>
The producer consists of a pool of buffer space that holds records that haven't yet been transmitted to the server
as well as a background I/O thread that is responsible for turning these records into requests and transmitting them
to the cluster. Failure to close the producer after use will leak these resources.
<p>
The <a href="#send(org.apache.kafka.clients.producer.ProducerRecord)"><code>send()</code></a> method is asynchronous. When called, it adds the record to a buffer of pending record sends
and immediately returns. This allows the producer to batch together individual records for efficiency.
<p>
The <code>acks</code> config controls the criteria under which requests are considered complete. The default setting "all"
will result in blocking on the full commit of the record, the slowest but most durable setting.
<p>
If the request fails, the producer can automatically retry. The <code>retries</code> setting defaults to <code>Integer.MAX_VALUE</code>, and
it's recommended to use <code>delivery.timeout.ms</code> to control retry behavior, instead of <code>retries</code>.
<p>
The producer maintains buffers of unsent records for each partition. These buffers are of a size specified by
the <code>batch.size</code> config. Making this larger can result in more batching, but requires more memory (since we will
generally have one of these buffers for each active partition).
<p>
By default a buffer is available to send immediately even if there is additional unused space in the buffer. However if you
want to reduce the number of requests you can set <code>linger.ms</code> to something greater than 0. This will
instruct the producer to wait up to that number of milliseconds before sending a request in hope that more records will
arrive to fill up the same batch. This is analogous to Nagle's algorithm in TCP. For example, in the code snippet above,
likely all 100 records would be sent in a single request since we set our linger time to 1 millisecond. However this setting
would add 1 millisecond of latency to our request waiting for more records to arrive if we didn't fill up the buffer. Note that
records that arrive close together in time will generally batch together even with <code>linger.ms=0</code>. So, under heavy load,
batching will occur regardless of the linger configuration; however setting this to something larger than 0 can lead to fewer, more
efficient requests when not under maximal load at the cost of a small amount of latency.
<p>
The <code>buffer.memory</code> controls the total amount of memory available to the producer for buffering. If records
are sent faster than they can be transmitted to the server then this buffer space will be exhausted. When the buffer space is
exhausted additional send calls will block. The threshold for time to block is determined by <code>max.block.ms</code> after which it throws
a TimeoutException.
<p>
The <code>key.serializer</code> and <code>value.serializer</code> instruct how to turn the key and value objects the user provides with
their <code>ProducerRecord</code> into bytes. You can use the included <a href="../../common/serialization/ByteArraySerializer.html" title="class in org.apache.kafka.common.serialization"><code>ByteArraySerializer</code></a> or
<a href="../../common/serialization/StringSerializer.html" title="class in org.apache.kafka.common.serialization"><code>StringSerializer</code></a> for simple byte or string types.
<p>
From Kafka 0.11, the KafkaProducer supports two additional modes: the idempotent producer and the transactional producer.
The idempotent producer strengthens Kafka's delivery semantics from at least once to exactly once delivery. In particular
producer retries will no longer introduce duplicates. The transactional producer allows an application to send messages
to multiple partitions (and topics!) atomically.
</p>
<p>
From Kafka 3.0, the <code>enable.idempotence</code> configuration defaults to true. When enabling idempotence,
<code>retries</code> config will default to <code>Integer.MAX_VALUE</code> and the <code>acks</code> config will
default to <code>all</code>. There are no API changes for the idempotent producer, so existing applications will
not need to be modified to take advantage of this feature.
</p>
<p>
To take advantage of the idempotent producer, it is imperative to avoid application level re-sends since these cannot
be de-duplicated. As such, if an application enables idempotence, it is recommended to leave the <code>retries</code>
config unset, as it will be defaulted to <code>Integer.MAX_VALUE</code>. Additionally, if a <a href="#send(org.apache.kafka.clients.producer.ProducerRecord)"><code>send(ProducerRecord)</code></a>
returns an error even with infinite retries (for instance if the message expires in the buffer before being sent),
then it is recommended to shut down the producer and check the contents of the last produced message to ensure that
it is not duplicated. Finally, the producer can only guarantee idempotence for messages sent within a single session.
</p>
<p>To use the transactional producer and the attendant APIs, you must set the <code>transactional.id</code>
configuration property. If the <code>transactional.id</code> is set, idempotence is automatically enabled along with
the producer configs which idempotence depends on. Further, topics which are included in transactions should be configured
for durability. In particular, the <code>replication.factor</code> should be at least <code>3</code>, and the
<code>min.insync.replicas</code> for these topics should be set to 2. Finally, in order for transactional guarantees
to be realized from end-to-end, the consumers must be configured to read only committed messages as well.
</p>
<p>
The purpose of the <code>transactional.id</code> is to enable transaction recovery across multiple sessions of a
single producer instance. It would typically be derived from the shard identifier in a partitioned, stateful, application.
As such, it should be unique to each producer instance running within a partitioned application.
</p>
<p>All the new transactional APIs are blocking and will throw exceptions on failure. The example
below illustrates how the new APIs are meant to be used. It is similar to the example above, except that all
100 messages are part of a single transaction.
</p>
<p>
<pre>
<code>
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("transactional.id", "my-transactional-id");
Producer&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props, new StringSerializer(), new StringSerializer());
producer.initTransactions();
try {
producer.beginTransaction();
for (int i = 0; i &lt; 100; i++)
producer.send(new ProducerRecord&lt;&gt;("my-topic", Integer.toString(i), Integer.toString(i)));
producer.commitTransaction();
} catch (ProducerFencedException | OutOfOrderSequenceException | AuthorizationException e) {
// We can't recover from these exceptions, so our only option is to close the producer and exit.
producer.close();
} catch (KafkaException e) {
// For all other exceptions, just abort the transaction and try again.
producer.abortTransaction();
}
producer.close();
</code> </pre>
</p>
<p>
As is hinted at in the example, there can be only one open transaction per producer. All messages sent between the
<a href="#beginTransaction()"><code>beginTransaction()</code></a> and <a href="#commitTransaction()"><code>commitTransaction()</code></a> calls will be part of a single transaction. When the
<code>transactional.id</code> is specified, all messages sent by the producer must be part of a transaction.
</p>
<p>
The transactional producer uses exceptions to communicate error states. In particular, it is not required
to specify callbacks for <code>producer.send()</code> or to call <code>.get()</code> on the returned Future: a
<code>KafkaException</code> would be thrown if any of the
<code>producer.send()</code> or transactional calls hit an irrecoverable error during a transaction. See the <a href="#send(org.apache.kafka.clients.producer.ProducerRecord)"><code>send(ProducerRecord)</code></a>
documentation for more details about detecting errors from a transactional send.
</p>
</p>By calling
<code>producer.abortTransaction()</code> upon receiving a <code>KafkaException</code> we can ensure that any
successful writes are marked as aborted, hence keeping the transactional guarantees.
</p>
<p>
This client can communicate with brokers that are version 0.10.0 or newer. Older or newer brokers may not support
certain client features. For instance, the transactional APIs need broker versions 0.11.0 or later. You will receive an
<code>UnsupportedVersionException</code> when invoking an API that is not available in the running broker version.
</p></div>
</section>
<section class="summary">
<ul class="summary-list">
<!-- =========== FIELD SUMMARY =========== -->
<li>
<section class="field-summary" id="field-summary">
<h2>Field Summary</h2>
<div class="caption"><span>Fields</span></div>
<div class="summary-table three-column-summary">
<div class="table-header col-first">Modifier and Type</div>
<div class="table-header col-second">Field</div>
<div class="table-header col-last">Description</div>
<div class="col-first even-row-color"><code>static final <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a></code></div>
<div class="col-second even-row-color"><code><a href="#NETWORK_THREAD_PREFIX" class="member-name-link">NETWORK_THREAD_PREFIX</a></code></div>
<div class="col-last even-row-color">&nbsp;</div>
<div class="col-first odd-row-color"><code>static final <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a></code></div>
<div class="col-second odd-row-color"><code><a href="#PRODUCER_METRIC_GROUP_NAME" class="member-name-link">PRODUCER_METRIC_GROUP_NAME</a></code></div>
<div class="col-last odd-row-color">&nbsp;</div>
</div>
</section>
</li>
<!-- ======== CONSTRUCTOR SUMMARY ======== -->
<li>
<section class="constructor-summary" id="constructor-summary">
<h2>Constructor Summary</h2>
<div class="caption"><span>Constructors</span></div>
<div class="summary-table two-column-summary">
<div class="table-header col-first">Constructor</div>
<div class="table-header col-last">Description</div>
<div class="col-constructor-name even-row-color"><code><a href="#%3Cinit%3E(java.util.Map)" class="member-name-link">KafkaProducer</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a>,<wbr><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">Object</a>&gt;&nbsp;configs)</code></div>
<div class="col-last even-row-color">
<div class="block">A producer is instantiated by providing a set of key-value pairs as configuration.</div>
</div>
<div class="col-constructor-name odd-row-color"><code><a href="#%3Cinit%3E(java.util.Map,org.apache.kafka.common.serialization.Serializer,org.apache.kafka.common.serialization.Serializer)" class="member-name-link">KafkaProducer</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a>,<wbr><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">Object</a>&gt;&nbsp;configs,
<a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization">Serializer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>&gt;&nbsp;keySerializer,
<a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization">Serializer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;&nbsp;valueSerializer)</code></div>
<div class="col-last odd-row-color">
<div class="block">A producer is instantiated by providing a set of key-value pairs as configuration, a key and a value <a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization"><code>Serializer</code></a>.</div>
</div>
<div class="col-constructor-name even-row-color"><code><a href="#%3Cinit%3E(java.util.Properties)" class="member-name-link">KafkaProducer</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Properties.html" title="class or interface in java.util" class="external-link">Properties</a>&nbsp;properties)</code></div>
<div class="col-last even-row-color">
<div class="block">A producer is instantiated by providing a set of key-value pairs as configuration.</div>
</div>
<div class="col-constructor-name odd-row-color"><code><a href="#%3Cinit%3E(java.util.Properties,org.apache.kafka.common.serialization.Serializer,org.apache.kafka.common.serialization.Serializer)" class="member-name-link">KafkaProducer</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Properties.html" title="class or interface in java.util" class="external-link">Properties</a>&nbsp;properties,
<a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization">Serializer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>&gt;&nbsp;keySerializer,
<a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization">Serializer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;&nbsp;valueSerializer)</code></div>
<div class="col-last odd-row-color">
<div class="block">A producer is instantiated by providing a set of key-value pairs as configuration, a key and a value <a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization"><code>Serializer</code></a>.</div>
</div>
</div>
</section>
</li>
<!-- ========== METHOD SUMMARY =========== -->
<li>
<section class="method-summary" id="method-summary">
<h2>Method Summary</h2>
<div id="method-summary-table">
<div class="table-tabs" role="tablist" aria-orientation="horizontal"><button id="method-summary-table-tab0" role="tab" aria-selected="true" aria-controls="method-summary-table.tabpanel" tabindex="0" onkeydown="switchTab(event)" onclick="show('method-summary-table', 'method-summary-table', 3)" class="active-table-tab">All Methods</button><button id="method-summary-table-tab2" role="tab" aria-selected="false" aria-controls="method-summary-table.tabpanel" tabindex="-1" onkeydown="switchTab(event)" onclick="show('method-summary-table', 'method-summary-table-tab2', 3)" class="table-tab">Instance Methods</button><button id="method-summary-table-tab4" role="tab" aria-selected="false" aria-controls="method-summary-table.tabpanel" tabindex="-1" onkeydown="switchTab(event)" onclick="show('method-summary-table', 'method-summary-table-tab4', 3)" class="table-tab">Concrete Methods</button><button id="method-summary-table-tab6" role="tab" aria-selected="false" aria-controls="method-summary-table.tabpanel" tabindex="-1" onkeydown="switchTab(event)" onclick="show('method-summary-table', 'method-summary-table-tab6', 3)" class="table-tab">Deprecated Methods</button></div>
<div id="method-summary-table.tabpanel" role="tabpanel">
<div class="summary-table three-column-summary" aria-labelledby="method-summary-table-tab0">
<div class="table-header col-first">Modifier and Type</div>
<div class="table-header col-second">Method</div>
<div class="table-header col-last">Description</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code>void</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#abortTransaction()" class="member-name-link">abortTransaction</a>()</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Aborts the ongoing transaction.</div>
</div>
<div class="col-first odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code>void</code></div>
<div class="col-second odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#beginTransaction()" class="member-name-link">beginTransaction</a>()</code></div>
<div class="col-last odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Should be called before the start of each new transaction.</div>
</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code>void</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#close()" class="member-name-link">close</a>()</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Close this producer.</div>
</div>
<div class="col-first odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code>void</code></div>
<div class="col-second odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#close(java.time.Duration)" class="member-name-link">close</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/Duration.html" title="class or interface in java.time" class="external-link">Duration</a>&nbsp;timeout)</code></div>
<div class="col-last odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">This method waits up to <code>timeout</code> for the producer to complete the sending of all incomplete requests.</div>
</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code>void</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#commitTransaction()" class="member-name-link">commitTransaction</a>()</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Commits the ongoing transaction.</div>
</div>
<div class="col-first odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code>void</code></div>
<div class="col-second odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#flush()" class="member-name-link">flush</a>()</code></div>
<div class="col-last odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Invoking this method makes all buffered records immediately available to send (even if <code>linger.ms</code> is
greater than 0) and blocks on the completion of the requests associated with these records.</div>
</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code>void</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#initTransactions()" class="member-name-link">initTransactions</a>()</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Needs to be called before any other methods when the <code>transactional.id</code> is set in the configuration.</div>
</div>
<div class="col-first odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="../../common/MetricName.html" title="class in org.apache.kafka.common">MetricName</a>,<wbr>? extends <a href="../../common/Metric.html" title="interface in org.apache.kafka.common">Metric</a>&gt;</code></div>
<div class="col-second odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#metrics()" class="member-name-link">metrics</a>()</code></div>
<div class="col-last odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Get the full set of internal metrics maintained by the producer.</div>
</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html" title="class or interface in java.util" class="external-link">List</a>&lt;<a href="../../common/PartitionInfo.html" title="class in org.apache.kafka.common">PartitionInfo</a>&gt;</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#partitionsFor(java.lang.String)" class="member-name-link">partitionsFor</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a>&nbsp;topic)</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Get the partition metadata for the given topic.</div>
</div>
<div class="col-first odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Future.html" title="class or interface in java.util.concurrent" class="external-link">Future</a>&lt;<a href="RecordMetadata.html" title="class in org.apache.kafka.clients.producer">RecordMetadata</a>&gt;</code></div>
<div class="col-second odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#send(org.apache.kafka.clients.producer.ProducerRecord)" class="member-name-link">send</a><wbr>(<a href="ProducerRecord.html" title="class in org.apache.kafka.clients.producer">ProducerRecord</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;&nbsp;record)</code></div>
<div class="col-last odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Asynchronously send a record to a topic.</div>
</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Future.html" title="class or interface in java.util.concurrent" class="external-link">Future</a>&lt;<a href="RecordMetadata.html" title="class in org.apache.kafka.clients.producer">RecordMetadata</a>&gt;</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#send(org.apache.kafka.clients.producer.ProducerRecord,org.apache.kafka.clients.producer.Callback)" class="member-name-link">send</a><wbr>(<a href="ProducerRecord.html" title="class in org.apache.kafka.clients.producer">ProducerRecord</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;&nbsp;record,
<a href="Callback.html" title="interface in org.apache.kafka.clients.producer">Callback</a>&nbsp;callback)</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Asynchronously send a record to a topic and invoke the provided callback when the send has been acknowledged.</div>
</div>
<div class="col-first odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4 method-summary-table-tab6"><code>void</code></div>
<div class="col-second odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4 method-summary-table-tab6"><code><a href="#sendOffsetsToTransaction(java.util.Map,java.lang.String)" class="member-name-link">sendOffsetsToTransaction</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="../../common/TopicPartition.html" title="class in org.apache.kafka.common">TopicPartition</a>,<wbr><a href="../consumer/OffsetAndMetadata.html" title="class in org.apache.kafka.clients.consumer">OffsetAndMetadata</a>&gt;&nbsp;offsets,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a>&nbsp;consumerGroupId)</code></div>
<div class="col-last odd-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4 method-summary-table-tab6">
<div class="block"><span class="deprecated-label">Deprecated.</span>
<div class="deprecation-comment">Since 3.0.0, please use <a href="#sendOffsetsToTransaction(java.util.Map,org.apache.kafka.clients.consumer.ConsumerGroupMetadata)"><code>sendOffsetsToTransaction(Map, ConsumerGroupMetadata)</code></a> instead.</div>
</div>
</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code>void</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4"><code><a href="#sendOffsetsToTransaction(java.util.Map,org.apache.kafka.clients.consumer.ConsumerGroupMetadata)" class="member-name-link">sendOffsetsToTransaction</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="../../common/TopicPartition.html" title="class in org.apache.kafka.common">TopicPartition</a>,<wbr><a href="../consumer/OffsetAndMetadata.html" title="class in org.apache.kafka.clients.consumer">OffsetAndMetadata</a>&gt;&nbsp;offsets,
<a href="../consumer/ConsumerGroupMetadata.html" title="class in org.apache.kafka.clients.consumer">ConsumerGroupMetadata</a>&nbsp;groupMetadata)</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab2 method-summary-table-tab4">
<div class="block">Sends a list of specified offsets to the consumer group coordinator, and also marks
those offsets as part of the current transaction.</div>
</div>
</div>
</div>
</div>
<div class="inherited-list">
<h3 id="methods-inherited-from-class-java.lang.Object">Methods inherited from class&nbsp;java.lang.<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">Object</a></h3>
<code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#clone()" title="class or interface in java.lang" class="external-link">clone</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)" title="class or interface in java.lang" class="external-link">equals</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#finalize()" title="class or interface in java.lang" class="external-link">finalize</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#getClass()" title="class or interface in java.lang" class="external-link">getClass</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#hashCode()" title="class or interface in java.lang" class="external-link">hashCode</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#notify()" title="class or interface in java.lang" class="external-link">notify</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#notifyAll()" title="class or interface in java.lang" class="external-link">notifyAll</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#toString()" title="class or interface in java.lang" class="external-link">toString</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#wait()" title="class or interface in java.lang" class="external-link">wait</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#wait(long)" title="class or interface in java.lang" class="external-link">wait</a>, <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#wait(long,int)" title="class or interface in java.lang" class="external-link">wait</a></code></div>
</section>
</li>
</ul>
</section>
<section class="details">
<ul class="details-list">
<!-- ============ FIELD DETAIL =========== -->
<li>
<section class="field-details" id="field-detail">
<h2>Field Details</h2>
<ul class="member-list">
<li>
<section class="detail" id="NETWORK_THREAD_PREFIX">
<h3>NETWORK_THREAD_PREFIX</h3>
<div class="member-signature"><span class="modifiers">public static final</span>&nbsp;<span class="return-type"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a></span>&nbsp;<span class="element-name">NETWORK_THREAD_PREFIX</span></div>
<dl class="notes">
<dt>See Also:</dt>
<dd>
<ul class="see-list">
<li><a href="../../../../../constant-values.html#org.apache.kafka.clients.producer.KafkaProducer.NETWORK_THREAD_PREFIX">Constant Field Values</a></li>
</ul>
</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="PRODUCER_METRIC_GROUP_NAME">
<h3>PRODUCER_METRIC_GROUP_NAME</h3>
<div class="member-signature"><span class="modifiers">public static final</span>&nbsp;<span class="return-type"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a></span>&nbsp;<span class="element-name">PRODUCER_METRIC_GROUP_NAME</span></div>
<dl class="notes">
<dt>See Also:</dt>
<dd>
<ul class="see-list">
<li><a href="../../../../../constant-values.html#org.apache.kafka.clients.producer.KafkaProducer.PRODUCER_METRIC_GROUP_NAME">Constant Field Values</a></li>
</ul>
</dd>
</dl>
</section>
</li>
</ul>
</section>
</li>
<!-- ========= CONSTRUCTOR DETAIL ======== -->
<li>
<section class="constructor-details" id="constructor-detail">
<h2>Constructor Details</h2>
<ul class="member-list">
<li>
<section class="detail" id="&lt;init&gt;(java.util.Map)">
<h3>KafkaProducer</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="element-name">KafkaProducer</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a>,<wbr><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">Object</a>&gt;&nbsp;configs)</span></div>
<div class="block">A producer is instantiated by providing a set of key-value pairs as configuration. Valid configuration strings
are documented <a href="http://kafka.apache.org/documentation.html#producerconfigs">here</a>. Values can be
either strings or Objects of the appropriate type (for example a numeric configuration would accept either the
string "42" or the integer 42).
<p>
Note: after creating a <code>KafkaProducer</code> you must always <a href="#close()"><code>close()</code></a> it to avoid resource leaks.</div>
<dl class="notes">
<dt>Parameters:</dt>
<dd><code>configs</code> - The producer configs</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="&lt;init&gt;(java.util.Map,org.apache.kafka.common.serialization.Serializer,org.apache.kafka.common.serialization.Serializer)">
<h3>KafkaProducer</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="element-name">KafkaProducer</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a>,<wbr><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">Object</a>&gt;&nbsp;configs,
<a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization">Serializer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>&gt;&nbsp;keySerializer,
<a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization">Serializer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;&nbsp;valueSerializer)</span></div>
<div class="block">A producer is instantiated by providing a set of key-value pairs as configuration, a key and a value <a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization"><code>Serializer</code></a>.
Valid configuration strings are documented <a href="http://kafka.apache.org/documentation.html#producerconfigs">here</a>.
Values can be either strings or Objects of the appropriate type (for example a numeric configuration would accept
either the string "42" or the integer 42).
<p>
Note: after creating a <code>KafkaProducer</code> you must always <a href="#close()"><code>close()</code></a> it to avoid resource leaks.</div>
<dl class="notes">
<dt>Parameters:</dt>
<dd><code>configs</code> - The producer configs</dd>
<dd><code>keySerializer</code> - The serializer for key that implements <a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization"><code>Serializer</code></a>. The configure() method won't be
called in the producer when the serializer is passed in directly.</dd>
<dd><code>valueSerializer</code> - The serializer for value that implements <a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization"><code>Serializer</code></a>. The configure() method won't
be called in the producer when the serializer is passed in directly.</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="&lt;init&gt;(java.util.Properties)">
<h3>KafkaProducer</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="element-name">KafkaProducer</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Properties.html" title="class or interface in java.util" class="external-link">Properties</a>&nbsp;properties)</span></div>
<div class="block">A producer is instantiated by providing a set of key-value pairs as configuration. Valid configuration strings
are documented <a href="http://kafka.apache.org/documentation.html#producerconfigs">here</a>.
<p>
Note: after creating a <code>KafkaProducer</code> you must always <a href="#close()"><code>close()</code></a> it to avoid resource leaks.</div>
<dl class="notes">
<dt>Parameters:</dt>
<dd><code>properties</code> - The producer configs</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="&lt;init&gt;(java.util.Properties,org.apache.kafka.common.serialization.Serializer,org.apache.kafka.common.serialization.Serializer)">
<h3>KafkaProducer</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="element-name">KafkaProducer</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Properties.html" title="class or interface in java.util" class="external-link">Properties</a>&nbsp;properties,
<a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization">Serializer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>&gt;&nbsp;keySerializer,
<a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization">Serializer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;&nbsp;valueSerializer)</span></div>
<div class="block">A producer is instantiated by providing a set of key-value pairs as configuration, a key and a value <a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization"><code>Serializer</code></a>.
Valid configuration strings are documented <a href="http://kafka.apache.org/documentation.html#producerconfigs">here</a>.
<p>
Note: after creating a <code>KafkaProducer</code> you must always <a href="#close()"><code>close()</code></a> it to avoid resource leaks.</div>
<dl class="notes">
<dt>Parameters:</dt>
<dd><code>properties</code> - The producer configs</dd>
<dd><code>keySerializer</code> - The serializer for key that implements <a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization"><code>Serializer</code></a>. The configure() method won't be
called in the producer when the serializer is passed in directly.</dd>
<dd><code>valueSerializer</code> - The serializer for value that implements <a href="../../common/serialization/Serializer.html" title="interface in org.apache.kafka.common.serialization"><code>Serializer</code></a>. The configure() method won't
be called in the producer when the serializer is passed in directly.</dd>
</dl>
</section>
</li>
</ul>
</section>
</li>
<!-- ============ METHOD DETAIL ========== -->
<li>
<section class="method-details" id="method-detail">
<h2>Method Details</h2>
<ul class="member-list">
<li>
<section class="detail" id="initTransactions()">
<h3>initTransactions</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">initTransactions</span>()</div>
<div class="block">Needs to be called before any other methods when the <code>transactional.id</code> is set in the configuration.
This method does the following:
<ol>
<li>Ensures any transactions initiated by previous instances of the producer with the same
<code>transactional.id</code> are completed. If the previous instance had failed with a transaction in
progress, it will be aborted. If the last transaction had begun completion,
but not yet finished, this method awaits its completion.</li>
<li>Gets the internal producer id and epoch, used in all future transactional
messages issued by the producer.</li>
</ol>
Note that this method will raise <a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors"><code>TimeoutException</code></a> if the transactional state cannot
be initialized before expiration of <code>max.block.ms</code>. Additionally, it will raise <a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors"><code>InterruptException</code></a>
if interrupted. It is safe to retry in either case, but once the transactional state has been successfully
initialized, this method should no longer be used.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#initTransactions()">initTransactions</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalStateException.html" title="class or interface in java.lang" class="external-link">IllegalStateException</a></code> - if no <code>transactional.id</code> has been configured</dd>
<dd><code><a href="../../common/errors/UnsupportedVersionException.html" title="class in org.apache.kafka.common.errors">UnsupportedVersionException</a></code> - fatal error indicating the broker
does not support transactions (i.e. if its version is lower than 0.11.0.0)</dd>
<dd><code><a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors">AuthorizationException</a></code> - error indicating that the configured
transactional.id is not authorized, or the idempotent producer id is unavailable. See the exception for
more details. User may retry this function call after fixing the permission.</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - if the producer has encountered a previous fatal error or for any other unexpected error</dd>
<dd><code><a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors">TimeoutException</a></code> - if the time taken for initialize the transaction has surpassed <code>max.block.ms</code>.</dd>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - if the thread is interrupted while blocked</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="beginTransaction()">
<h3>beginTransaction</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">beginTransaction</span>()
throws <span class="exceptions"><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></span></div>
<div class="block">Should be called before the start of each new transaction. Note that prior to the first invocation
of this method, you must invoke <a href="#initTransactions()"><code>initTransactions()</code></a> exactly one time.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#beginTransaction()">beginTransaction</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalStateException.html" title="class or interface in java.lang" class="external-link">IllegalStateException</a></code> - if no <code>transactional.id</code> has been configured or if <a href="#initTransactions()"><code>initTransactions()</code></a>
has not yet been invoked</dd>
<dd><code><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></code> - if another producer with the same transactional.id is active</dd>
<dd><code><a href="../../common/errors/InvalidProducerEpochException.html" title="class in org.apache.kafka.common.errors">InvalidProducerEpochException</a></code> - if the producer has attempted to produce with an old epoch
to the partition leader. See the exception for more details</dd>
<dd><code><a href="../../common/errors/UnsupportedVersionException.html" title="class in org.apache.kafka.common.errors">UnsupportedVersionException</a></code> - fatal error indicating the broker
does not support transactions (i.e. if its version is lower than 0.11.0.0)</dd>
<dd><code><a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors">AuthorizationException</a></code> - fatal error indicating that the configured
<code>transactional.id</code> is not authorized. See the exception for more details</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - if the producer has encountered a previous fatal error or for any other unexpected error</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="sendOffsetsToTransaction(java.util.Map,java.lang.String)">
<h3>sendOffsetsToTransaction</h3>
<div class="member-signature"><span class="annotations"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Deprecated.html" title="class or interface in java.lang" class="external-link">@Deprecated</a>
</span><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">sendOffsetsToTransaction</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="../../common/TopicPartition.html" title="class in org.apache.kafka.common">TopicPartition</a>,<wbr><a href="../consumer/OffsetAndMetadata.html" title="class in org.apache.kafka.clients.consumer">OffsetAndMetadata</a>&gt;&nbsp;offsets,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a>&nbsp;consumerGroupId)</span>
throws <span class="exceptions"><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></span></div>
<div class="deprecation-block"><span class="deprecated-label">Deprecated.</span>
<div class="deprecation-comment">Since 3.0.0, please use <a href="#sendOffsetsToTransaction(java.util.Map,org.apache.kafka.clients.consumer.ConsumerGroupMetadata)"><code>sendOffsetsToTransaction(Map, ConsumerGroupMetadata)</code></a> instead.</div>
</div>
<div class="block">Sends a list of specified offsets to the consumer group coordinator, and also marks
those offsets as part of the current transaction. These offsets will be considered
committed only if the transaction is committed successfully. The committed offset should
be the next message your application will consume, i.e. lastProcessedMessageOffset + 1.
<p>
This method should be used when you need to batch consumed and produced messages
together, typically in a consume-transform-produce pattern. Thus, the specified
<code>consumerGroupId</code> should be the same as config parameter <code>group.id</code> of the used
<a href="../consumer/KafkaConsumer.html" title="class in org.apache.kafka.clients.consumer"><code>consumer</code></a>. Note, that the consumer should have <code>enable.auto.commit=false</code>
and should also not commit offsets manually (via <a href="../consumer/KafkaConsumer.html#commitSync(java.util.Map)"><code>sync</code></a> or
<a href="../consumer/KafkaConsumer.html#commitAsync(java.util.Map,org.apache.kafka.clients.consumer.OffsetCommitCallback)"><code>async</code></a> commits).
<p>
This method is a blocking call that waits until the request has been received and acknowledged by the consumer group
coordinator; but the offsets are not considered as committed until the transaction itself is successfully committed later (via
the <a href="#commitTransaction()"><code>commitTransaction()</code></a> call).</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#sendOffsetsToTransaction(java.util.Map,java.lang.String)">sendOffsetsToTransaction</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalStateException.html" title="class or interface in java.lang" class="external-link">IllegalStateException</a></code> - if no transactional.id has been configured, no transaction has been started</dd>
<dd><code><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></code> - fatal error indicating another producer with the same transactional.id is active</dd>
<dd><code><a href="../../common/errors/UnsupportedVersionException.html" title="class in org.apache.kafka.common.errors">UnsupportedVersionException</a></code> - fatal error indicating the broker
does not support transactions (i.e. if its version is lower than 0.11.0.0)</dd>
<dd><code><a href="../../common/errors/UnsupportedForMessageFormatException.html" title="class in org.apache.kafka.common.errors">UnsupportedForMessageFormatException</a></code> - fatal error indicating the message
format used for the offsets topic on the broker does not support transactions</dd>
<dd><code><a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors">AuthorizationException</a></code> - fatal error indicating that the configured
transactional.id is not authorized, or the consumer group id is not authorized.</dd>
<dd><code><a href="../../common/errors/InvalidProducerEpochException.html" title="class in org.apache.kafka.common.errors">InvalidProducerEpochException</a></code> - if the producer has attempted to produce with an old epoch
to the partition leader. See the exception for more details</dd>
<dd><code><a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors">TimeoutException</a></code> - if the time taken for sending the offsets has surpassed <code>max.block.ms</code>.</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - if the producer has encountered a previous fatal or abortable error, or for any
other unexpected error</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="sendOffsetsToTransaction(java.util.Map,org.apache.kafka.clients.consumer.ConsumerGroupMetadata)">
<h3>sendOffsetsToTransaction</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">sendOffsetsToTransaction</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="../../common/TopicPartition.html" title="class in org.apache.kafka.common">TopicPartition</a>,<wbr><a href="../consumer/OffsetAndMetadata.html" title="class in org.apache.kafka.clients.consumer">OffsetAndMetadata</a>&gt;&nbsp;offsets,
<a href="../consumer/ConsumerGroupMetadata.html" title="class in org.apache.kafka.clients.consumer">ConsumerGroupMetadata</a>&nbsp;groupMetadata)</span>
throws <span class="exceptions"><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></span></div>
<div class="block">Sends a list of specified offsets to the consumer group coordinator, and also marks
those offsets as part of the current transaction. These offsets will be considered
committed only if the transaction is committed successfully. The committed offset should
be the next message your application will consume, i.e. lastProcessedMessageOffset + 1.
<p>
This method should be used when you need to batch consumed and produced messages
together, typically in a consume-transform-produce pattern. Thus, the specified
<code>groupMetadata</code> should be extracted from the used <a href="../consumer/KafkaConsumer.html" title="class in org.apache.kafka.clients.consumer"><code>consumer</code></a> via
<a href="../consumer/KafkaConsumer.html#groupMetadata()"><code>KafkaConsumer.groupMetadata()</code></a> to leverage consumer group metadata. This will provide
stronger fencing than just supplying the <code>consumerGroupId</code> and passing in <code>new ConsumerGroupMetadata(consumerGroupId)</code>,
however note that the full set of consumer group metadata returned by <a href="../consumer/KafkaConsumer.html#groupMetadata()"><code>KafkaConsumer.groupMetadata()</code></a>
requires the brokers to be on version 2.5 or newer to understand.
<p>
This method is a blocking call that waits until the request has been received and acknowledged by the consumer group
coordinator; but the offsets are not considered as committed until the transaction itself is successfully committed later (via
the <a href="#commitTransaction()"><code>commitTransaction()</code></a> call).
<p>
Note, that the consumer should have <code>enable.auto.commit=false</code> and should
also not commit offsets manually (via <a href="../consumer/KafkaConsumer.html#commitSync(java.util.Map)"><code>sync</code></a> or
<a href="../consumer/KafkaConsumer.html#commitAsync(java.util.Map,org.apache.kafka.clients.consumer.OffsetCommitCallback)"><code>async</code></a> commits).
This method will raise <a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors"><code>TimeoutException</code></a> if the producer cannot send offsets before expiration of <code>max.block.ms</code>.
Additionally, it will raise <a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors"><code>InterruptException</code></a> if interrupted.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#sendOffsetsToTransaction(java.util.Map,org.apache.kafka.clients.consumer.ConsumerGroupMetadata)">sendOffsetsToTransaction</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalStateException.html" title="class or interface in java.lang" class="external-link">IllegalStateException</a></code> - if no transactional.id has been configured or no transaction has been started.</dd>
<dd><code><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></code> - fatal error indicating another producer with the same transactional.id is active</dd>
<dd><code><a href="../../common/errors/UnsupportedVersionException.html" title="class in org.apache.kafka.common.errors">UnsupportedVersionException</a></code> - fatal error indicating the broker
does not support transactions (i.e. if its version is lower than 0.11.0.0) or
the broker doesn't support the latest version of transactional API with all consumer group metadata
(i.e. if its version is lower than 2.5.0).</dd>
<dd><code><a href="../../common/errors/UnsupportedForMessageFormatException.html" title="class in org.apache.kafka.common.errors">UnsupportedForMessageFormatException</a></code> - fatal error indicating the message
format used for the offsets topic on the broker does not support transactions</dd>
<dd><code><a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors">AuthorizationException</a></code> - fatal error indicating that the configured
transactional.id is not authorized, or the consumer group id is not authorized.</dd>
<dd><code><a href="../consumer/CommitFailedException.html" title="class in org.apache.kafka.clients.consumer">CommitFailedException</a></code> - if the commit failed and cannot be retried
(e.g. if the consumer has been kicked out of the group). Users should handle this by aborting the transaction.</dd>
<dd><code><a href="../../common/errors/FencedInstanceIdException.html" title="class in org.apache.kafka.common.errors">FencedInstanceIdException</a></code> - if this producer instance gets fenced by broker due to a
mis-configured consumer instance id within group metadata.</dd>
<dd><code><a href="../../common/errors/InvalidProducerEpochException.html" title="class in org.apache.kafka.common.errors">InvalidProducerEpochException</a></code> - if the producer has attempted to produce with an old epoch
to the partition leader. See the exception for more details</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - if the producer has encountered a previous fatal or abortable error, or for any
other unexpected error</dd>
<dd><code><a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors">TimeoutException</a></code> - if the time taken for sending the offsets has surpassed <code>max.block.ms</code>.</dd>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - if the thread is interrupted while blocked</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="commitTransaction()">
<h3>commitTransaction</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">commitTransaction</span>()
throws <span class="exceptions"><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></span></div>
<div class="block">Commits the ongoing transaction. This method will flush any unsent records before actually committing the transaction.
<p>
Further, if any of the <a href="#send(org.apache.kafka.clients.producer.ProducerRecord)"><code>send(ProducerRecord)</code></a> calls which were part of the transaction hit irrecoverable
errors, this method will throw the last received exception immediately and the transaction will not be committed.
So all <a href="#send(org.apache.kafka.clients.producer.ProducerRecord)"><code>send(ProducerRecord)</code></a> calls in a transaction must succeed in order for this method to succeed.
<p>
If the transaction is committed successfully and this method returns without throwing an exception, it is guaranteed
that all <a href="Callback.html" title="interface in org.apache.kafka.clients.producer"><code>callbacks</code></a> for records in the transaction will have been invoked and completed.
Note that exceptions thrown by callbacks are ignored; the producer proceeds to commit the transaction in any case.
<p>
Note that this method will raise <a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors"><code>TimeoutException</code></a> if the transaction cannot be committed before expiration
of <code>max.block.ms</code>, but this does not mean the request did not actually reach the broker. In fact, it only indicates
that we cannot get the acknowledgement response in time, so it's up to the application's logic
to decide how to handle timeouts.
Additionally, it will raise <a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors"><code>InterruptException</code></a> if interrupted.
It is safe to retry in either case, but it is not possible to attempt a different operation (such as abortTransaction)
since the commit may already be in the progress of completing. If not retrying, the only option is to close the producer.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#commitTransaction()">commitTransaction</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalStateException.html" title="class or interface in java.lang" class="external-link">IllegalStateException</a></code> - if no transactional.id has been configured or no transaction has been started</dd>
<dd><code><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></code> - fatal error indicating another producer with the same transactional.id is active</dd>
<dd><code><a href="../../common/errors/UnsupportedVersionException.html" title="class in org.apache.kafka.common.errors">UnsupportedVersionException</a></code> - fatal error indicating the broker
does not support transactions (i.e. if its version is lower than 0.11.0.0)</dd>
<dd><code><a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors">AuthorizationException</a></code> - fatal error indicating that the configured
transactional.id is not authorized. See the exception for more details</dd>
<dd><code><a href="../../common/errors/InvalidProducerEpochException.html" title="class in org.apache.kafka.common.errors">InvalidProducerEpochException</a></code> - if the producer has attempted to produce with an old epoch
to the partition leader. See the exception for more details</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - if the producer has encountered a previous fatal or abortable error, or for any
other unexpected error</dd>
<dd><code><a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors">TimeoutException</a></code> - if the time taken for committing the transaction has surpassed <code>max.block.ms</code>.</dd>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - if the thread is interrupted while blocked</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="abortTransaction()">
<h3>abortTransaction</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">abortTransaction</span>()
throws <span class="exceptions"><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></span></div>
<div class="block">Aborts the ongoing transaction. Any unflushed produce messages will be aborted when this call is made.
This call will throw an exception immediately if any prior <a href="#send(org.apache.kafka.clients.producer.ProducerRecord)"><code>send(ProducerRecord)</code></a> calls failed with a
<a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors"><code>ProducerFencedException</code></a> or an instance of <a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors"><code>AuthorizationException</code></a>.
<p>
Note that this method will raise <a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors"><code>TimeoutException</code></a> if the transaction cannot be aborted before expiration
of <code>max.block.ms</code>, but this does not mean the request did not actually reach the broker. In fact, it only indicates
that we cannot get the acknowledgement response in time, so it's up to the application's logic
to decide how to handle timeouts. Additionally, it will raise <a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors"><code>InterruptException</code></a> if interrupted.
It is safe to retry in either case, but it is not possible to attempt a different operation (such as <a href="#commitTransaction()"><code>commitTransaction()</code></a>)
since the abort may already be in the progress of completing. If not retrying, the only option is to close the producer.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#abortTransaction()">abortTransaction</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalStateException.html" title="class or interface in java.lang" class="external-link">IllegalStateException</a></code> - if no transactional.id has been configured or no transaction has been started</dd>
<dd><code><a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors">ProducerFencedException</a></code> - fatal error indicating another producer with the same transactional.id is active</dd>
<dd><code><a href="../../common/errors/InvalidProducerEpochException.html" title="class in org.apache.kafka.common.errors">InvalidProducerEpochException</a></code> - if the producer has attempted to produce with an old epoch
to the partition leader. See the exception for more details</dd>
<dd><code><a href="../../common/errors/UnsupportedVersionException.html" title="class in org.apache.kafka.common.errors">UnsupportedVersionException</a></code> - fatal error indicating the broker
does not support transactions (i.e. if its version is lower than 0.11.0.0)</dd>
<dd><code><a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors">AuthorizationException</a></code> - fatal error indicating that the configured
transactional.id is not authorized. See the exception for more details</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - if the producer has encountered a previous fatal error or for any other unexpected error</dd>
<dd><code><a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors">TimeoutException</a></code> - if the time taken for aborting the transaction has surpassed <code>max.block.ms</code>.</dd>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - if the thread is interrupted while blocked</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="send(org.apache.kafka.clients.producer.ProducerRecord)">
<h3>send</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Future.html" title="class or interface in java.util.concurrent" class="external-link">Future</a>&lt;<a href="RecordMetadata.html" title="class in org.apache.kafka.clients.producer">RecordMetadata</a>&gt;</span>&nbsp;<span class="element-name">send</span><wbr><span class="parameters">(<a href="ProducerRecord.html" title="class in org.apache.kafka.clients.producer">ProducerRecord</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;&nbsp;record)</span></div>
<div class="block">Asynchronously send a record to a topic. Equivalent to <code>send(record, null)</code>.
See <a href="#send(org.apache.kafka.clients.producer.ProducerRecord,org.apache.kafka.clients.producer.Callback)"><code>send(ProducerRecord, Callback)</code></a> for details.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#send(org.apache.kafka.clients.producer.ProducerRecord)">send</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="send(org.apache.kafka.clients.producer.ProducerRecord,org.apache.kafka.clients.producer.Callback)">
<h3>send</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Future.html" title="class or interface in java.util.concurrent" class="external-link">Future</a>&lt;<a href="RecordMetadata.html" title="class in org.apache.kafka.clients.producer">RecordMetadata</a>&gt;</span>&nbsp;<span class="element-name">send</span><wbr><span class="parameters">(<a href="ProducerRecord.html" title="class in org.apache.kafka.clients.producer">ProducerRecord</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;&nbsp;record,
<a href="Callback.html" title="interface in org.apache.kafka.clients.producer">Callback</a>&nbsp;callback)</span></div>
<div class="block">Asynchronously send a record to a topic and invoke the provided callback when the send has been acknowledged.
<p>
The send is asynchronous and this method will return immediately once the record has been stored in the buffer of
records waiting to be sent. This allows sending many records in parallel without blocking to wait for the
response after each one.
<p>
The result of the send is a <a href="RecordMetadata.html" title="class in org.apache.kafka.clients.producer"><code>RecordMetadata</code></a> specifying the partition the record was sent to, the offset
it was assigned and the timestamp of the record. If the producer is configured with acks = 0, the <a href="RecordMetadata.html" title="class in org.apache.kafka.clients.producer"><code>RecordMetadata</code></a>
will have offset = -1 because the producer does not wait for the acknowledgement from the broker.
If <code>CreateTime</code> is used by the topic, the timestamp
will be the user provided timestamp or the record send time if the user did not specify a timestamp for the
record. If <code>LogAppendTime</code> is used for the
topic, the timestamp will be the Kafka broker local time when the message is appended.
<p>
Since the send call is asynchronous it returns a <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Future.html" title="class or interface in java.util.concurrent" class="external-link"><code>Future</code></a> for the
<a href="RecordMetadata.html" title="class in org.apache.kafka.clients.producer"><code>RecordMetadata</code></a> that will be assigned to this record. Invoking <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Future.html#get()" title="class or interface in java.util.concurrent" class="external-link"><code>get()</code></a> on this future will block until the associated request completes and then return the metadata for the record
or throw any exception that occurred while sending the record.
<p>
If you want to simulate a simple blocking call you can call the <code>get()</code> method immediately:
<pre>
<code>
byte[] key = "key".getBytes();
byte[] value = "value".getBytes();
ProducerRecord&lt;byte[],byte[]&gt; record = new ProducerRecord&lt;byte[],byte[]&gt;("my-topic", key, value)
producer.send(record).get();
</code></pre>
<p>
Fully non-blocking usage can make use of the <a href="Callback.html" title="interface in org.apache.kafka.clients.producer"><code>Callback</code></a> parameter to provide a callback that
will be invoked when the request is complete.
<pre>
<code>
ProducerRecord&lt;byte[],byte[]&gt; record = new ProducerRecord&lt;byte[],byte[]&gt;("the-topic", key, value);
producer.send(myRecord,
new Callback() {
public void onCompletion(RecordMetadata metadata, Exception e) {
if(e != null) {
e.printStackTrace();
} else {
System.out.println("The offset of the record we just sent is: " + metadata.offset());
}
}
});
</code>
</pre>
Callbacks for records being sent to the same partition are guaranteed to execute in order. That is, in the
following example <code>callback1</code> is guaranteed to execute before <code>callback2</code>:
<pre>
<code>
producer.send(new ProducerRecord&lt;byte[],byte[]&gt;(topic, partition, key1, value1), callback1);
producer.send(new ProducerRecord&lt;byte[],byte[]&gt;(topic, partition, key2, value2), callback2);
</code>
</pre>
<p>
When used as part of a transaction, it is not necessary to define a callback or check the result of the future
in order to detect errors from <code>send</code>. If any of the send calls failed with an irrecoverable error,
the final <a href="#commitTransaction()"><code>commitTransaction()</code></a> call will fail and throw the exception from the last failed send. When
this happens, your application should call <a href="#abortTransaction()"><code>abortTransaction()</code></a> to reset the state and continue to send
data.
</p>
<p>
Some transactional send errors cannot be resolved with a call to <a href="#abortTransaction()"><code>abortTransaction()</code></a>. In particular,
if a transactional send finishes with a <a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors"><code>ProducerFencedException</code></a>, a <a href="../../common/errors/OutOfOrderSequenceException.html" title="class in org.apache.kafka.common.errors"><code>OutOfOrderSequenceException</code></a>,
a <a href="../../common/errors/UnsupportedVersionException.html" title="class in org.apache.kafka.common.errors"><code>UnsupportedVersionException</code></a>, or an
<a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors"><code>AuthorizationException</code></a>, then the only option left is to call <a href="#close()"><code>close()</code></a>.
Fatal errors cause the producer to enter a defunct state in which future API calls will continue to raise
the same underlying error wrapped in a new <a href="../../common/KafkaException.html" title="class in org.apache.kafka.common"><code>KafkaException</code></a>.
</p>
<p>
It is a similar picture when idempotence is enabled, but no <code>transactional.id</code> has been configured.
In this case, <a href="../../common/errors/UnsupportedVersionException.html" title="class in org.apache.kafka.common.errors"><code>UnsupportedVersionException</code></a> and
<a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors"><code>AuthorizationException</code></a> are considered fatal errors. However,
<a href="../../common/errors/ProducerFencedException.html" title="class in org.apache.kafka.common.errors"><code>ProducerFencedException</code></a> does not need to be handled. Additionally, it is possible to continue
sending after receiving an <a href="../../common/errors/OutOfOrderSequenceException.html" title="class in org.apache.kafka.common.errors"><code>OutOfOrderSequenceException</code></a>, but doing so
can result in out of order delivery of pending messages. To ensure proper ordering, you should close the
producer and create a new instance.
</p>
<p>
If the message format of the destination topic is not upgraded to 0.11.0.0, idempotent and transactional
produce requests will fail with an <a href="../../common/errors/UnsupportedForMessageFormatException.html" title="class in org.apache.kafka.common.errors"><code>UnsupportedForMessageFormatException</code></a>
error. If this is encountered during a transaction, it is possible to abort and continue. But note that future
sends to the same topic will continue receiving the same exception until the topic is upgraded.
</p>
<p>
Note that callbacks will generally execute in the I/O thread of the producer and so should be reasonably fast or
they will delay the sending of messages from other threads. If you want to execute blocking or computationally
expensive callbacks it is recommended to use your own <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Executor.html" title="class or interface in java.util.concurrent" class="external-link"><code>Executor</code></a> in the callback body
to parallelize processing.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#send(org.apache.kafka.clients.producer.ProducerRecord,org.apache.kafka.clients.producer.Callback)">send</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Parameters:</dt>
<dd><code>record</code> - The record to send</dd>
<dd><code>callback</code> - A user-supplied callback to execute when the record has been acknowledged by the server (null
indicates no callback)</dd>
<dt>Throws:</dt>
<dd><code><a href="../../common/errors/AuthenticationException.html" title="class in org.apache.kafka.common.errors">AuthenticationException</a></code> - if authentication fails. See the exception for more details</dd>
<dd><code><a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors">AuthorizationException</a></code> - fatal error indicating that the producer is not allowed to write</dd>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalStateException.html" title="class or interface in java.lang" class="external-link">IllegalStateException</a></code> - if a transactional.id has been configured and no transaction has been started, or
when send is invoked after producer has been closed.</dd>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - If the thread is interrupted while blocked</dd>
<dd><code><a href="../../common/errors/SerializationException.html" title="class in org.apache.kafka.common.errors">SerializationException</a></code> - If the key or value are not valid objects given the configured serializers</dd>
<dd><code><a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors">TimeoutException</a></code> - If the record could not be appended to the send buffer due to memory unavailable
or missing metadata within <code>max.block.ms</code>.</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - If a Kafka related error occurs that does not belong to the public API exceptions.</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="flush()">
<h3>flush</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">flush</span>()</div>
<div class="block">Invoking this method makes all buffered records immediately available to send (even if <code>linger.ms</code> is
greater than 0) and blocks on the completion of the requests associated with these records. The post-condition
of <code>flush()</code> is that any previously sent record will have completed (e.g. <code>Future.isDone() == true</code>).
A request is considered completed when it is successfully acknowledged
according to the <code>acks</code> configuration you have specified or else it results in an error.
<p>
Other threads can continue sending records while one thread is blocked waiting for a flush call to complete,
however no guarantee is made about the completion of records sent after the flush call begins.
<p>
This method can be useful when consuming from some input system and producing into Kafka. The <code>flush()</code> call
gives a convenient way to ensure all previously sent messages have actually completed.
<p>
This example shows how to consume from one Kafka topic and produce to another Kafka topic:
<pre>
<code>
for(ConsumerRecord&lt;String, String&gt; record: consumer.poll(100))
producer.send(new ProducerRecord("my-topic", record.key(), record.value());
producer.flush();
consumer.commitSync();
</code>
</pre>
Note that the above example may drop records if the produce request fails. If we want to ensure that this does not occur
we need to set <code>retries=&lt;large_number&gt;</code> in our config.
</p>
<p>
Applications don't need to call this method for transactional producers, since the <a href="#commitTransaction()"><code>commitTransaction()</code></a> will
flush all buffered records before performing the commit. This ensures that all the <a href="#send(org.apache.kafka.clients.producer.ProducerRecord)"><code>send(ProducerRecord)</code></a>
calls made since the previous <a href="#beginTransaction()"><code>beginTransaction()</code></a> are completed before the commit.
</p></div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#flush()">flush</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - If the thread is interrupted while blocked</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="partitionsFor(java.lang.String)">
<h3>partitionsFor</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html" title="class or interface in java.util" class="external-link">List</a>&lt;<a href="../../common/PartitionInfo.html" title="class in org.apache.kafka.common">PartitionInfo</a>&gt;</span>&nbsp;<span class="element-name">partitionsFor</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html" title="class or interface in java.lang" class="external-link">String</a>&nbsp;topic)</span></div>
<div class="block">Get the partition metadata for the given topic. This can be used for custom partitioning.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#partitionsFor(java.lang.String)">partitionsFor</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="../../common/errors/AuthenticationException.html" title="class in org.apache.kafka.common.errors">AuthenticationException</a></code> - if authentication fails. See the exception for more details</dd>
<dd><code><a href="../../common/errors/AuthorizationException.html" title="class in org.apache.kafka.common.errors">AuthorizationException</a></code> - if not authorized to the specified topic. See the exception for more details</dd>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - if the thread is interrupted while blocked</dd>
<dd><code><a href="../../common/errors/TimeoutException.html" title="class in org.apache.kafka.common.errors">TimeoutException</a></code> - if metadata could not be refreshed within <code>max.block.ms</code></dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - for all Kafka-related exceptions, including the case where this method is called after producer close</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="metrics()">
<h3>metrics</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type"><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html" title="class or interface in java.util" class="external-link">Map</a>&lt;<a href="../../common/MetricName.html" title="class in org.apache.kafka.common">MetricName</a>,<wbr>? extends <a href="../../common/Metric.html" title="interface in org.apache.kafka.common">Metric</a>&gt;</span>&nbsp;<span class="element-name">metrics</span>()</div>
<div class="block">Get the full set of internal metrics maintained by the producer.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#metrics()">metrics</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="close()">
<h3>close</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">close</span>()</div>
<div class="block">Close this producer. This method blocks until all previously sent requests complete.
This method is equivalent to <code>close(Long.MAX_VALUE, TimeUnit.MILLISECONDS)</code>.
<p>
<strong>If close() is called from <a href="Callback.html" title="interface in org.apache.kafka.clients.producer"><code>Callback</code></a>, a warning message will be logged and close(0, TimeUnit.MILLISECONDS)
will be called instead. We do this because the sender thread would otherwise try to join itself and
block forever.</strong>
<p></div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/AutoCloseable.html#close()" title="class or interface in java.lang" class="external-link">close</a></code>&nbsp;in interface&nbsp;<code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/AutoCloseable.html" title="class or interface in java.lang" class="external-link">AutoCloseable</a></code></dd>
<dt>Specified by:</dt>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/Closeable.html#close()" title="class or interface in java.io" class="external-link">close</a></code>&nbsp;in interface&nbsp;<code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/Closeable.html" title="class or interface in java.io" class="external-link">Closeable</a></code></dd>
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#close()">close</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Throws:</dt>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - If the thread is interrupted while blocked.</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - If an unexpected error occurs while trying to close the client, this error should be treated
as fatal and indicate the client is no longer usable.</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="close(java.time.Duration)">
<h3>close</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">close</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/Duration.html" title="class or interface in java.time" class="external-link">Duration</a>&nbsp;timeout)</span></div>
<div class="block">This method waits up to <code>timeout</code> for the producer to complete the sending of all incomplete requests.
<p>
If the producer is unable to complete all requests before the timeout expires, this method will fail
any unsent and unacknowledged records immediately. It will also abort the ongoing transaction if it's not
already completing.
<p>
If invoked from within a <a href="Callback.html" title="interface in org.apache.kafka.clients.producer"><code>Callback</code></a> this method will not block and will be equivalent to
<code>close(Duration.ofMillis(0))</code>. This is done since no further sending will happen while
blocking the I/O thread of the producer.</div>
<dl class="notes">
<dt>Specified by:</dt>
<dd><code><a href="Producer.html#close(java.time.Duration)">close</a></code>&nbsp;in interface&nbsp;<code><a href="Producer.html" title="interface in org.apache.kafka.clients.producer">Producer</a>&lt;<a href="KafkaProducer.html" title="type parameter in KafkaProducer">K</a>,<wbr><a href="KafkaProducer.html" title="type parameter in KafkaProducer">V</a>&gt;</code></dd>
<dt>Parameters:</dt>
<dd><code>timeout</code> - The maximum time to wait for producer to complete any pending requests. The value should be
non-negative. Specifying a timeout of zero means do not wait for pending send requests to complete.</dd>
<dt>Throws:</dt>
<dd><code><a href="../../common/errors/InterruptException.html" title="class in org.apache.kafka.common.errors">InterruptException</a></code> - If the thread is interrupted while blocked.</dd>
<dd><code><a href="../../common/KafkaException.html" title="class in org.apache.kafka.common">KafkaException</a></code> - If an unexpected error occurs while trying to close the client, this error should be treated
as fatal and indicate the client is no longer usable.</dd>
<dd><code><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalArgumentException.html" title="class or interface in java.lang" class="external-link">IllegalArgumentException</a></code> - If the <code>timeout</code> is negative.</dd>
</dl>
</section>
</li>
</ul>
</section>
</li>
</ul>
</section>
<!-- ========= END OF CLASS DATA ========= -->
</main>
</div>
</div>
</body>
</html>