Example #1
0
 /// <summary>
 ///     The serializer to use to serialize values.
 /// </summary>
 public ProducerBuilder <TKey, TValue> SetValueSerializer(IAsyncSerializer <TValue> serializer)
 {
     if (this.ValueSerializer != null || this.AsyncValueSerializer != null)
     {
         throw new ArgumentException("Value serializer may not be specified more than once.");
     }
     this.AsyncValueSerializer = serializer;
     return(this);
 }
Example #2
0
 public ProducerFactory(
     ILogger logger,
     IAsyncSerializer <TValue> valueSerializer,
     IOptions <ProducerConfig> options)
 {
     _valueSerializer = valueSerializer ?? throw new ArgumentNullException(nameof(valueSerializer));
     _config          = options?.Value ?? throw new ArgumentNullException(nameof(options));
     _logger          = logger ?? throw new ArgumentNullException(nameof(logger));
 }
 /// <summary>
 ///     The serializer to use to serialize keys.
 /// </summary>
 /// <remarks>
 ///     If your key serializer throws an exception, this will be
 ///     wrapped in a ProduceException with ErrorCode
 ///     Local_KeySerialization and thrown by the initiating call to
 ///     Produce or ProduceAsync.
 /// </remarks>
 public ProducerBuilder <TKey, TValue> SetKeySerializer(IAsyncSerializer <TKey> serializer)
 {
     if (this.KeySerializer != null || this.AsyncKeySerializer != null)
     {
         throw new InvalidOperationException("Key serializer may not be specified more than once.");
     }
     this.AsyncKeySerializer = serializer;
     return(this);
 }
Example #4
0
        private void InitializeSerializers(
            Serializer <TKey> keySerializer,
            Serializer <TValue> valueSerializer,
            IAsyncSerializer <TKey> asyncKeySerializer,
            IAsyncSerializer <TValue> asyncValueSerializer)
        {
            // setup key serializer.
            if (keySerializer == null && asyncKeySerializer == null)
            {
                if (!defaultSerializers.TryGetValue(typeof(TKey), out object serializer))
                {
                    throw new ArgumentNullException(
                              $"Key serializer not specified and there is no default serializer defined for type {typeof(TKey).Name}.");
                }
                this.keySerializer = (Serializer <TKey>)serializer;
            }
            else if (keySerializer == null && asyncKeySerializer != null)
            {
                this.asyncKeySerializer = asyncKeySerializer;
            }
            else if (keySerializer != null && asyncKeySerializer == null)
            {
                this.keySerializer = keySerializer;
            }
            else
            {
                throw new InvalidOperationException("FATAL: Both async and sync key serializers were set.");
            }

            // setup value serializer.
            if (valueSerializer == null && asyncValueSerializer == null)
            {
                if (!defaultSerializers.TryGetValue(typeof(TValue), out object serializer))
                {
                    throw new ArgumentNullException(
                              $"Value serializer not specified and there is no default serializer defined for type {typeof(TKey).Name}.");
                }
                this.valueSerializer = (Serializer <TValue>)serializer;
            }
            else if (valueSerializer == null && asyncValueSerializer != null)
            {
                this.asyncValueSerializer = asyncValueSerializer;
            }
            else if (valueSerializer != null && asyncValueSerializer == null)
            {
                this.valueSerializer = valueSerializer;
            }
            else
            {
                throw new InvalidOperationException("FATAL: Both async and sync value serializers were set.");
            }
        }
Example #5
0
        private void Init(IAsyncSerializer <TKey> taskKeySerializer, IAsyncSerializer <TValue> taskValueSerializer)
        {
            this.taskKeySerializer   = taskKeySerializer;
            this.taskValueSerializer = taskValueSerializer;

            if (this.taskKeySerializer == null)
            {
                throw new ArgumentNullException("Key serializer must be specified.");
            }

            if (this.taskValueSerializer == null)
            {
                throw new ArgumentNullException("Value serializer must be specified.");
            }
        }
Example #6
0
        public KafkaProducer(
            ProducerConfig config,
            string avroSchema,
            ILogger logger)
        {
            this.logger = logger;
            var builder = new ProducerBuilder <TKey, TValue>(config);

            IAsyncSerializer <TValue> asyncValueSerializer = null;
            ISerializer <TValue>      valueSerializer      = null;
            IAsyncSerializer <TKey>   keySerializer        = null;

            if (!string.IsNullOrEmpty(avroSchema))
            {
                var schemaRegistry = new LocalSchemaRegistry(avroSchema);
                asyncValueSerializer = new AvroSerializer <TValue>(schemaRegistry);
            }
            else
            {
                if (typeof(Google.Protobuf.IMessage).IsAssignableFrom(typeof(TValue)))
                {
                    // protobuf: need to create using reflection due to generic requirements in ProtobufSerializer
                    valueSerializer = (ISerializer <TValue>)Activator.CreateInstance(typeof(ProtobufSerializer <>).MakeGenericType(typeof(TValue)));
                }
            }

            if (asyncValueSerializer != null)
            {
                builder.SetValueSerializer(asyncValueSerializer);
            }
            else if (valueSerializer != null)
            {
                builder.SetValueSerializer(valueSerializer);
            }

            if (keySerializer != null)
            {
                builder.SetKeySerializer(keySerializer);
            }

            this.producer = builder.Build();
        }
Example #7
0
 /// <summary>
 ///     Refer to <see cref="Confluent.Kafka.Producer{TKey,TValue}" />.
 /// </summary>
 public Producer(
     Handle handle,
     IAsyncSerializer <TKey> keySerializer,
     IAsyncSerializer <TValue> valueSerializer
     ) : base(handle) => Init(keySerializer, valueSerializer);
Example #8
0
 /// <summary>
 ///     Refer to <see cref="Confluent.Kafka.Producer{TKey,TValue}" />.
 /// </summary>
 public Producer(
     IEnumerable <KeyValuePair <string, string> > config,
     IAsyncSerializer <TKey> keySerializer,
     IAsyncSerializer <TValue> valueSerializer
     ) : base(config) => Init(keySerializer, valueSerializer);
Example #9
0
 /// <summary>
 ///     The async serializer to use to serialize keys.
 /// </summary>
 public DependentProducerBuilder <TKey, TValue> SetKeySerializer(IAsyncSerializer <TKey> serializer)
 {
     this.AsyncKeySerializer = serializer;
     return(this);
 }
 /// <summary>
 ///     Create a sync serializer by wrapping an async
 ///     one. For more information on the potential
 ///     pitfalls in doing this, refer to <see cref="Confluent.Kafka.SyncOverAsyncSerializer{T}" />.
 /// </summary>
 public static ISerializer <T> AsSyncOverAsync <T>(this IAsyncSerializer <T> asyncSerializer)
 => new SyncOverAsyncSerializer <T>(asyncSerializer);
 /// <summary>
 ///     Initializes a new SyncOverAsyncSerializer
 ///     instance.
 /// </summary>
 public SyncOverAsyncSerializer(IAsyncSerializer <T> asyncSerializer)
 {
     this.asyncSerializer = asyncSerializer;
 }
Example #12
0
 public InnerConfluentSerializerWrapper(Func <object> serializerFactory)
 {
     this.serializer = (IAsyncSerializer <T>)serializerFactory();
 }
 public IProducerBuilderWrapper <TKey, TValue> SetValueSerializer(IAsyncSerializer <TValue> serializer)
 {
     _dependentProducerBuilder.SetValueSerializer(serializer);
     return(this);
 }