返回首页


在队列,既高性能,需要时可以处理大量的条目不吹我的系统的内存出水面的要求,我创建了以下实用队列。
总之,它是一个标准的内存中的通用。NET队列,配置溢出达到限制时,将开始使用MSMQ作为后端,以减轻内存使用的问题。在低量的情况下使用时,队列中的行为像一个正常的泛型队列。
任何疑问,意见,随时给我发电子邮件。
另外,看到我在CodeProject上的文章在这里:

public class OverflowQueue<T> where T : class

{

     Queue<T> internalQueue = new Queue<T>();



     BinaryMessageFormatter messageFormatter = new BinaryMessageFormatter();



     internal static int compactOverflowQueue = 

       AppConfigHelper.GetValue("CompactOverflowQueueAfterMessagesDequeued", 10000);

     internal static int maxInternalQueueSize = 

       AppConfigHelper.GetValue("MaxInternalOverflowQueueSize", 10000);



     QueuePath queuePath;



     MessageQueue overflowMSMQ;



     long currentQueueSize;

     long currentMSMQSize;



     bool useBackgroundMsmqPull;

     Thread backgroundMsmqPullThread;



     volatile bool pushingToMSMQ;



     object msmqPullLock = new object();

     object msmqLock = new object();



     int itemDequeued;



     public long CurrentMemoryQueueSize

     {

          get

          {

               return currentQueueSize;

          }

     }



     public long CurrentMSMQSize

     {

          get

          {

               return Interlocked.Read(ref currentMSMQSize);

          }

     }



     public long TotalCount

     {

          get

          {

               return Interlocked.Read(ref currentMSMQSize) + 

                 Interlocked.Read(ref currentQueueSize);

          }

     }



     public OverflowQueue(string queueName, bool purgeQueue, bool useBackgroundMsmqPull)

     {

          queuePath = new QueuePath(queueName, Environment.MachineName, false);

          overflowMSMQ = QueueCreator.CreateAndReturnQueue(queuePath);

          overflowMSMQ.Formatter = new BinaryMessageFormatter();



          if (purgeQueue)

               overflowMSMQ.Purge();



          this.useBackgroundMsmqPull = useBackgroundMsmqPull;



          if (useBackgroundMsmqPull)

          {

               // start a thread that pulls msmq messages back into the queue 

               backgroundMsmqPullThread = new Thread(BackgroundMsmqPull);

               backgroundMsmqPullThread.IsBackground = true;

               backgroundMsmqPullThread.Name = "BackgroundMsmqPullThread";

               backgroundMsmqPullThread.Start();

          }

     }



     void BackgroundMsmqPull()

     {

          while (true)

          {

               PullFromMSMQ();

               Thread.Sleep(1000);

          }

     }



     public void Enqueue(T item)

     {

          if (!pushingToMSMQ)

          {

               if (currentQueueSize >= maxInternalQueueSize)

               {

                    // We've busted the queue size... 

                    // start pushing to MSMQ until the current

                    // queue zeros out,

                    // then pop all entries (up to max) back into memory.

                    lock (msmqLock)

                    {

                         pushingToMSMQ = true;

                         PushToMSMQ(item);

                    }

               }

               else

               {

                    // We're still pushing into the memory queue

                    PushToMemoryQueue(item);

               }

          }

          else

          {

               // This lock looks like this (split if) because

               // I don't wnat to hold onto the lock for a push to the memory queue

               lock (msmqLock)

               {

                    if (pushingToMSMQ) // verify we're still pushing to MSMQ

                    {

                         PushToMSMQ(item);

                         return; // Skip the push to memory queue

                    }

               }



               // This will only get hit if we aren't still pushing to MSMQ

               PushToMemoryQueue(item);

          }

     }



     void PushToMemoryQueue(T item)

     {

          lock (internalQueue)

          internalQueue.Enqueue(item);



          Interlocked.Increment(ref currentQueueSize);

     }



     void PushToMSMQ(T item)

     {

          Message message = new Message(item, messageFormatter);

          overflowMSMQ.Send(message);

          Interlocked.Increment(ref currentMSMQSize);

     }



     public T Dequeue()

