예제 #1
0
        private void PlainKeyValueProducer(string topic)
        {
            //var jsonSchem = AvroSchema<JournalEntry>.Of(typeof(JournalEntry));
            var jsonSchem = KeyValueSchema <string, string> .Of(ISchema <string> .String, ISchema <string> .String);

            var builder = new ConsumerConfigBuilder <KeyValue <string, string> >()
                          .Topic(topic)
                          .SubscriptionName($"subscriber-name-{DateTimeHelper.CurrentUnixTimeMillis()}")
                          .AckTimeout(TimeSpan.FromMilliseconds(20000))
                          .ForceTopicCreation(true)
                          .AcknowledgmentGroupTime(0);
            var consumer       = _client.NewConsumer(jsonSchem, builder);
            var producerConfig = new ProducerConfigBuilder <KeyValue <string, string> >()
                                 .ProducerName(topic.Split("/").Last())
                                 .Topic(topic)
                                 .Schema(jsonSchem)
                                 .SendTimeout(10000);

            var producer = _client.NewProducer(jsonSchem, producerConfig);

            for (var i = 0; i < 10; i++)
            {
                var metadata = new Dictionary <string, string>
                {
                    ["Key"]        = "Single",
                    ["Properties"] = JsonSerializer.Serialize(new Dictionary <string, string> {
                        { "Tick", DateTime.Now.Ticks.ToString() }
                    }, new JsonSerializerOptions {
                        WriteIndented = true
                    })
                };
                var id = producer.NewMessage().Properties(metadata).Value <string, string>(new KeyValue <string, string>("Ebere", $"[{i}]Ebere")).Send();
                _output.WriteLine(id.ToString());
            }
            Thread.Sleep(TimeSpan.FromSeconds(5));
            for (var i = 0; i < 10; i++)
            {
                var msg = consumer.Receive();
                if (msg != null)
                {
                    var kv = msg.Value;
                    _output.WriteLine($"key:{kv.Key}, value:{kv.Value}");
                }
            }
        }
예제 #2
0
 public static ISchema <KeyValue <K, V> > NewKeyValueSchema <K, V>(Type key, Type value, SchemaType type)
 {
     return(KeyValueSchema <K, V> .Of(key, value, type));
 }
예제 #3
0
 public static ISchema <KeyValue <K, V> > NewKeyValueSchema <K, V>(ISchema <K> keySchema, ISchema <V> valueSchema, KeyValueEncodingType keyValueEncodingType)
 {
     return(KeyValueSchema <K, V> .Of(keySchema, valueSchema, keyValueEncodingType));
 }
예제 #4
0
 public static ISchema <KeyValue <K, V> > NewKeyValueSchema <K, V>(ISchema <K> keySchema, ISchema <V> valueSchema)
 {
     //return catchExceptions(() => (Schema<KeyValue<K, V>>)getStaticMethod("org.apache.pulsar.client.impl.schema.KeyValueSchema", "of", typeof(Schema), typeof(Schema)).invoke(null, KeySchema, ValueSchema));
     return(KeyValueSchema <K, V> .Of(keySchema, valueSchema));
 }