public opcKafkaProducer(kafkaProducerConf conf, CachedSchemaRegistryClient schemaRegistry) { _conf = conf; // setup the logger log = LogManager.GetLogger(this.GetType().Name); // instance the List of schemas schemas = new opcSchemas(); // instace producer with Avro serializers producer = new ProducerBuilder <string, GenericRecord>(_conf.getProducerConf()) //.SetKeySerializer(new AvroSerializer<string>(schemaRegistry)) .SetValueSerializer(new AvroSerializer <GenericRecord>(schemaRegistry)) .SetErrorHandler((_, e) => log.Error($"Error: {e.Reason}")) .Build(); log.Info("Producer streaming to broker: " + _conf.BootstrapServers); log.Info("Data stream on topic: " + _conf.opcSystemName); }
public GenericRecord getGenericRecord(opcSchemas schemas) { var res = new GenericRecord(schemas.rpcResponse); var error = new GenericRecord(schemas.rpcError); if (result != null) { res.Add("result", result); res.Add("error", null); } // if there is an error then overrides the result if (error_code != -999 || error_message != null) { error.Add("message", error_message); error.Add("code", error_code); res.Add("error", error); res.Add("result", null); } res.Add("id", id); return(res); }
public opcKafkaRPC(kafkaRPCConf conf, CachedSchemaRegistryClient schemaRegistry) { _conf = conf; logger = LogManager.GetLogger(this.GetType().Name); _schemas = new opcSchemas(); // Override default GroupID if name exist if (conf.GroupId == "OPC") { conf.GroupId = conf.opcSystemName; } // build the consumer, and subscribe it to topic _consumer = new ConsumerBuilder <String, GenericRecord>(conf.getConsumerConf()) .SetValueDeserializer(new AvroDeserializer <GenericRecord>(schemaRegistry).AsSyncOverAsync()) .SetErrorHandler((_, e) => logger.Error($"Error: {e.Reason}")) .Build(); _consumer.Subscribe(conf.opcSystemName + "-request"); // build the producer for the responses var producer_conf = new ProducerConfig() { BootstrapServers = conf.BootstrapServers, LingerMs = 5 // low latency response }; _producer = new ProducerBuilder <String, GenericRecord>(producer_conf) .SetValueSerializer(new AvroSerializer <GenericRecord>(schemaRegistry)) .SetErrorHandler((_, e) => logger.Error($"Error: {e.Reason}")) .Build(); if (_conf.enableKafkaRPC) { logger.Info("Connected to broker: " + _conf.BootstrapServers); logger.Info("Request are to be made on topic: " + conf.opcSystemName + "-request"); logger.Info("Response will be on topic: " + _conf.opcSystemName + "-response"); } }