/** * computes partition for given record. * if the record has partition returns the value otherwise * calls configured partitioner class to compute the partition. */ private int partition(ProducerRecord<K, V> record, byte[] serializedKey, byte[] serializedValue, Cluster cluster) { Integer partition = record.partition(); return partition != null ? partition : partitioner.partition( record.topic(), record.key(), serializedKey, record.value(), serializedValue, cluster); }
/** * Complete the earliest uncompleted call successfully. * * @return true if there was an uncompleted call to complete */ public synchronized boolean completeNext() { return errorNext(null); }
/** * Adds the record to the list of sent records. The {@link RecordMetadata} returned will be immediately satisfied. * * @see #history() */ @Override public synchronized Future<RecordMetadata> send(ProducerRecord<K, V> record) { return send(record, null); }
/** * Asynchronously send a record to a topic. Equivalent to <code>send(record, null)</code>. * See {@link #send(ProducerRecord, Callback)} for details. */ @Override public Future<RecordMetadata> send(ProducerRecord<K, V> record) { return send(record, null); }
@Override public void close() { producer.close(); } }
@Override public void flush() { producer.flush(); }
@Override protected void flush() { if (this.producer != null) { producer.flush(); } } }
@Test public void shouldThrowOnSendIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.send(null); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@Test public void shouldThrowOnCommitTransactionIfProducerGotFenced() { buildMockProducer(true); producer.initTransactions(); producer.fenceProducer(); try { producer.commitTransaction(); fail("Should have thrown as producer is fenced off"); } catch (ProducerFencedException e) { } }
@Test public void shouldThrowOnCommitTransactionIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.commitTransaction(); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@Test public void shouldThrowOnAbortTransactionIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.abortTransaction(); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
public void fenceProducerOnClose() { verifyProducerState(); verifyTransactionsInitialized(); this.producerFencedOnClose = true; }
@Test(expected = IllegalStateException.class) public void shouldThrowOnAbortIfTransactionsNotInitialized() { buildMockProducer(true); producer.abortTransaction(); }
@Test(expected = IllegalStateException.class) public void shouldThrowFenceProducerIfTransactionsNotInitialized() { buildMockProducer(true); producer.fenceProducer(); }
@Test(expected = IllegalStateException.class) public void shouldThrowOnCommitIfTransactionsNotInitialized() { buildMockProducer(true); producer.commitTransaction(); }
@Test(expected = IllegalStateException.class) public void shouldThrowOnBeginTransactionIfTransactionsNotInitialized() { buildMockProducer(true); producer.beginTransaction(); }
@Test(expected = IllegalStateException.class) public void shouldThrowOnSendOffsetsToTransactionIfTransactionsNotInitialized() { buildMockProducer(true); producer.sendOffsetsToTransaction(null, null); }
/** * 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} you must always {@link #close()} it to avoid resource leaks. * @param properties The producer configs */ public KafkaProducer(Properties properties) { this(propsToMap(properties), null, null, null, null, null, Time.SYSTEM); }
@Override public void beginTransaction() throws ProducerFencedException { verifyProducerState(); verifyTransactionsInitialized(); this.transactionInFlight = true; this.transactionCommitted = false; this.transactionAborted = false; this.sentOffsets = false; }
public synchronized void fenceProducer() { verifyProducerState(); verifyTransactionsInitialized(); this.producerFenced = true; }