示例#1
0
        public virtual async Task UpsertDocument <TDocument>(IUpsertDocumentContext <TDocument> context) where TDocument : class
        {
            var topicName = context.Document.GetType().Name;
            var config    = this._producerConfigManager.GetConfiguration(x => (x.ConfigurationScope & ConfigurationScope.Producer) == ConfigurationScope.Producer);
            var options   = new KafkaOptions
                                (new Uri("http://localhost:9092"));
            var router = new BrokerRouter(options);

            var client = new KafkaNet.Producer(router);

            client.SendMessageAsync(topicName,
                                    new[] { new Message("!!!Test message sent from alternative .Net => kafka provider!!!") },
                                    -1,
                                    TimeSpan.FromSeconds(5))
            .Wait();

            //var valueSerialiser = new BinarySerializer<TDocument>();
            // var keySerialiser = new BinarySerializer<Guid>();
            //var deliveryHandler = new DeliveryHandler<Guid, TDocument>();
            //using (var producer = new Producer<Guid, TDocument>(config, keySerialiser, valueSerialiser))
            //{
            //    var deliveryReport = await producer.ProduceAsync(topicName, context.Id, context.Document);

            //    //producer.ProduceAsync(topicName, null, context.Document, deliveryHandler);
            //    //producer.Flush();
            //}
        }
        public virtual Task UpsertDocument <TDocument>(IUpsertDocumentContext <TDocument> context) where TDocument : class
        {
            var documentType = typeof(TDocument);
            var client       = this._clientFactory.GetClient();

            var documentPath = new DocumentPath <TDocument>(context.Id)
                               .Type(documentType);

            var index            = this.GetIndex(context.IndexContext);
            var updateDescriptor = new UpdateDescriptor <TDocument, dynamic>(documentPath)
                                   .Index(index);

            if (context.Script != null)
            {
                updateDescriptor.Script(d => d.Inline(context.Script)
                                        .Params(context.ScriptParams.ToDictionary(k => k.Key, v => v.Value)));
            }
            else
            {
                updateDescriptor.Upsert(context.Document)
                .Doc(context.PartialUpdate ?? context.Document);
            }

            var updateDocResponse = client.Update <TDocument, dynamic>(updateDescriptor);

            this._responseHandler.ValdateAndHandleException(updateDocResponse, true);
            return(Task.CompletedTask);
        }
        //public KafkaDispatcherMS(ProducerConfigManager producerConfigManager)
        //{
        //    this._producerConfigManager = producerConfigManager;
        //}

        public virtual async Task UpsertDocument <TDocument>(IUpsertDocumentContext <TDocument> context) where TDocument : class
        {
            try
            {
                var topicName    = context.Document.GetType().Name;
                var brokerConfig = new BrokerConfiguration()
                {
                    BrokerId = 0,
                    Host     = "localhost",
                    Port     = 9092
                };
                var config = new ProducerConfiguration(new List <BrokerConfiguration> {
                    brokerConfig
                });
                //var serializer = new BinarySerializer<TDocument>();
                var json          = this._serializer.Serialize(context.Document);
                var content       = Encoding.Default.GetBytes(json);
                var msg           = new Message(content);
                var kafkaProducer = new Producer(config);

                var batch = new ProducerData <string, Message>(topicName, msg);
                kafkaProducer.Send(batch);
            }
            catch (Exception e)
            {
            }
        }
        public virtual async Task UpsertDocument <TDocument>(IUpsertDocumentContext <TDocument> context) where TDocument : class
        {
            var topicName       = context.Document.GetType().Name;
            var config          = this._producerConfigManager.GetConfiguration(x => (x.ConfigurationScope & ConfigurationScope.Producer) == ConfigurationScope.Producer);
            var valueSerialiser = new BinarySerializer <TDocument>();
            var keySerialiser   = new BinarySerializer <Guid>();
            var deliveryHandler = new DeliveryHandler <Guid, TDocument>();

            using (var producer = new Producer <Guid, TDocument>(config, keySerialiser, valueSerialiser))
            {
                var deliveryReport = await producer.ProduceAsync(topicName, context.Id, context.Document);

                //producer.ProduceAsync(topicName, null, context.Document, deliveryHandler);
                //producer.Flush();
            }
        }
示例#5
0
 public virtual async Task UpsertDocument <TDocument>(IUpsertDocumentContext <TDocument> context) where TDocument : class
 {
     await this._documentDispatcher.UpsertDocument <TDocument>(context);
 }