消息队列MQ
基本版本 Queue
代码:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Rongzi.BZone.Admin.Functions
{
public class MyExceptionFilterAttribute : System.Web.Mvc.HandleErrorAttribute
{
//版本1:使用预置队列类型存储异常对象
public static Queue<Exception> ExceptionQueue = new Queue<Exception>(); public override void OnException(ExceptionContext filterContext)
{
//将异常信息入队
ExceptionQueue.Enqueue(filterContext.Exception);
//跳转到自定义错误页
filterContext.HttpContext.Response.Redirect("~/Common/CommonError.html"); base.OnException(filterContext);
}
}
}
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Rongzi.BZone.Admin.Functions; namespace Rongzi.BZone.Admin
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute()); // MyExceptionFilterAttribute继承自HandleError,主要作用是将异常信息写入日志文件中
filters.Add(new MyExceptionFilterAttribute());
//filters.Add(new CustomHandleExceptionAttribute(GetError));
} //public static string GetError(System.Exception ex)
//{
// ResponseContext result = new ResponseContext();
// result.Head.Ret = -1;
// result.Head.Code = ErrCode.ParameterError;
// var ret = JsonConvert.SerializeObject(result);
// return ret;
//}
}
}
Global
添加配置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Net.Http.Formatting;
using Rongzi.BZone.Admin.Functions; namespace Rongzi.BZone.Admin
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure(
new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\log4net.config")); MessageQueueConfig.RegisterExceptionLogQueue();
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//GlobalConfiguration.Configuration.EnableCors();
log4net.Config.XmlConfigurator.Configure();
var jsonFormatter = new JsonMediaTypeFormatter();
//GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
GlobalConfiguration.Configuration.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
GlobalConfiguration.Configuration.MessageHandlers.Add(new ResponseDelegatingHandler());
}
}
}
对消息进行处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Text;
using System.IO; namespace Rongzi.BZone.Admin.Functions
{
public class MessageQueueConfig
{
public static void RegisterExceptionLogQueue()
{
string logFilePath = HttpContext.Current.Server.MapPath("/App_Data/");
//通过线程池开启线程,不停地从队列中获取异常信息并将其写入日志文件
ThreadPool.QueueUserWorkItem(o =>
{
while (true)
{
try
{
if (MyExceptionFilterAttribute.ExceptionQueue.Count > )
{
Exception ex = MyExceptionFilterAttribute.ExceptionQueue.Dequeue(); //从队列中出队,获取异常对象
if (ex != null)
{
//构建完整的日志文件名
string logFileName = logFilePath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
//获得异常堆栈信息
string exceptionMsg = ex.ToString();
//将异常信息写入日志文件中
File.AppendAllText(logFileName, exceptionMsg, Encoding.Default);
}
}
else
{
Thread.Sleep(); //为避免CPU空转,在队列为空时休息1秒
}
}
catch (Exception ex)
{
MyExceptionFilterAttribute.ExceptionQueue.Enqueue(ex);
}
}
}, logFilePath);
}
}
}
Redis消息队列版本
对上面的进行修改
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceStack.Redis;
using System.Configuration;
using ServiceStack.Redis.Generic;
using Rongzi.BZone.Common.Util; namespace Rongzi.BZone.Admin.Functions
{
public class MyExceptionFilterAttribute : System.Web.Mvc.HandleErrorAttribute
{
//版本2:使用Redis的客户端管理器(对象池)
public IRedisClient redisClient = RedisCommon.getInstance.getRedisClient(); public override void OnException(ExceptionContext filterContext)
{
//将异常信息入队
redisClient.EnqueueItemOnList("ExceptionLog", filterContext.Exception.ToString());
//跳转到自定义错误页
filterContext.HttpContext.Response.Redirect("~/Common/CommonError.html"); base.OnException(filterContext);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Text;
using System.IO;
using ServiceStack.Redis;
using System.Configuration;
using ServiceStack.Redis.Generic;
using Rongzi.BZone.Common.Util; namespace Rongzi.BZone.Admin.Functions
{
public class MessageQueueConfig
{
public static IRedisClient redisClient = RedisCommon.getInstance.getRedisClient();
public static void RegisterExceptionLogQueue()
{
//通过线程池开启线程,不停地从队列中获取异常信息并将其写入日志文件
ThreadPool.QueueUserWorkItem(o =>
{
while (true)
{
try
{
if (redisClient.GetListCount("ExceptionLog") > )
{
//从队列中出队,获取异常对象
string errorMsg = redisClient.DequeueItemFromList("ExceptionLog");
if (!string.IsNullOrEmpty(errorMsg))
{
//使用Log4Net写入异常日志
LogHelper.Error(errorMsg);
}
}
else
{
Thread.Sleep(); //为避免CPU空转,在队列为空时休息1秒
}
}
catch (Exception ex)
{
redisClient.EnqueueItemOnList("ExceptionLog", ex.ToString());
}
}
});
}
}
}
http://www.tuicool.com/articles/Ubeyay3
消息队列MQ的更多相关文章
- 为什么会需要消息队列(MQ)?
为什么会需要消息队列(MQ)? #################################################################################### ...
- 消息队列一:为什么需要消息队列(MQ)?
为什么会需要消息队列(MQ)? #################################################################################### ...
- 详解RPC远程调用和消息队列MQ的区别
PC(Remote Procedure Call)远程过程调用,主要解决远程通信间的问题,不需要了解底层网络的通信机制. RPC框架 知名度较高的有Thrift(FB的).dubbo(阿里的). RP ...
- 消息队列 MQ 入门理解
功能特性: 应用场景: 消息队列 MQ 可应用于如下几个场景: 分布式事务 在传统的事务处理中,多个系统之间的交互耦合到一个事务中,响应时间长,影响系统可用性.引入分布式事务消息,交易系统和消息队列之 ...
- 消息队列MQ简介
项目中要用到RabbitMQ,领导让我先了解一下.在之前的公司中,用到过消息队列MQ,阿里的那款RocketMQ,当时公司也做了简单的技术分享,自己也看了一些博客.自己在有道云笔记上,做了一些整理,但 ...
- 消息队列MQ集合
消息队列MQ集合 消息队列简介 kafka简介 Centos7部署zookeeper和Kafka集群 .
- 高并发系统:消息队列MQ
注:前提是知道什么是消息队列.不懂的去搜索各种消息队列入门(activeMQ.rabbitMQ.rocketMQ.kafka) 1.为什么要使用MQ?(MQ的好处:解耦.异步.削峰) (1)解耦:主要 ...
- java面试记录三:hashmap、hashtable、concurrentHashmap、ArrayList、linkedList、linkedHashmap、Object类的12个成员方法、消息队列MQ的种类
口述题 1.HashMap的原理?(数组+单向链表.put.get.size方法) 非线程安全:(1)hash冲突:多线程某一时刻同时操作hashmap并执行put操作时,可能会产两个key的hash ...
- 【消息队列MQ】各类MQ比较
目录(?)[-] RabbitMQ Redis ZeroMQ ActiveMQ JafkaKafka 目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息 ...
- 消息队列mq的原理及实现方法
消息队列技术是分布式应用间交换信息的一种技术.消息队列可驻留在内存或磁盘上,队列存储消息直到它们被应用程序读走.通过消息队列,应用程序可独立地执行--它们不需要知道彼此的位置.或在继续执行前不需要等待 ...
随机推荐
- 分布式架构高可用架构篇_06_MySQL源码编译安装(CentOS-6.7+MySQL-5.6)
redhat: 下载:http://dev.mysql.com/downloads/mysql/ 选择5.6 source包 解压 cmake . -DCMAKE_INSTALL_PREFIX=/us ...
- 设计模式学习系列9 外观模式Facade
1.概述 自己卖了一辆越野自行车,但毕竟不是自己定制的,买回来之后可能需要更改一下脚蹬,座皮,里程计数器或者刹车系统,假如将自行车看做一个整体系统,对我们而言使用的是自行车,然后我们对自己车构件的修改 ...
- 本田--CRV
名称:CR-V 类型:紧凑型SUV 排量:2L/2.4L 变速箱:CVT无级变速 价格:18-25W 品牌:本田 生产:东风本田 历史:1995年第一代 外观特点:没啥特点= =空间算大的
- 【转载】MySQL性能优化的最佳20+条经验
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...
- MySQL出现大量unauthenticated user的问题
发现这算属MySQL的一个bug,不管连接是通过hosts还是ip的方式,MySQL都会对DNS做反查,IP到DNS,由于反查的接续速度过 慢(不管是不是isp提供的dns服务器的问题或者其他原因), ...
- Supesite 参数说明
supesite有人看到的是强大的功能,我看到的是坑爷的一些用法,第一次看到block,我晕了.对于参数一头雾水,下面收集了一些,备用吧. supesite标签调用参数详解 参数: blocktype ...
- struct stat结构体的详解和用法
[cpp] view plaincopy //! 需要包含de头文件 #include <sys/types.h> #include <sys/stat.h> S_ISLNK( ...
- [收藏]ASP.NET MVC管道详述
ASP.NET MVC从诞生到现在已经好几个年头了,这个框架提供一种全新的开发模式,更符合web开发本质.你可以很好的使用以及个性化和扩展这个框架,但这需要你对它有足够的了解.这篇文章主要从整体角度总 ...
- install zabbix-agent on CENTOS
in ubuntu--https://www.digitalocean.com/community/tutorials/how-to-install-zabbix-on-ubuntu-configur ...
- PHP--浏览器禁用cookie后,怎么使用session
sessionid是存储在cookie中的,解决方案如下: Session URL重写,保证在客户端禁用或不支持COOKIE时,仍然可以使用Session session机制.session机制是一种 ...