public class QueueManger
{
private static string QueuePath = @".\private$\{0}"; /// <summary>
/// 创建MSMQ队列
/// </summary>
/// <param name="queueName">队列路径</param>
/// <param name="transactional">是否事务队列</param>
public static void Createqueue(string queueName, bool transactional = false)
{
try
{
QueuePath = string.Format(QueuePath, queueName);
//判断队列是否存在
if (!MessageQueue.Exists(QueuePath))
{
MessageQueue.Create(QueuePath);
LoggerFile.Write(QueuePath + "已成功创建!");
}
else
{
LoggerFile.Write(QueuePath + "已经存在!");
}
}
catch (MessageQueueException e)
{
LoggerFile.Write(e.Message);
}
}
/// <summary>
/// 删除队列
/// </summary>
/// <param name="queueName"></param>
public static void Deletequeue(string queueName)
{
try
{
QueuePath = string.Format(QueuePath, queueName);
//判断队列是否存在
if (MessageQueue.Exists(QueuePath))
{
MessageQueue.Delete(QueuePath);
LoggerFile.Write(QueuePath + "已删除!");
}
else
{
LoggerFile.Write(QueuePath + "不存在!");
}
}
catch (MessageQueueException e)
{
LoggerFile.Write(e.Message);
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <typeparam name="T">用户数据类型</typeparam>
/// <param name="target">用户数据</param>
/// <param name="queueName">队列名称</param>
/// <param name="tran"></param>
/// <returns></returns>
public static bool SendMessage<T>(T target, string queueName, MessageQueueTransaction tran = null)
{
try
{
QueuePath = string.Format(QueuePath, queueName);
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(QueuePath);
System.Messaging.Message myMessage = new System.Messaging.Message();
myMessage.Body = target;
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
//发送消息到队列中
if (tran == null)
{
myQueue.Send(myMessage);
}
else
{
myQueue.Send(myMessage, tran);
}
LoggerFile.Write("消息已成功发送到" + queueName + "队列!");
return true;
}
catch (ArgumentException e)
{
LoggerFile.Write(e.Message);
return false;
}
}
/// <summary>
/// 接收消息
/// </summary>
/// <typeparam name="T">用户的数据类型</typeparam>
/// <param name="queueName">消息路径</param>
/// <returns>用户填充在消息当中的数据</returns>
public static T ReceiveMessage<T>(string queueName, MessageQueueTransaction tran = null)
{
QueuePath = string.Format(QueuePath, queueName);
//连接到本地队列
MessageQueue myQueue = new MessageQueue(QueuePath);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
try
{
//从队列中接收消息
System.Messaging.Message myMessage = tran == null ? myQueue.Receive() : myQueue.Receive(tran);
return (T)myMessage.Body; //获取消息的内容
}
catch (MessageQueueException e)
{
LoggerFile.Write(e.Message);
}
catch (InvalidCastException e)
{
LoggerFile.Write(e.Message);
}
return default(T);
}
/// <summary>
/// 采用Peek方法接收消息
/// </summary>
/// <typeparam name="T">用户数据类型</typeparam>
/// <param name="queueName">队列路径</param>
/// <returns>用户数据</returns>
public static T ReceiveMessageByPeek<T>(string queueName)
{
QueuePath = string.Format(QueuePath, queueName);
//连接到本地队列
MessageQueue myQueue = new MessageQueue(QueuePath);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
try
{
//从队列中接收消息
System.Messaging.Message myMessage = myQueue.Peek();
return (T)myMessage.Body; //获取消息的内容
}
catch (MessageQueueException e)
{
LoggerFile.Write(e.Message);
}
catch (InvalidCastException e)
{
LoggerFile.Write(e.Message);
}
return default(T);
}
/// <summary>
/// 获取队列中的所有消息
/// </summary>
/// <typeparam name="T">用户数据类型</typeparam>
/// <param name="queueName">队列路径</param>
/// <returns>用户数据集合</returns>
public static List<T> GetAllMessage<T>(string queueName)
{
QueuePath = string.Format(QueuePath, queueName);
MessageQueue myQueue = new MessageQueue(QueuePath);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
try
{
Message[] msgArr = myQueue.GetAllMessages();
List<T> list = new List<T>();
msgArr.ToList().ForEach((o) =>
{
list.Add((T)o.Body);
});
return list;
}
catch (Exception e)
{
LoggerFile.Write(e.Message);
}
return null;
}
}

  

MQ单一消息完整流程的更多相关文章

  1. Objective-C RunTime 学习笔记 之 消息转发流程

    1) 当向某个对象发送消息时,先从cache(cache_t)中查找方法对象(method_t),如果找到则进行回调:否则通过查找对象的类(元类)定义中方法列表,一直追溯到NSObject, 如果找到 ...

