Esempio n. 1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="settings">the settings dictionary, which follows
        /// the norm defined by Kafka settings.</param>
        /// <exception cref="ArgumentNullException">if any of the method parameters is null.</exception>
        /// <exception cref="ArgumentException">if any of the mandatory Kafka parameters is not set.</exception>
        public GojulMQKafkaMessageConsumer(Dictionary <string, string> settings)
        {
            Condition.Requires(settings, "settings").IsNotNull();
            Condition.Requires((string)settings[BootstrapServers], BootstrapServers)
            .IsNotNull()
            .IsNotEmpty();
            Condition.Requires((string)settings[GroupId], GroupId)
            .IsNotNull()
            .IsNotEmpty();
            Condition.Requires((string)settings[SchemaRegistryUrl], SchemaRegistryUrl)
            .IsNotNull()
            .IsNotEmpty();

            _disposed = false;

            _schemaRegistry = new CachedSchemaRegistryClient(settings);
            _consumer       = new ConsumerBuilder <string, T>(KafkaSettingsList.SanitizeConfiguration(settings))
                              .SetKeyDeserializer(Deserializers.Utf8)
                              .SetValueDeserializer(new AvroDeserializer <T>(_schemaRegistry).AsSyncOverAsync())
                              .SetErrorHandler((_, error) =>
            {
                _log.Error(string.Format("Error while processing message %s - Skipping this message !", error));
            })
                              .Build();
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="settings">the settings dictionary, which follows
        /// the norm defined by Kafka settings.</param>
        /// <exception cref="ArgumentNullException">if any of the method parameters is null.</exception>
        /// <exception cref="ArgumentException">if any of the mandatory Kafka parameters is not set.</exception>
        public GojulMQKafkaMessageProducer(Dictionary <string, string> settings)
        {
            Condition.Requires(settings, "settings").IsNotNull();
            Condition.Requires((string)settings[BootstrapServers], BootstrapServers)
            .IsNotNull()
            .IsNotEmpty();
            Condition.Requires((string)settings[ClientId], ClientId)
            .IsNotNull()
            .IsNotEmpty();
            Condition.Requires((string)settings[SchemaRegistryUrl], SchemaRegistryUrl)
            .IsNotNull()
            .IsNotEmpty();

            _disposed       = false;
            _schemaRegistry = new CachedSchemaRegistryClient(settings);
            _producer       = new ProducerBuilder <string, T>(KafkaSettingsList.SanitizeConfiguration(settings))
                              .SetKeySerializer(Serializers.Utf8)
                              .SetValueSerializer(new AvroSerializer <T>(_schemaRegistry).AsSyncOverAsync())
                              .Build();
        }