     {

          try

          {

               if (!useBackgroundMsmqPull)

               {

                    PullFromMSMQ();

               }

               else

               {

                    // This is here because if the background pull

                    // is on and a user tries to dequeue too quickly, 

                    // they can run up against an empty memory queue,

                    // but there could still be something in the MSMQ.

                    // So, we need to double check for them ;)



                    if (Interlocked.Read(ref currentQueueSize) == 0 

                         && Interlocked.Read(ref currentMSMQSize) > 0)

                    PullFromMSMQ();

               }



               T item = null;



               if (Interlocked.Read(ref currentQueueSize) > 0)

               {

                    lock(internalQueue)

                    item = internalQueue.Dequeue();

               }



               if (item != null)

               {

                    Interlocked.Increment(ref itemDequeued);

                    Interlocked.Decrement(ref currentQueueSize);



                    return item;

               }

               else

               {

                    throw new Exception("Nothing to dequeue!");

               }

          }

          finally

          {

               if (itemDequeued >= compactOverflowQueue)

               {

                    lock (internalQueue)

                    {                      

                         if (itemDequeued >= compactOverflowQueue)

                         {

                              // Compact the internal queue to save space

                              internalQueue.TrimExcess();

                              itemDequeued = 0;

                         }

                    }

               }

          }

     }



     void PullFromMSMQ()

     {

          // We've been putting all new messages into MSMQ...

          // now is the time to get them out and put them back into memory.

          while (Interlocked.Read(ref currentMSMQSize) > 0 && 

                 Interlocked.Read(ref currentQueueSize) < maxInternalQueueSize)

                 // currentQueueSize should be low here

          {

               Message message = overflowMSMQ.Receive();



               // decrement the MSMQ size

               Interlocked.Decrement(ref currentMSMQSize);



               T item = message.Body as T;

               PushToMemoryQueue(item);

          }

          if (Interlocked.Read(ref currentMSMQSize) <= 0)

          {

               lock (msmqLock)

               // lock around this to prevent the count

               // from being wrong when turning off msmq pushing

               {

                    if (Interlocked.Read(ref currentMSMQSize) <= 0)

                         pushingToMSMQ = false;

               }

          }

     }

}
|阿隆维勒

回答