  2. 架构设计:系统间通信(20)——MQ:消息协议(下)

    (接上文<架构设计:系统间通信(19)--MQ:消息协议(上)>) 上篇文章中我们重点讨论了"协议"的重要性.并为各位读者介绍了Stomp协议和XMPP协议. 这两种协 ...

  3. RocketMQ之九:RocketMQ消息发送流程解读

    在讨论这个问题之前,我们先看一下Client的整体架构. Producer与Consumer类体系 从下图可以看出以下几点:(1)Producer与Consumer的共同逻辑,封装在MQClientI ...

  4. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  5. Grunt搭建自动化web前端开发环境--完整流程

    Grunt搭建自动化web前端开发环境-完整流程 jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前 ...

  6. MQ(队列消息的入门)

    消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成,通过提供消息传递和消息排队模型,它可以在分布式环境下拓展进程间的通信,对于消息中间件,常见的角色大致也 ...

  7. 与MQ通讯的完整JAVA程序

    该程序实现了发送消息与读取消息的功能,见其中的 send***与get***方法.这只适合于测试,因为环境中的程序还需要对此有稍微的更改,在真实的环境中肯定是在while(true){...} 的无限 ...

  8. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程

    我们在之前的文章中中已经讲到了正确部署运行cas server 和 在cas client中配置. 在此基础上 我们去掉了https的验证,启用了http访问的模式. 单点登录(七)-----实战-- ...

  9. 【转】Windows消息投递流程:WM_COMMAND消息流程

    原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182585 该示例通过研究基本的单文档程序的“文件”--“打开”命令,分析WM_COM ...

随机推荐

  1. [ASE][Daily Scrum]11.06

    我们的<坦克大战·无尽>正式开始动工了,今天的任务计划如下~ [Plan] View Shilin Liu 搭建好开发环境 收集素材         Control Jiafan Zhu ...

  2. Windows下Node.js的安装与配置

    一.下载和安装 1. 前往官网https://nodejs.org/或https://nodejs.org/en/download/下载最新推荐版的Node.js,本文使用10.13.0版本. 对于W ...

  3. [翻译]第三天- 在 Mac 上运行 .NET Core 应用程序

    原文: http://michaelcrump.net/part3-aspnetcore/ *** 简介 该系列文章的完整列表如下: 第一天 - 在 Windows 下安装和运行 .NET Core ...

  4. asp.net mvc 配置ckeditor4.x

    下载地址:https://ckeditor.com/ckeditor-4/download/ 一.使用方法: 1.在页面中引入ckeditor核心文件ckeditor.js 2.在使用编辑器的地方插入 ...

  5. php根据修改时间删除指定目录下文件

    //$dir-文件地址,$files-存储返回数组,$type-查找文件类型组 public function read_dir($dir,&$files,$type) { if(!is_di ...

  6. winform执行程序报错:已停止工作,windows正在检查该问题的解决方案

    每次运行程序时都会弹出错误框:winform已停止工作,windows正在检查该问题的解决方案 事件查看器错位信息: 错误应用程序名称: TMS_winform.exe,版本: 1.0.0.0,时间戳 ...

  7. 类库中使用WPF 资源文件

    1.类库的 后缀.csproj文件,第一个<PropertyGroup>中加入下面代码 <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-b ...

  8. MVC 视图不使用模板页的两种方法

    直接对view页面的Layout值设置null @{ Layout = null;//"~/Views/Shared/_Layout.cshtml"; } 对_ViewStart. ...

  9. window.open新打开窗口与新开标签页

    最近在使用window.open时忽略了一个细节问题:window.open新打开一个窗口,但是有时却是新打开一个窗口有时打开一个新标签页.虽然对一般的需求来说,这个两种情况都无所谓,但是对于那种有强 ...

  10. Liferay开发实战(1):入门

    网址: https://www.liferay.com/zh/ 文档: https://dev.liferay.com/develop 入门文章网上很多,中文的较少,存在版本太旧的问题,也缺少一步一步 ...