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. [leet code 4] Median of Two Sorted Arrays

    1 题目 There are two sorted arrays A and B of size m and n respectively. Find the median of the two so ...

  2. cnn的说明

    概述 前面的练习中,解决了一些有关低分辨率图像的问题,比如:小块图像,手写数字小幅图像等.在这部分中,我们将把已知的方法扩展到实际应用中更加常见的大图像数据集. 全联通网络 在稀疏自编码章节中,我们介 ...

  3. SharePoint 列表中增加列编辑功能菜单

    需求描述 在企业的部署中,经常将SharePoint和TFS集成在一起,两个系统之间相互读取数据,展现开发进度.在TFS 2018之前版本中,由于TFS的门户定制功能有限,用户比较喜欢使用ShareP ...

  4. C# Winform模仿百度日历

    想写博客不知道从何处开始,就从回忆开始吧. 第一个就从自定义日历控件开始 产生背景: 大概2015年时候有个项目要用到日历,用默认日历展示给用户看,用户毫不客气都说界面太丑,最好做成像百度日历那样方便 ...

  5. 定时任务 Wpf.Quartz.Demo.4

    本文继续介绍定时任务 Wpf.Quartz.Demo.3的一些小细节, 代码也请前往第3节下载. 1.RichTextBox右键菜单 <RichTextBox.ContextMenu>   ...

  6. DZY Loves Math(莫比乌斯反演)

    \(x=p_1^{\alpha_1}p_2^{\alpha_2}...p_c^{\alpha_c}\) \(f(x)=\max(\alpha_1,\alpha_2,...,\alpha_c)\) \( ...

  7. OSX10.12搭建IPv6本地环境测试APP

    前记 最近刚换了工作,生活终于又安定下来了,又可以更博了 正文 最近公司在上线APP(整体全是用JS去写的,就用了我原生的一个控制器),然后APP就去上线,就被苹果巴巴给拒了.通过阅读苹果回复的邮件, ...

  8. 微信小程序实现给循环列表点击添加类(单项和多项)

    在微信小程序里面没有DOM对象, 不能操作DOM. 所有的操作通过数据来实现,下面主要实现了给循环列表点击添加类的操作 一.单项 目标需求:实现下图,给点击的view增加类,每次只能选择一个. 主要思 ...

  9. nodeJs实现微信小程序的图片上传

    今天我来介绍一下nodejs如何实现保存微信小程序传过来的图片及其返回 首先wx.uploadFile绝大部分时候是配合wx.chooseImage一起出现的,毕竟选择好了图片,再统一上传是实现用户图 ...

  10. TypeScript设计模式之装饰、代理

    看看用TypeScript怎样实现常见的设计模式,顺便复习一下. 学模式最重要的不是记UML,而是知道什么模式可以解决什么样的问题,在做项目时碰到问题可以想到用哪个模式可以解决,UML忘了可以查,思想 ...