markusthoemmes commented on a change in pull request #3072: Enhance kafka 
message provider
URL: 
https://github.com/apache/incubator-openwhisk/pull/3072#discussion_r160154244
 
 

 ##########
 File path: 
common/scala/src/main/scala/whisk/connector/kafka/KafkaConsumerConnector.scala
 ##########
 @@ -21,46 +21,106 @@ import java.util.Properties
 
 import scala.collection.JavaConversions.iterableAsScalaIterable
 import scala.collection.JavaConversions.seqAsJavaList
-import scala.concurrent.duration.Duration
-import scala.concurrent.duration.DurationInt
-import scala.concurrent.duration.FiniteDuration
-
+import scala.concurrent.duration._
+import scala.concurrent.Future
 import org.apache.kafka.clients.consumer.ConsumerConfig
 import org.apache.kafka.clients.consumer.KafkaConsumer
 import org.apache.kafka.common.serialization.ByteArrayDeserializer
-
+import org.apache.kafka.common.errors.{InvalidMetadataException, 
WakeupException}
+import akka.actor.ActorSystem
+import org.apache.kafka.common.KafkaException
 import whisk.common.Logging
 import whisk.core.connector.MessageConsumer
 
 class KafkaConsumerConnector(kafkahost: String,
                              groupid: String,
                              topic: String,
+                             actorSystem: ActorSystem,
                              override val maxPeek: Int = Int.MaxValue,
                              readeos: Boolean = true,
                              sessionTimeout: FiniteDuration = 30.seconds,
                              autoCommitInterval: FiniteDuration = 10.seconds,
                              maxPollInterval: FiniteDuration = 
5.minutes)(implicit logging: Logging)
     extends MessageConsumer {
+  implicit val ec = actorSystem.dispatcher
 
   /**
    * Long poll for messages. Method returns once message are available but no 
later than given
    * duration.
    *
    * @param duration the maximum duration for the long poll
    */
-  override def peek(duration: Duration = 500.milliseconds) = {
-    val records = consumer.poll(duration.toMillis)
-    records map { r =>
-      (r.topic, r.partition, r.offset, r.value)
+  override def peek(duration: Duration = 500.milliseconds,
+                    retry: Int = 3): Iterable[(String, Int, Long, 
Array[Byte])] = {
+    // Since kafka client can be infinitely blocked in poll(timeout), we 
should interrupt it by wakeup() method
+    val wakeUpTask = actorSystem.scheduler.scheduleOnce(sessionTimeout) {
+      consumer.wakeup()
     }
+
+    try {
+      val records = consumer.poll(duration.toMillis)
+      records map { r =>
+        (r.topic, r.partition, r.offset, r.value)
+      }
 
 Review comment:
   FWIW: This can be 
   
   ```scala
   consumer.poll(duration.toMillis).map(r => (r.topic, r.partition, r.offset, 
r.value))
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to