评论会员:感谢分享 时间:2012/01/27
。rajeshm75
评论会员:转到{A} 时间:2012/01/27
monze2
评论会员:游客 时间:2012/01/27
你可能要检查www.krazykoder.com编码器可以创建,编译,运行和共享全面项目在线免费。您还可以浏览,投票和refork其他项目。一个伟大的方式来学习新技术,动手,值得考虑看看
!boggen
评论会员:游客 时间:2012/01/27
首先,对不起,我(差)英语只要读页面,因为我找一个在线的IDE。在线IDE有一些积极的好处,但也有一些负面影响。这里没有参数我觉得这个结论非常糟糕。它说,如果你不使用一个在线的IDE,您使用的是昨天工具......嘿,醒了。自我同一页还在线IDE仍然不是100%有用的脱机IDE。怎样才能离线昨天的工具,当脱机在线IDEaheart喜欢它的结论声音已经从其他地方复制..."使用此,它的明天techonology,这是最好的...''ECT。像推销的结论的声音。保留的结论是真实的,请
。帕特里克Hankinson
评论会员:游客 时间:2012/01/27
好,启动一个更大的项目,并希望能找到"开源"在线IDE中的代码的项目,我可以下载并安装网页服务器。这有以下...-修订内建而是文件系统,代码数据为基础。例如在MySQL。选项​​,做一个数据的基础上,文件转换。副对比也能够运行在数据库中,或代码的文件,根据不同的URL,通过复选框的代码和代码的不同版本。所以需要的东西可以进行调试/双重检查。-ACL(用户访问级别),不同的合并,和代码检查时,梳理代码。-代码紧缩,整个编码,可选的选择框,选择不同的文件,以紧缩或不。当我说代码紧缩,我的意思是,删除输入键,制表符,双空间。然后选项,由于进一步运算的变量名。主要是为了帮助加快生产环境。-每个"编程语言"我选择左或右侧页面功能上市。对于那些的HTML,PHP,JAVASCRIPT,CSS分钟光秃秃的骨头,。随着能力的扫描码,拉自定义函数(显示对象以及不错的自定义功能。能够加入,他们做什么的细节,通过所见即所得的编辑器。然后在另一个框,键入信息在,自动完成自定义功能也有很大的帮助,显示库函数,YUI,谷歌,道场,MooTools的,等..)的详细信息。-我不记得哪个网上IDE,本文是指,但它已"提前代码紧缩",并自动删除的认识功能。根本没有的好。给我()函数列表旁边括号,多少次功能,。给我的选择,"查找和替换"它想看看有什么可以截断和减少,在这样做之前。为变量做同样的事情。-整体做将允许各种手持设备也使用IDE。例如:刚刚想到的方式来处理一些事情或一个错误可能。转到掌上电脑,转到网站,登录,仔细检查,如果易足够的PDA上。做的变化和完成。对希望不使其家庭/工作/学校。和使用笔记本电脑/台式电脑。-没有必要的,但将是非常有益的,谁没有这方面的工作或代码跟踪。-追踪系统发出的错误,更新,希望,附加等..============================={BR}虽然我喜欢的在线IDE的想法。我不喜欢别人的网站,我需要有网站登录到一个在线的IDE并列。并会更加喜欢,便携式在线IDE。这与网上的开源软件。因此,管理员,和广大市民的编码,有手的详细信息。一个更不能混淆,去的信息。管理员喜欢将没有使用IDE构建,而是通过网页建立一个大节。每个人都有点。它是所有开源软件本身绑成换句话说。文件和意见,他们都需要。在20个不同的网站和100个不同的网页。但一切都捆绑在一起。和升级的需要。笔式驱动器符号,是一个很好的例子。说,WAMP的安装在笔式驱动器,然后在线IDE内WAMP安装。但更进一步,和在线网站软件,也安装到随身碟。这将是一个良好的便携式在线IDE。但对我来说,我想说允许用户转到"网站托管公司"的文件上传。打开一个安装文件。填写数据库信息。密码/用户名。而归。与在线的IDE,然后采取额外的步骤或2。设置成网站的开发模式或生产模式((ERR可选的代码修改))===================={BR}但在目前的IDE的后,我只是没有看到的能力有。有一个机会,我可能已经错过了什么,但在此刻。它看起来像更大的项目,将导致在版本控制系统和IDE的和什么可以被结合和重做
。WA
评论会员:游客 时间:2012/01/27
是在线的IDE/编译器和跨这对谷歌的文章寻找。真正有见地
!Wonde德斯
评论会员:游客 时间:2012/01/27
ShiftEdit.com梁你http://www.scribblelive.com/
。NickBudden
评论会员:游客 时间:2012/01/27
好。5。但一个问题吗?目前尚不清楚的类别和项目模板之间的区别?我选择分类-Web和软件开发语言-C#,语言地台-NET4.0项目模板-窗口的形式申请。Wonde德斯MCTS
Wonde德斯
评论会员:游客 时间:2012/01/27
A类是一个组织的组成部分,描述你的项目做什么,如果它是公共的,将分类网站的探索节。模板的代码模板,以启动新的项目与
。alleqs
评论会员:游客 时间:2012/01/27
感谢您的答复。但我期待一个Web项目,因为我选择了Web和软件开发类。不幸的是,我在结束了一个窗口的形式应用。这是如何符合类别和模板?Wonde德斯MCTS2011年5月30日(星期一),下午09:36修改
NickBudden
评论会员:游客 时间:2012/01/27
如何在云中存储代码和您的随身碟上使用一台便携式的IDE?在JAVA的东西将运行在几乎所有的操作系​​统
。NetDefender
评论会员:游客 时间:2012/01/27
它的肯定可能的,但如果在网上的IDE增长点它的能力应该有所有在您的计算机上的IDE功能(或笔式驱动器)。的意见,感谢:Greizzerland:得到了我的5
TassosK
评论会员:分享感谢 时间:2012/01/27
NickBudden
评论会员:它的辉煌 时间:2012/01/27
gaurav_verma_mca
评论会员:!感谢 时间:2012/01/27
NickBudden
评论会员:游客 时间:2012/01/27
我总是使用一个版本控制系统,以共享代码和,没有任何其他机制现在有一些更大的challanges各种数据库,EAI,电子邮件,网络服务器。开发这是任何严肃的项目要求。我觉得爱好项目,这些系统甚至没有sutable。他们充其量是入门级别的编程课程
。gaurav_verma_mca
评论会员:的意见{S0}感谢 时间:2012/01/27
NickBudden
评论会员:游客 时间:2012/01/27
这是一个彗星#编译器
CodingLover
评论会员:游客 时间:2012/01/27
老年退休金计划,感谢,如果我改变,这将是把后面的文章进入"等待"
?NickBudden
评论会员:游客 时间:2012/01/27
这是一个极好。这是我期待的东西。但我的意见,我的团队是一个在线版本控制,使用和管理。因此,我们可以随时随地进行工作。但是,我们需要脱机,本地安装该IDE中,如家用PC和所有。因此,在这一点上在线的IDE都远远超过其他任何方便。协作网上的源代码控制和在线版本控制,让我的生活更容易。感谢分享我感谢您的帮助所有的时间......CodingLover{S0}
CodingLover
评论会员:游客 时间:2012/01/27
谢谢你的意见,我认为这一次在线IDE的集成版本控制自己,我们将真正打破壁垒的一个工具,在寻找
。Southmountain
评论会员:游客 时间:2012/01/27
这很酷,我想说的。我感谢您的帮助所有的时间......CodingLover{S0}
NickBudden
评论会员:游客 时间:2012/01/27
分享感谢可用性真理