码迷,mamicode.com
首页 > 其他好文 > 详细

使用Redis分布式队列

时间:2016-04-03 19:01:19      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

1.这是处理异常的类

    public class MyExceptionAttribute:HandleErrorAttribute
    {
        //public static Queue<Exception> ExceptionQueue = new Queue<Exception>();
        //项目中使用下面方式创建redisclient
        public static IRedisClientsManager clientManager = new PooledRedisClientManager(new string[] { "127.0.0.1:6379" });
        public static IRedisClient redisClient = clientManager.GetClient();
        /// <summary>
        /// 捕获异常
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            Exception ex = filterContext.Exception;
            //写到队列
            //ExceptionQueue.Enqueue(ex);
            redisClient.EnqueueItemOnList("errorQueue", ex.ToString());
            //跳转到错误页面.
            filterContext.HttpContext.Response.Redirect("/Error.html");
        }
    }

2.Global文件中配置

public class MvcApplication : SpringMvcApplication //System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            log4net.Config.XmlConfigurator.Configure();//读取了配置文件中关于Log4Net配置信息.
            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //开启一个线程,扫描异常信息队列。
            string filePath = Server.MapPath("/Log/");
            ThreadPool.QueueUserWorkItem((a) => {
                while (true)
                {
                    //判断一下队列中是否有数据
                    //if (MyExceptionAttribute.ExceptionQueue.Count() > 0)
                    if (MyExceptionAttribute.redisClient.GetListCount("errorQueue") > 0)
                    {
                        string errorMsg =
MyExceptionAttribute.redisClient.DequeueItemFromList("errorQueue");
                        {
                            //Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue();
                            if (!string.IsNullOrEmpty(errorMsg))
                            {
                                //将异常信息写到日志文件中。
                                //string fileName = DateTime.Now.ToString("yyyy-MM-dd");
                                //File.AppendAllText(filePath+fileName+".txt",ex.ToString(),System.Text.Encoding.UTF8);
                                ILog logger = LogManager.GetLogger("errorMsg");
                                logger.Error(errorMsg);
                            }
                            else
                            {
                                //如果队列中没有数据,休息
                                Thread.Sleep(3000);
                            }
                        }
                    }
                    else
                    {
                        //如果队列中没有数据,休息
                        Thread.Sleep(3000);
                    }
                }
            
            
            },filePath);

        }
    }

3.不要忘记使用Redis需要的3个dll文件

技术分享

使用Redis分布式队列

标签:

原文地址:http://www.cnblogs.com/xtxtx/p/5350266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!