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. C# 两个datatable中的数据快速比较返回交集或差集

    转自: https://www.cnblogs.com/lacey/p/5893380.html 如果两个datatable的字段完全一致的话,可以直接使用Except,Intersect //Exc ...

  2. CefSharp.v49.0.1浏览器控件完全WPF版,实现禁止弹出新窗口,在同一窗口打开链接,并且支持带type="POST" target="_blank"的链接

    需求场景:在查询页面,填写查询条件,查询条件包括上传的图片,根据图片的特征查询,这就需要在提交的时候,使用POST提交,因为GET提交无法提交图片数据,提交查询条件之后,在新的窗口展示查询结果.(当然 ...

  3. WTS 2.1.18124.1 彻底抛弃了 15063(Win 10 创意者更新)

    现在新建的WTS模板,默认最低版本是16299了,目标版本是17134 17134到来之前,就感觉到会这样,不过终究还是来了. 不支持15063的原因是导航菜单Windows.UI.Xaml.Cont ...

  4. nginx windown命令

    cmd进入nginx程序文件夹启动nginx:start nginx nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出 nginx nginx -t ...

  5. 常用Java集合类总结

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 7.1.List(允许重复元素) ArrayList: 底层数据结构:Object[] 在查询(get).遍 ...

  6. fatal: LF would be replaced by CRLF in index.html

    进入项目目录,在.git文件夹下打开config配置文件,添加上下面两句话: autocrlf = false safecrlf = false 即可

  7. centos7----pstree

    centos 默认没有pstree 安装 yum -y install psmisc

  8. SQL Server 用户'NT AUTHORITY\IUSR' 登录失败

    今天打开网站时,突然报这个错误,平时都好好的 Cannot open database "JMECC" requested by the login. The login fail ...

  9. 【xsy2913】 enos 动态dp

    题目大意:给你一棵 $n$个点 以 $1$为根 的树,每个点有$ 0,1,2 $三种颜色之一,初始时整棵树的颜色均为 $0$. $m$ 次操作, 每次操作形如: 1 x y c : 将 $x$到$y$ ...

  10. java和js获取当前天之后或之前7天(任意)日期

    一.获取过去第几天的日期(- 操作) 或者 未来 第几天的日期( + 操作) /** * 获取过去第几天的日期(- 操作) 或者 未来 第几天的日期( + 操作) * * @param past * ...