消息队列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的原理及实现方法
消息队列技术是分布式应用间交换信息的一种技术.消息队列可驻留在内存或磁盘上,队列存储消息直到它们被应用程序读走.通过消息队列,应用程序可独立地执行--它们不需要知道彼此的位置.或在继续执行前不需要等待 ...
随机推荐
- Scrum会议8(Beta版本)
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- OpenCV学习笔记——形态学梯度操作
代码: #include<cv.h> #include<highgui.h> int main(void) { cvNamedWindow("cmp"); ...
- Open vSwitch使用案例扩展实验
参考:Open vSwitch使用案例扩展实验 实验目的: 通过python脚本调用OpenvSwitch命令: 学习Mininet基于python脚本创建拓扑的实现: 进一步深度使用"ov ...
- lua5.2版本在VS2010下的环境搭建
第一次使用脚本语言,第一次使用解释性语言 公司的现在维护的游戏开发的比较早,采用的lua还是比较早的版本,像NPC脚本系统就是使用lua+luabind结合的module模式,服务器端没涉及到,所以不 ...
- php 文件和表单内容一起上传
<?php $filename = $_POST['filename']; $explain = $_POST['explain']; $upfile = $_FILES['upfile']; ...
- Bigtable: A Distributed Storage System for Structured Data
https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...
- Hashtable、Dictionary和List 谁效率更高
一 前言 很少接触HashTable晚上回来简单看了看,然后做一些增加和移除的操作,就想和List 与 Dictionary比较下存数据与取数据的差距,然后便有了如下的一此测试, 当然我测的方法可能不 ...
- mysql 存储过程 php版本
<?php /** * PHP操作Mysql存储过程示例 * * @author flyer0126 * @date 2011-12-23 * */ //配置数据库连接信息 $hostname ...
- 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...
- 一段检测IP设备是否在线的代码
原理是通过发送ARP包来检测 uses WinSock function SendARP(const DestIP, SrcIP: Cardinal; pMacAddr: PULONG; var Ph ...