MQ单一消息完整流程
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单一消息完整流程的更多相关文章
- Objective-C RunTime 学习笔记 之 消息转发流程
1) 当向某个对象发送消息时,先从cache(cache_t)中查找方法对象(method_t),如果找到则进行回调:否则通过查找对象的类(元类)定义中方法列表,一直追溯到NSObject, 如果找到 ...
- 架构设计:系统间通信(20)——MQ:消息协议(下)
(接上文<架构设计:系统间通信(19)--MQ:消息协议(上)>) 上篇文章中我们重点讨论了"协议"的重要性.并为各位读者介绍了Stomp协议和XMPP协议. 这两种协 ...
- RocketMQ之九:RocketMQ消息发送流程解读
在讨论这个问题之前,我们先看一下Client的整体架构. Producer与Consumer类体系 从下图可以看出以下几点:(1)Producer与Consumer的共同逻辑,封装在MQClientI ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- Grunt搭建自动化web前端开发环境--完整流程
Grunt搭建自动化web前端开发环境-完整流程 jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前 ...
- MQ(队列消息的入门)
消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成,通过提供消息传递和消息排队模型,它可以在分布式环境下拓展进程间的通信,对于消息中间件,常见的角色大致也 ...
- 与MQ通讯的完整JAVA程序
该程序实现了发送消息与读取消息的功能,见其中的 send***与get***方法.这只适合于测试,因为环境中的程序还需要对此有稍微的更改,在真实的环境中肯定是在while(true){...} 的无限 ...
- 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程
我们在之前的文章中中已经讲到了正确部署运行cas server 和 在cas client中配置. 在此基础上 我们去掉了https的验证,启用了http访问的模式. 单点登录(七)-----实战-- ...
- 【转】Windows消息投递流程:WM_COMMAND消息流程
原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182585 该示例通过研究基本的单文档程序的“文件”--“打开”命令,分析WM_COM ...
随机推荐
- redis-master/slave模式
类似mysql的master-slave模式一样,redis的master-slave可以提升系统的可用性,master节点写入cache后,会自动同步到slave上. 环境: master node ...
- 不合法的DB Index
redis报错,Invalid Db Index . 需要清理一下redis, 进入redis文件加下,登录redis, redis-cli ,输入账号密码, flush all, 回收程序池,重新生 ...
- [Word]让字符重合显示
某些时候需要让字符重合显示,比如您好二字,显示为: 需要用到word的Advance域,他可以让后面的文字上下左右移动一定的磅. 譬如上面你好的显示:word中域代码为: 意思是好字向left移动了2 ...
- Effective C++笔记:继承与面向对象设计
关于OOP 博客地址:http://www.cnblogs.com/ronny 转载请注明出处! 1,继承可以是单一继承或多重继承,每一个继承连接可以是public.protected或private ...
- Android开发教程 - 使用Data Binding(八)使用自定义Interface
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- 解决linux下source /etc/profile关闭终端失效问题
本来想配置环境变量的,看网上和博客上很多说改/etc/profile,然后source /etc/profile之后就可以永久保存使环境变量生效,但是终端一关闭,就环境变量就失效了,其他终端也用不了. ...
- Office 2010激活 NO KMS products detected问题
今天用office2010激活工具Office 2010 Toolkit激活安装的office2010时悲剧的遇到了这个问题,如下图: (这张图是从网上找的,不过和我遇到的问题是一样的). 然后上网搜 ...
- redis集成相关工具类
package cn.yiyuan.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; i ...
- Math.log()对数的妙用
###对数log 的妙用... formatBytes(bytes) { if (bytes === '0' || isNaN(bytes)) return ''; var s = ['Bytes', ...
- 插入排序的Java代码实现
插入排序也是一类非常常见的排序方法,它主要包含直接插入排序,Shell排序和折半插入排序等几种常见的排序方法. 1.直接插入排序 直接插入排序的思路非常简单:依次将待排序的数据元素按其关键字值的大小插 ...