-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review feedback; DRY deferred producer creation
- Loading branch information
Showing
5 changed files
with
132 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
core/src/main/scala/akka/kafka/internal/DeferredProducer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (C) 2014 - 2016 Softwaremill <http://softwaremill.com> | ||
* Copyright (C) 2016 - 2019 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.kafka.internal | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
import akka.annotation.InternalApi | ||
import akka.dispatch.ExecutionContexts | ||
import akka.kafka.ProducerSettings | ||
import akka.stream.stage._ | ||
import org.apache.kafka.clients.producer.Producer | ||
|
||
import scala.util.control.NonFatal | ||
import scala.util.{Failure, Success} | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@InternalApi | ||
private[kafka] trait DeferredProducer[K, V] { | ||
self: GraphStageLogic with StageLogging => | ||
|
||
/** The Kafka producer may be created lazily, assigned via `preStart` in `assignProducer`. */ | ||
protected var producer: Producer[K, V] = _ | ||
|
||
protected def producerSettings: ProducerSettings[K, V] | ||
protected def producerAssigned(): Unit | ||
protected def closeAndFailStageCb: AsyncCallback[Throwable] | ||
|
||
private def assignProducer(p: Producer[K, V]): Unit = { | ||
producer = p | ||
producerAssigned() | ||
} | ||
|
||
final protected def resolveProducer(): Unit = { | ||
val producerFuture = producerSettings.createKafkaProducerAsync()(materializer.executionContext) | ||
producerFuture.value match { | ||
case Some(Success(p)) => assignProducer(p) | ||
case Some(Failure(e)) => failStage(e) | ||
case None => | ||
val assign = getAsyncCallback(assignProducer) | ||
producerFuture | ||
.transform( | ||
producer => assign.invoke(producer), | ||
e => { | ||
log.error(e, "producer creation failed") | ||
closeAndFailStageCb.invoke(e) | ||
e | ||
} | ||
)(ExecutionContexts.sameThreadExecutionContext) | ||
} | ||
} | ||
|
||
protected def closeProducerImmediately(): Unit = | ||
if (producer != null) { | ||
// Discard unsent ProducerRecords after encountering a send-failure in ProducerStage | ||
// https://github.com/akka/alpakka-kafka/pull/318 | ||
producer.close(0L, TimeUnit.MILLISECONDS) | ||
} | ||
|
||
protected def closeProducer(): Unit = | ||
if (producerSettings.closeProducerOnStop && producer != null) { | ||
try { | ||
// we do not have to check if producer was already closed in send-callback as `flush()` and `close()` are effectively no-ops in this case | ||
producer.flush() | ||
producer.close(producerSettings.closeTimeout.toMillis, TimeUnit.MILLISECONDS) | ||
log.debug("Producer closed") | ||
} catch { | ||
case NonFatal(ex) => log.error(ex, "Problem occurred during producer close") | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.