You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@kafka.apache.org by Rajiv Kurian <ra...@signalfuse.com> on 2014/10/24 05:20:23 UTC

Thread safety of encoders

Are encoders only ever called from a single thread?

I have a stateful utility class that I use to encode my objects. Is it safe
to only create a single instance? Something like this:

MyObjectEncoder

public class MyObjectEncoder implements Encoder<MyObject> {

    private final MyObjectEncoderHelper helper =
new MyObjectEncoderHelper();

    public MyObjectEncoder(VerifiableProperties props) {}

    @Override

    public byte[] toBytes(MyObject myObject) {

        // Stateful. So can't be called concurrently.

        helper.encode(myObject);

        return helper.bytes();

    }

}

I am wondering if this is legal or if I have to create a new helper object
every time toBytes is called.


Thanks!

Re: Thread safety of encoders

Posted by Jun Rao <ju...@gmail.com>.
At this moment, the encoder is called from a single thread per producer
instance.

Thanks,

Jun

On Thu, Oct 23, 2014 at 8:20 PM, Rajiv Kurian <ra...@signalfuse.com> wrote:

> Are encoders only ever called from a single thread?
>
> I have a stateful utility class that I use to encode my objects. Is it safe
> to only create a single instance? Something like this:
>
> MyObjectEncoder
>
> public class MyObjectEncoder implements Encoder<MyObject> {
>
>     private final MyObjectEncoderHelper helper =
> new MyObjectEncoderHelper();
>
>     public MyObjectEncoder(VerifiableProperties props) {}
>
>     @Override
>
>     public byte[] toBytes(MyObject myObject) {
>
>         // Stateful. So can't be called concurrently.
>
>         helper.encode(myObject);
>
>         return helper.bytes();
>
>     }
>
> }
>
> I am wondering if this is legal or if I have to create a new helper object
> every time toBytes is called.
>
>
> Thanks!
>