You can of course use KafkaProducer<Object, Object> to get a producer interface that can accept a variety of types. For example, if you have an Avro serializer that accepts both primitive types (e.g. String, integer types) and complex types (e.g. records, arrays, maps), Object is the only type you can use to cover all of those. As long as your serializer supports it, you can use a general type and pass in a variety of types to a single producer.
The drawback is that you don't get feedback at compile time if you pass in a type that you weren't expecting. For example, if you know your keys are always going to be Strings, it's probably a good idea to use a KafkaProducer<String, Object> so that you catch a case where you accidentally pass in a different object. There are a lot of use cases where an application is only producing a single format of data, so supporting the type checking can be valuable. The type checking isn't going to be perfect because of type erasure and since serializers are often instantiated via reflection. However, having the type information can offer some compile-time protection to application code using the clients. -Ewen On Wed, May 13, 2015 at 10:03 AM, Mohit Gupta <success.mohit.gu...@gmail.com > wrote: > Hello, > > I've a question regarding the design of the new Producer API. > > As per the design (KafkaProducer<K,V>), it seems that a separate producer > is required for every combination of key and value type. Where as, in > documentation ( and elsewhere ) it's recommended to create a single > producer instance per application and share it among the all the threads > for best performance? > > One way to create only single producer would be to use byte[] as key/value > type and handle the serialization at the client itself, rather than the > producer, similar to the example in javadocs. But wouldn't this defeat the > purpose of using generics in the producer? > > Specific to our use case, we have multiple types of messages, where each > message type can have multiple custom serializers. And, a message can be > pushed into mulitple topics with different serialization. > > > -- > Best Regards, > > Mohit Gupta > -- Thanks, Ewen