日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

      首頁(yè)編程開發(fā)ASP.NET → aspx請(qǐng)求進(jìn)行異步隊(duì)列控制處理

      aspx請(qǐng)求進(jìn)行異步隊(duì)列控制處理

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:西西整理時(shí)間:2012/11/21 22:57:22字體大。A-A+

      作者:西西點(diǎn)擊:0次評(píng)論:0次標(biāo)簽: 異步

      三相異步電機(jī)圖形編輯器v2.0 綠色免費(fèi)版
      • 類型:行業(yè)軟件大小:1.5M語(yǔ)言:中文 評(píng)分:5.0
      • 標(biāo)簽:
      立即下載

      當(dāng)一個(gè)aspx頁(yè)面請(qǐng)求處理包括大量的IO工作,而這些IO資源又非常有限的情況下,那這個(gè)頁(yè)面在對(duì)面大量請(qǐng)求的時(shí)候就有可能導(dǎo)致大量線程等待處理,從而使應(yīng)用程序線程開銷過多影響整體的處理效能.在這種情況我們更希望通過一個(gè)隊(duì)列的機(jī)制控制處理線程的開銷來(lái)實(shí)現(xiàn)更高效的處理效能.因此.net提供IHttpAsyncHandler來(lái)解決這些事情,但有個(gè)問題就是實(shí)現(xiàn)一個(gè)IHttpAsyncHandler意味著要自己要實(shí)現(xiàn)自己的處理過程,并不能對(duì)已經(jīng)實(shí)現(xiàn)功能的.aspx進(jìn)行控制.但通過反編譯.net代碼來(lái)看可以實(shí)現(xiàn)一個(gè)IHttpAsyncHandler接管現(xiàn)有的.aspx頁(yè)面實(shí)現(xiàn)異步處理,又不需要修改現(xiàn)有頁(yè)面實(shí)現(xiàn)的代碼.下面詳細(xì)講述實(shí)現(xiàn)過

      從.net的web配置文件來(lái)看asp.net默認(rèn)處理aspx的并不是IHttpHandler而是System.Web.UI.PageHandlerFactory,反編譯代碼看下

      [PermissionSet(SecurityAction.InheritanceDemand, Unrestricted = true), PermissionSet(SecurityAction.LinkDemand, Unrestricted = true)]

          public class PageHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory

          {

              private bool _isInheritedInstance;

              protected internal PageHandlerFactory()

              {

                  this._isInheritedInstance = (base.GetType() != typeof(PageHandlerFactory));

              }

              public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)

              {

                  return this.GetHandlerHelper(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);

              }

              IHttpHandler IHttpHandlerFactory2.GetHandler(HttpContext context, string requestType, VirtualPath virtualPath, string physicalPath)

              {

                  if (this._isInheritedInstance)

                  {

                      return this.GetHandler(context, requestType, virtualPath.VirtualPathString, physicalPath);

                  }

                  return this.GetHandlerHelper(context, requestType, virtualPath, physicalPath);

              }

              public virtual void ReleaseHandler(IHttpHandler handler)

              {

              }

              private IHttpHandler GetHandlerHelper(HttpContext context, string requestType, VirtualPath virtualPath, string physicalPath)

              {

                  Page page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page), context, true) as Page;

                  if (page == null)

                  {

                      return null;

                  }

                  page.TemplateControlVirtualPath = virtualPath;

                  return page;

              }

          }

      從反編譯的代碼來(lái)看,看到的希望.首先PageHandlerFactory是可以繼承的,而GetHandler又是可重寫的,有了這兩個(gè)條件完全可以滿足我們的需要.通過承繼PageHandlerFactory就可以直接處理現(xiàn)有的aspx文件.

      實(shí)現(xiàn)IHttpAsyncHandler

      既然可以重寫PageHandlerFactory的GetHandler,而IhttpAsyncHandler又是繼承IHttpHandler;那事情就變得簡(jiǎn)單多了可能通過構(gòu)建一個(gè)IhttpAsyncHandler直接返回.

      public class CustomPageFactory : System.Web.UI.PageHandlerFactory
          {
              static CustomPageFactory()
              {
                  G_TaskQueue = new TaskQueue(20);
              }
              public static TaskQueue G_TaskQueue;
              public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
              {
                  AspxAsyncHandler handler = new AspxAsyncHandler(base.GetHandler(context, requestType, virtualPath, path));
                  return handler;
              }
          }

      可以實(shí)現(xiàn)一個(gè)IHttpAsyncHandler把PageHandlerFactory返回的IHttpHandler重新包裝一下

      public class AspxAsyncHandler : IHttpAsyncHandler
          {
              public AspxAsyncHandler(IHttpHandler handler)
              {
                  mHandler = handler;
              }
              private IHttpHandler mHandler;
              public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
              {
                  AspxAsyncResult result = new AspxAsyncResult(context, mHandler, cb);
                  CustomPageFactory.G_TaskQueue.Add(result);
                  return result;
              }
              public void EndProcessRequest(IAsyncResult result)
              {
              }
              public bool IsReusable
              {
                  get { return false; }
              }
              public void ProcessRequest(HttpContext context)
              {
                  throw new NotImplementedException();
              }
          }

      這樣一個(gè)異步處理的httphandler就包裝完了.我們只需要通過配置httphandler就可以實(shí)現(xiàn)對(duì)現(xiàn)有的aspx進(jìn)行異步隊(duì)列處理.


            
          

      隊(duì)列和線程控制

      在處理的過程中并沒有使用線程池來(lái)完成具體的工作,如果每個(gè)直接調(diào)用線程池那同樣面臨的問題就是線池線耗出現(xiàn)大量線程調(diào)度問題影響性能.所以在上面實(shí)現(xiàn)IHttpAsyncHandler的BeginProcessRequest方法中是構(gòu)建一個(gè)IAsyncResult添加到隊(duì)列中.之于這個(gè)隊(duì)列的實(shí)現(xiàn)相對(duì)比較簡(jiǎn)單:

      public class TaskQueue
          {
              public TaskQueue(int group)
              {
                  mDispatchs = new List(group);
                  for (int i = 0; i < group; i++)
                  {
                      mDispatchs.Add(new Dispatch());
                  }
              }
              private IList mDispatchs;
              private long mIndex = 0;
              private int GetIndex()
              {
                  return (int)System.Threading.Interlocked.Increment(ref mIndex) % mDispatchs.Count;
              }
              public void Add(AspxAsyncResult aspAsync)
              {
                  if (aspAsync != null)
                  {
                      mDispatchs[GetIndex()].Push(aspAsync);
                  }
              }
              class Dispatch
              {
                  public Dispatch()
                  {
                      System.Threading.ThreadPool.QueueUserWorkItem(OnRun);
                  }
                  private Queue mQueue = new Queue(1024);
                  public void Push(AspxAsyncResult aspAR)
                  {
                      lock (this)
                      {
                          mQueue.Enqueue(aspAR);
                      }
                  }
                  private AspxAsyncResult Pop()
                  {
                      lock (this)
                      {
                          if (mQueue.Count > 0)
                              return mQueue.Dequeue();
                          return null;
                      }
                  }
                  private void OnRun(object state)
                  {
                      while (true)
                      {
                          AspxAsyncResult asyncResult = Pop();
                          if (asyncResult != null)
                          {
                              asyncResult.Execute();
                          }
                          else
                          {
                              System.Threading.Thread.Sleep(10);
                          }
                      }
                  }
              }
          }

      為了更好地控制線程,隊(duì)列的實(shí)現(xiàn)是采用多隊(duì)列多線程機(jī)制,就是根據(jù)你需要的并發(fā)情況來(lái)指定線程隊(duì)列數(shù)來(lái)處理,當(dāng)然這種設(shè)計(jì)是比較死板并不靈活,如果想設(shè)計(jì)靈活一點(diǎn)是根據(jù)當(dāng)前隊(duì)列的處理情況和資源情況來(lái)動(dòng)態(tài)計(jì)算擴(kuò)沖現(xiàn)有隊(duì)列線程數(shù).

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無(wú)聊無(wú)聊

        熱門評(píng)論

        最新評(píng)論

        第 1 樓 江蘇蘇州蘇州工業(yè)職業(yè)技術(shù)學(xué)院 網(wǎng)友 客人 發(fā)表于: 2014/2/10 20:30:12
        牛 大神qq多少 互動(dòng)一下吧

        支持( 0 ) 蓋樓(回復(fù))

        發(fā)表評(píng)論 查看所有評(píng)論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過審核才能顯示)
        推薦文章

        沒有數(shù)據(jù)

        最新文章
          沒有數(shù)據(jù)