Esempio n. 1
0
        private static async Task <IProducer <OrderKey, OrderEventRecord> > CreateProducer(
            ISchemaRegistryClient registryClient,
            AutomaticRegistrationBehavior automaticRegistrationBehavior)
        {
            var schemaBuilder = new SchemaBuilder(
                SchemaBuilder.CreateDefaultCaseBuilders()
                .Prepend(builder => new OrderEventUnionSchemaBuilderCase(builder)));

            using var serializerBuilder = new SchemaRegistrySerializerBuilder(
                      registryClient,
                      schemaBuilder,
                      serializerBuilder: new BinarySerializerBuilder(
                          BinarySerializerBuilder.CreateDefaultCaseBuilders()
                          .Prepend(builder => new OrderEventUnionSerializerBuilderCase(builder))));

            var producerBuilder = new ProducerBuilder <OrderKey, OrderEventRecord>(
                new ProducerConfig
            {
                BootstrapServers = BootstrapServers,
            });

            await producerBuilder.SetAvroKeySerializer(
                registryClient,
                SubjectNameStrategy.Topic.ConstructKeySubjectName(Topic),
                automaticRegistrationBehavior);

            await producerBuilder.SetAvroValueSerializer(
                serializerBuilder,
                SubjectNameStrategy.Topic.ConstructKeySubjectName(Topic),
                automaticRegistrationBehavior);

            return(producerBuilder.Build());
        }
 /// <summary>
 /// Set the message value serializer.
 /// </summary>
 /// <param name="producerBuilder">
 /// The <see cref="ProducerBuilder{TKey, TValue}" /> instance to be configured.
 /// </param>
 /// <param name="registryConfiguration">
 /// Schema Registry configuration. Using the <see cref="SchemaRegistryConfig" /> class is
 /// highly recommended.
 /// </param>
 /// <param name="id">
 /// The ID of the schema that should be used to serialize values.
 /// </param>
 public static async Task <ProducerBuilder <TKey, TValue> > SetAvroValueSerializer <TKey, TValue>(
     this ProducerBuilder <TKey, TValue> producerBuilder,
     IEnumerable <KeyValuePair <string, string> > registryConfiguration,
     int id
     )
 {
     using (var serializerBuilder = new SchemaRegistrySerializerBuilder(registryConfiguration))
     {
         return(await producerBuilder.SetAvroValueSerializer(serializerBuilder, id));
     }
 }
 /// <summary>
 /// Set the message value serializer.
 /// </summary>
 /// <param name="producerBuilder">
 /// The <see cref="ProducerBuilder{TKey, TValue}" /> instance to be configured.
 /// </param>
 /// <param name="registryClient">
 /// A client to use to resolve the schema. (The client will not be disposed.)
 /// </param>
 /// <param name="id">
 /// The ID of the schema that should be used to serialize values.
 /// </param>
 public static async Task <ProducerBuilder <TKey, TValue> > SetAvroValueSerializer <TKey, TValue>(
     this ProducerBuilder <TKey, TValue> producerBuilder,
     ISchemaRegistryClient registryClient,
     int id
     )
 {
     using (var serializerBuilder = new SchemaRegistrySerializerBuilder(registryClient))
     {
         return(await producerBuilder.SetAvroValueSerializer(serializerBuilder, id));
     }
 }
Esempio n. 4
0
 private void Start()
 {
     using (var registry = new CachedSchemaRegistryClient(registryConfig))
     {
         var typeResolver  = new ReflectionResolver(resolveReferenceTypesAsNullable: true);
         var schemaBuilder = new SchemaBuilder(typeResolver: typeResolver);
         using (var serializerBuilder = new SchemaRegistrySerializerBuilder(registry, schemaBuilder: schemaBuilder))
         {
             var  builder = new ProducerBuilder <TKey, TValue>(producerConfig);
             Task t       = builder.SetAvroValueSerializer(serializerBuilder, this.topic, registerAutomatically: AutomaticRegistrationBehavior.Always);
             while (!t.IsCompletedSuccessfully)
             {
                 ;
             }
             using (var producer = builder.Build())
             {
                 while (true)
                 {
                     if (KVPS.Count == 0)
                     {
                         Thread.Sleep(1);
                     }
                     else
                     {
                         lock (KVPS)
                         {
                             foreach (KeyValuePair <TKey, TValue> KVP in KVPS)
                             {
                                 try
                                 {
                                     Task p = producer.ProduceAsync(topic, new Message <TKey, TValue>
                                     {
                                         Key   = KVP.Key,
                                         Value = KVP.Value
                                     });
                                     while (!t.IsCompleted)
                                     {
                                         Console.WriteLine(p.Status);
                                     }
                                     Console.WriteLine(p.Status);
                                 }
                                 catch (Exception e)
                                 {
                                     System.Console.WriteLine(e);
                                 }
                             }
                             KVPS.Clear();
                         }
                     }
                 }
             }
         }
     }
 }
 /// <summary>
 /// Set the message value serializer.
 /// </summary>
 /// <param name="producerBuilder">
 /// The <see cref="ProducerBuilder{TKey, TValue}" /> instance to be configured.
 /// </param>
 /// <param name="registryConfiguration">
 /// Schema Registry configuration. Using the <see cref="SchemaRegistryConfig" /> class is
 /// highly recommended.
 /// </param>
 /// <param name="subject">
 /// The subject of the schema that should be used to serialize values. The latest version
 /// of the subject will be resolved.
 /// </param>
 /// <param name="registerAutomatically">
 /// Whether to automatically register a schema that matches <typeparamref name="TValue" />
 /// if one does not already exist.
 /// </param>
 public static async Task <ProducerBuilder <TKey, TValue> > SetAvroValueSerializer <TKey, TValue>(
     this ProducerBuilder <TKey, TValue> producerBuilder,
     IEnumerable <KeyValuePair <string, string> > registryConfiguration,
     string subject,
     bool registerAutomatically = false
     )
 {
     using (var serializerBuilder = new SchemaRegistrySerializerBuilder(registryConfiguration))
     {
         return(await producerBuilder.SetAvroValueSerializer(serializerBuilder, subject, registerAutomatically));
     }
 }
 /// <summary>
 /// Set the message value serializer.
 /// </summary>
 /// <param name="producerBuilder">
 /// The <see cref="ProducerBuilder{TKey, TValue}" /> instance to be configured.
 /// </param>
 /// <param name="registryClient">
 /// A client to use to resolve the schema. (The client will not be disposed.)
 /// </param>
 /// <param name="subject">
 /// The subject of the schema that should be used to serialize values. The latest version
 /// of the subject will be resolved.
 /// </param>
 /// <param name="registerAutomatically">
 /// Whether to automatically register a schema that matches <typeparamref name="TValue" />
 /// if one does not already exist.
 /// </param>
 public static async Task <ProducerBuilder <TKey, TValue> > SetAvroValueSerializer <TKey, TValue>(
     this ProducerBuilder <TKey, TValue> producerBuilder,
     ISchemaRegistryClient registryClient,
     string subject,
     bool registerAutomatically = false
     )
 {
     using (var serializerBuilder = new SchemaRegistrySerializerBuilder(registryClient))
     {
         return(await producerBuilder.SetAvroValueSerializer(serializerBuilder, subject, registerAutomatically));
     }
 }