豪仕知识网--知识就是力量!

微信
手机版
生活常识

aspx请求进行异步队列控制处理-异步请求能放mutation吗

作者 :有胸为大 2024-01-06 10:58:32 围观 : 评论

aspx请求进行异步队列控制处理-异步请求能放mutation吗

豪士君测试所用平台

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

纪录美好生活,一起观趣事,这里包罗IT知识,豪仕知识网是信息的海洋,有你听不到的故事,接下来我们一起来看看aspx请求进行异步队列控制处理。

当一个aspx页面请求处理包括大量的IO工作,而这些IO资源又非常有限的情况下,那这个页面在对面大量请求的时候就有可能导致大量线程等待处理,从而使应用程序线程开销过多影响整体的处理效能.在这种情况我们更希望通过一个队列的机制控制处理线程的开销来实现更高效的处理效能.因此.net提供IHttpAsyncHandler来解决这些事情,但有个问题就是实现一个IHttpAsyncHandler意味着要自己要实现自己的处理过程,并不能对已经实现功能的.aspx进行控制.但通过反编译.net代码来看可以实现一个IHttpAsyncHandler接管现有的.aspx页面实现异步处理,又不需要修改现有页面实现的代码.下面详细讲述实现过

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

从.net的web配置文件来看asp.net默认处理aspx的并不是IHttpHandler而是System.Web.UI.PageHandlerFactory,反编译代码看下

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

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

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲HTTP://WWW.hAoz.net███████████████████████████东方金报网

public class PageHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory

{

private bool _isInheritedInstance;

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识网HTtp://www.haoZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

protected internal PageHandlerFactory()

{

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

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

}

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

HTTP://WWW.haoz.net◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐豪仕知识网

{

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识网HTtp://www.haoZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

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

}

◐◐◐◐●☛█▼▲豪仕知识网http://www.haOz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

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

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识网HTtp://www.haoZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

{

if (this._isInheritedInstance)

豪仕知识网http://www.haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

{

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

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

}

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

HTTP://WWW.haoz.net◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐豪仕知识网

}

public virtual void ReleaseHandler(IHttpHandler handler)

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

{

}

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

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

{

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

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

if (page == null)

{

return null;

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

}

page.TemplateControlVirtualPath = virtualPath;

return page;

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲HtTp://wWW.haoz.net豪仕知识网●●●●●●●●●●●●●●●●●●●●●●●●●●

}

}

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

从反编译的代码来看,看到的希望.首先PageHandlerFactory是可以继承的,而GetHandler又是可重写的,有了这两个条件完全可以满足我们的需要.通过承继PageHandlerFactory就可以直接处理现有的aspx文件.

豪仕知识网http://www.haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

实现IHttpAsyncHandler

既然可以重写PageHandlerFactory的GetHandler,而IhttpAsyncHandler又是继承IHttpHandler;那事情就变得简单多了可能通过构建一个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; } }

豪仕知识网http://www.haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

可以实现一个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(); } }

这样一个异步处理的httphandler就包装完了.我们只需要通过配置httphandler就可以实现对现有的aspx进行异步队列处理.

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

队列和线程控制

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

在处理的过程中并没有使用线程池来完成具体的工作,如果每个直接调用线程池那同样面临的问题就是线池线耗出现大量线程调度问题影响性能.所以在上面实现IHttpAsyncHandler的BeginProcessRequest方法中是构建一个IAsyncResult添加到队列中.之于这个队列的实现相对比较简单:

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); } } } } }

为了更好地控制线程,队列的实现是采用多队列多线程机制,就是根据你需要的并发情况来指定线程队列数来处理,当然这种设计是比较死板并不灵活,如果想设计灵活一点是根据当前队列的处理情况和资源情况来动态计算扩冲现有队列线程数.

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲豪仕知识网HtTp://▲▼▲▼▲

123在本页阅读全文

本文导航

第1页: 首页 第2页: IAsyncResult实现 第3页: 测试效果和总结

