public void Insert(MongodbItem item) { try { var databaseName = item.DatabaseName; if (!_submitDataMemoryQueueServices.ContainsKey(databaseName)) { lock (_submitDataMemoryQueueServices) { if (!_submitDataMemoryQueueServices.ContainsKey(databaseName)) { var memoryQueueService = LocalServiceLocator.GetService<IMemoryQueueService<MongodbItem>>(); memoryQueueService.Init(new MemoryQueueServiceConfiguration<MongodbItem>(string.Format("MongodbInsertServiceQueue<{0}>", databaseName), InternalSubmitData) { ConsumeIntervalMilliseconds = 1000, ConsumeItemCountInOneBatch = 100, ConsumeThreadCount = 1 }); _submitDataMemoryQueueServices.Add(databaseName, memoryQueueService); } } } if (_submitDataMemoryQueueServices.ContainsKey(databaseName)) _submitDataMemoryQueueServices[databaseName].Enqueue(item); } catch (Exception ex) { LocalLoggingService.Exception("MongodbInsertService.Insert error: {0}", ex.Message); } }
public void BatchInsertTest() { var mongodbInsertService = LocalServiceLocator.GetService<IMongodbInsertService>(); for (var i = 0; i < 10; i++) { var item1 = new MongodbItem { DatabaseName = "testdb1", TableName = "OperationLog", Data = new TestData { Operator = string.Format("010227-{0}", i), OperationType = "login", IsSuccess = true, OperationTime = DateTime.Now } }; mongodbInsertService.Insert(item1); var item2 = new MongodbItem { DatabaseName = "testdb2", TableName = "OperationLog", Data = new TestData { Operator = string.Format("leo-{0}", i), OperationType = "change_password", IsSuccess = true, OperationTime = DateTime.Now } }; mongodbInsertService.Insert(item2); } Thread.Sleep(2000); }