private void SetQueue(int index, SafedQueue <SqlMessageQueue> msQueue) { if (msQueue != null && _distribeCollection.ContainsKey(index)) { _distribeCollection[index] = msQueue; } }
private void InitQueue(MessageConfig msgConfig) { if (msgConfig == null) { return; } if (msgConfig.ManagerThreadNumber == 0) { throw new Exception("ManagerThreadNumber is error."); } _distribeCollection = new Dictionary <int, SafedQueue <SqlMessageQueue> >(msgConfig.ManagerThreadNumber); for (int i = 0; i < msgConfig.ManagerThreadNumber; i++) { if (!_distribeCollection.ContainsKey(i)) { _distribeCollection.Add(i, new SafedQueue <SqlMessageQueue>()); } else { _distribeCollection[i] = new SafedQueue <SqlMessageQueue>(); } new Thread(StartWriteThread).Start(new MSMQContext(msgConfig, i)); } }
private void PutQueue(SqlMessageQueue sqlmq) { if (sqlmq == null) { return; } if (_distribeCollection.Count > 0) { int index = sqlmq.IdentityID % _distribeCollection.Count; SafedQueue <SqlMessageQueue> msQueue = GetQueue(index); while (msQueue != null && !msQueue.TryEnqueue(sqlmq)) { Thread.Sleep(SleepSeconds); } SetQueue(index, msQueue); } Interlocked.Exchange(ref writeCount, writeCount + 1); //TraceWriteLine(string.Format("写入ID:{0}队列数{1}", sqlmq.IdentityID, writeCount)); }
private void StartWriteThread(object item) { MSMQContext context = item as MSMQContext; if (context == null) { return; } try { SafedQueue <SqlMessageQueue> msmqQueue = GetQueue(context.Index); while (true) { try { while (msmqQueue.Count == 0) { Thread.Sleep(BufferWait); } List <SqlMessageQueue> smqBuffer = new List <SqlMessageQueue>(BufferMaxCount); while (smqBuffer.Count < BufferMaxCount) { if (msmqQueue.Count == 0) { break; } SqlMessageQueue sqlMsgQueue; if (msmqQueue.TryDequeue(out sqlMsgQueue)) { smqBuffer.Add(sqlMsgQueue); } else { Thread.Sleep(SleepSeconds); } } Interlocked.Exchange(ref writeCount, writeCount - smqBuffer.Count); TraceWriteLine(string.Format("Queue [{0} _ {1}] has buffered {2} / {3}, remaining: {4}", context.MsgConfig.ManagerMessagePath, context.Index, smqBuffer.Count, BufferMaxCount, writeCount)); foreach (SqlMessageQueue itemQueue in smqBuffer) { if (itemQueue == null) { continue; } int runTimes = 0; if (!DoSqlExecute(itemQueue, runTimes)) { //失败重执行次数 ReDoSqlExecute(context, itemQueue, runTimes); } } } catch (Exception e) { LogHelper.WriteException(string.Format("Queue write {0} error:", context.MsgConfig.ManagerMessagePath), e); } } } catch (Exception error) { LogHelper.WriteException(string.Format("Queue write {0} error:", context.MsgConfig.ManagerMessagePath), error); } }
private void SetQueue(int index, SafedQueue<SqlMessageQueue> msQueue) { if (msQueue != null && _distribeCollection.ContainsKey(index)) { _distribeCollection[index] = msQueue; } }
private void InitQueue(MessageConfig msgConfig) { if (msgConfig == null) return; if (msgConfig.ManagerThreadNumber == 0) { throw new Exception("ManagerThreadNumber is error."); } _distribeCollection = new Dictionary<int, SafedQueue<SqlMessageQueue>>(msgConfig.ManagerThreadNumber); for (int i = 0; i < msgConfig.ManagerThreadNumber; i++) { if (!_distribeCollection.ContainsKey(i)) { _distribeCollection.Add(i, new SafedQueue<SqlMessageQueue>()); } else { _distribeCollection[i] = new SafedQueue<SqlMessageQueue>(); } new Thread(StartWriteThread).Start(new MSMQContext(msgConfig, i)); } }