使用Redis分布式队列
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)
)
{
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();
}
}
}
else
{
//如果队列中没有数据,休息
Thread.Sleep();
}
}
},filePath);
}
}
3.不要忘记使用Redis需要的3个dll文件

使用Redis分布式队列的更多相关文章
- Redis分布式队列解决文件并发的问题
1.首先将捕获的异常写到Redis的队列中 public class MyExceptionAttribute : HandleErrorAttribute { public static IRedi ...
- Redis分布式队列和缓存更新
原文链接:https://www.cnblogs.com/hua66/p/9600085.html 在使用Redis中,我们可能会遇到以下场景: 例如: 某用户向服务器中发送一个请求,服务器将用户请求 ...
- 分布式日志2 用redis的队列写日志
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- redis分布式锁和消息队列
最近博主在看redis的时候发现了两种redis使用方式,与之前redis作为缓存不同,利用的是redis可设置key的有效时间和redis的BRPOP命令. 分布式锁 由于目前一些编程语言,如PHP ...
- JavaWeb项目架构之Redis分布式日志队列
架构.分布式.日志队列,标题自己都看着唬人,其实就是一个日志收集的功能,只不过中间加了一个Redis做消息队列罢了. 前言 为什么需要消息队列? 当系统中出现"生产"和" ...
- java-spring基于redis单机版(redisTemplate)实现的分布式锁+redis消息队列,可用于秒杀,定时器,高并发,抢购
此教程不涉及整合spring整合redis,可另行查阅资料教程. 代码: RedisLock package com.cashloan.analytics.utils; import org.slf4 ...
- 基于redis分布式缓存实现
Redis的复制功能是完全建立在之前我们讨论过的基 于内存快照的持久化策略基础上的,也就是说无论你的持久化策略选择的是什么,只要用到了Redis的复制功能,就一定会有内存快照发生,那么首先要注意你 的 ...
- Redis分布式
昨天公司技术大牛做了一个Redis分布式的技术分享: Redis分布式资源: http://redis.io/topics/cluster-tutorialhttp://redis.io/topics ...
- Spring-data-redis: 分布式队列
Redis中list数据结构,具有"双端队列"的特性,同时redis具有持久数据的能力,因此redis实现分布式队列是非常安全可靠的.它类似于JMS中的"Queue&qu ...
随机推荐
- script标签不带属性与带async、defer的区别
<script> 当页面解析到script标签时,会停止解析并下载对应的脚本,并马上执行,执行完毕后再继续解析页面 <script async> async 在下载脚本的同时不 ...
- [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- Centos 6.5 挂载硬盘 4K对齐 (笔记 实测)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:Linux centos 2.6.32-431.17.1.el6.x86_64(Centos-6.5-x86_ ...
- Sqoop_ 简单介绍
一.基本作用 概念: Sqoop被称为协作框架,是在Hadoop.2.X生态系统的辅助型框架,简单说,就是一个数据转换工具,类似的协作框架有文件收集库框架Flume,任务协调框架Oozie,大数据We ...
- JQuery基本方法介绍和使用
1.属性 $("p").addClass(css中定义的样式类型); 给某个元素添加样式 常用于表格鼠标移动效果 $(document).ready(function(){ //& ...
- mpdf部署到linux环境中遇到的问题
首先遇到的问题未:Error - mPDF requires mb_string functions. Ensure that PHP is compiled with php_mbstring.dl ...
- sign in和sign up区别
如果是网站的话sign up是注册,sign in是登录的意思,另外,sign out退出
- javascript 数组的深度复制
javascript 数组的深度复制 一般情况下,使用 "=" 可以实现赋值.但对于数组.对象.函数等这些引用类型的数据,这个符号就不好使了. 1. 数组的简单复制 1.1 简单遍 ...
- 关于svn的使用
svn听课笔记 1. 下载并安装svn2. 将svn安装目录中bin目录添加到用户path变量中.3. 创建svn根目录svnroot4. 启动svn服务 在dos启动命令: svnserve -d ...
- 深入理解C++虚函数表
虚函数表是C++类中存放虚函数的一张表,理解虚函数表对于理解多态很重要. 本次使用的编译器是VS2013,为了简化操作,不用去操作函数指针,我使用到了VS的CL编译选项来查看类的内存布局. CL使用方 ...