You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@kafka.apache.org by S Ahmed <sa...@gmail.com> on 2014/06/16 18:12:45 UTC

kafka producer, one per web app?

In my web application, I should be creating a single instance of a producer
correct?

So in scala I should be doing something like:

object KafkaProducer {
  // props...
   val producer = new Producer[AnyRef, AnyRef](new ProducerConfig(props))
}

And then say in my QueueService I would do:

class QueueService {

def send(topic: String, message: Array[Byte], partition: Array[Byte]): Unit
= {
    try {
      KakfaProducer.producer.send(new KeyedMessage(topic,message,
partition))
    } catch {
      case e: Exception =>
        e.printStackTrace
        System.exit(1)
    }
  }

}

Threading wise, is this correct?

Re: kafka producer, one per web app?

Posted by Jay Kreps <ja...@gmail.com>.
Yes, the producer is thread safe, and sharing instances will be more
efficient if you are producing in async mode.

-Jay


On Mon, Jun 16, 2014 at 9:12 AM, S Ahmed <sa...@gmail.com> wrote:

> In my web application, I should be creating a single instance of a producer
> correct?
>
> So in scala I should be doing something like:
>
> object KafkaProducer {
>   // props...
>    val producer = new Producer[AnyRef, AnyRef](new ProducerConfig(props))
> }
>
> And then say in my QueueService I would do:
>
> class QueueService {
>
> def send(topic: String, message: Array[Byte], partition: Array[Byte]): Unit
> = {
>     try {
>       KakfaProducer.producer.send(new KeyedMessage(topic,message,
> partition))
>     } catch {
>       case e: Exception =>
>         e.printStackTrace
>         System.exit(1)
>     }
>   }
>
> }
>
> Threading wise, is this correct?
>