当一个aspx页面请求处理包括大量的IO工作,而这些IO资源又非常有限的情况下,那这个页面在对面大量请求的时候就有可能导致大量线程等待处理,从而使应用程序线程开销过多影响整体的处理效能.在这种情况我们更希望通过一个队列的机制控制处理线程的开销来实现更高效的处理效能.因此.net提供IHttpAsyncHandler来解决这些事情,但有个问题就是实现一个IHttpAsyncHandler意味着要自己要实现自己的处理过程,并不能对已经实现功能的.aspx进行控制.但通过反编译.net代码来看可以实现一个IHttpAsyncHandler接管现有的.aspx页面实现异步处理,又不需要修改现有页面实现的代码.下面详细讲述实现过

HTTP://WWW.haoz.net◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐豪仕知识网

从.net的web配置文件来看asp.net默认处理aspx的并不是IHttpHandler而是System.Web.UI.PageHandlerFactory,反编译代码看下

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

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

public class PageHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory

{

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲豪仕知识网HtTp://▲▼▲▼▲

private bool _isInheritedInstance;

protected internal PageHandlerFactory()

{

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

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

}

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

◐◐◐◐●☛█▼▲豪仕知识网http://www.haOz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

{

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

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

}

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

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

{

●☛█▼▲豪仕知识网◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲

if (this._isInheritedInstance)

{

●☛█▼▲豪仕知识网◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲

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

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

}

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲豪仕知识网HtTp://▲▼▲▼▲

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

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲豪仕知识网HtTp://▲▼▲▼▲

}

public virtual void ReleaseHandler(IHttpHandler handler)

{

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

}

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

◐◐◐◐●☛█▼▲豪仕知识网http://www.haOz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

{

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

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

if (page == null)

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

{

return null;

}

HTTP://WWW.haoz.net豪仕知识网采集不好玩哦◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐撒旦法师打发斯蒂芬

page.TemplateControlVirtualPath = virtualPath;

return page;

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲HTTP://WWW.hAoz.net███████████████████████████东方金报网

}

}

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲豪仕知识网HtTp://▲▼▲▼▲

从反编译的代码来看,看到的希望.首先PageHandlerFactory是可以继承的,而GetHandler又是可重写的,有了这两个条件完全可以满足我们的需要.通过承继PageHandlerFactory就可以直接处理现有的aspx文件.

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

实现IHttpAsyncHandler

既然可以重写PageHandlerFactory的GetHandler,而IhttpAsyncHandler又是继承IHttpHandler;那事情就变得简单多了可能通过构建一个IhttpAsyncHandler直接返回.

HTTP://WWW.haoz.net◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐豪仕知识网

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; } }

可以实现一个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(); } }

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲HtTp://wWW.haoz.net豪仕知识网●●●●●●●●●●●●●●●●●●●●●●●●●●

这样一个异步处理的httphandler就包装完了.我们只需要通过配置httphandler就可以实现对现有的aspx进行异步队列处理.

●☛█▼▲豪仕知识网◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲

队列和线程控制

◐◐◐◐●☛█▼▲豪仕知识网HT●☛█▼▲◐◐◐◐●☛█▼▲

在处理的过程中并没有使用线程池来完成具体的工作,如果每个直接调用线程池那同样面临的问题就是线池线耗出现大量线程调度问题影响性能.所以在上面实现IHttpAsyncHandler的BeginProcessRequest方法中是构建一个IAsyncResult添加到队列中.之于这个队列的实现相对比较简单:

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); } } } } }

◐◐◐◐●☛█▼▲豪仕知识网http://www.haOz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

为了更好地控制线程,队列的实现是采用多队列多线程机制,就是根据你需要的并发情况来指定线程队列数来处理,当然这种设计是比较死板并不灵活,如果想设计灵活一点是根据当前队列的处理情况和资源情况来动态计算扩冲现有队列线程数.

123在本页阅读全文

本文导航
HTTP://WWW.haoz.net豪仕知识网采集不好玩哦◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐撒旦法师打发斯蒂芬

第1页: 首页 第2页: IAsyncResult实现 第3页: 测试效果和总结

aspx请求进行异步队列控制处理的介绍就聊到这里吧,感谢你花时间阅读,更多关于aspx请求进行异步队列控制处理的信息别忘了在本站进行查找哦。豪仕知识网往后会继续推荐aspx请求进行异步队列控制处理相关内容。

相关文章