title: Returning values from kafka consumer
body: <p>I am working on a scala application. I am using kafka in it. I am to consume message from kafka topic. Since I am writing a test case, I need to get record do some assertion to get pass my testcase. I am using following code to consume kafka message:</p>
  <pre><code>import java.util.{Collections, Properties}
  import java.util.regex.Pattern
  import org.apache.kafka.clients.consumer.KafkaConsumer
  import scala.collection.JavaConverters._
  object KafkaConsumerSubscribeApp extends App {
  
    val props:Properties = new Properties()
    props.put(&quot;group.id&quot;, &quot;test&quot;)
    props.put(&quot;bootstrap.servers&quot;,&quot;localhost:9092&quot;)
    props.put(&quot;key.deserializer&quot;,
        &quot;org.apache.kafka.common.serialization.StringDeserializer&quot;) 
    Props.put(&quot;value.deserializer&quot;,
        &quot;org.apache.kafka.common.serialization.StringDeserializer&quot;)
    props.put(&quot;enable.auto.commit&quot;, &quot;true&quot;)
    props.put(&quot;auto.commit.interval.ms&quot;, &quot;1000&quot;)
    val consumer = new KafkaConsumer(props)
    val topics = List(&quot;topic_text&quot;)
    try {
      consumer.subscribe(topics.asJava)
      while (true) {
        val records = consumer.poll(10)
        for (record &lt;- records.asScala) {
          println(&quot;Topic: &quot; + record.topic() + 
                   &quot;,Key: &quot; + record.key() +  
                   &quot;,Value: &quot; + record.value() +
                   &quot;, Offset: &quot; + record.offset() + 
                   &quot;, Partition: &quot; + record.partition())
        }
      }
    }catch{
      case e:Exception =&gt; e.printStackTrace()
    }finally {
      consumer.close()
    }
  }
  </code></pre>
  <p>There are two problems I am facing with this code. In intellij it is giving warning that poll method is deprecated. How can I modify this code for deprecation? Second problem is I want to this method to return the message which it gets from kafka topic. That message is in record.value(). How can I return it? With this code since while(true) is used, so it will be a endless loop and it will keep listening to messages from topic. How can I return record.value() from this method so that I can use data which I got from topic in other methods.</p>