消息队列的使用
换了新公司,有了新的开始。
现在开始用消息队列,做邮件的异步发送。
本系统,通过webservice访问本地消息队列,没有用wcf的消息队列访问,也没有用公共队列。
不过由于有webservice的支持,访问本地消息队列,没有任何的问题。
以下为队列操作代码
/// <summary>
/// 队列操作类
/// </summary>
public class OperateQueue:Interface.IStore
{
/// <summary>
/// 建立队列
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>是否成功</returns>
private ResultInfo<bool> CreateQueue(string queueName)
{
ResultInfo<bool> result = new ResultInfo<bool>();
result.Data = false;
result.Result = false;
try
{
MessageQueue.Create(queueName, true);
result.Data = true;
result.Result = true;
}
catch(Exception ex)
{
throw ex;
}
return result;
}
/// <summary>
/// 判断队列是否存在
/// </summary>
/// <param name="queueName">队列名字</param>
/// <returns></returns>
private bool IsQueueExists(string queueName)
{
return MessageQueue.Exists(queueName);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>是否成功</returns>
public ResultInfo<bool> StoreMessage(string queueName, string message)
{
if (!this.IsQueueExists(queueName))
{
this.CreateQueue(queueName);
}
MessageQueue queue = new MessageQueue() ;
ResultInfo<bool> result = new ResultInfo<bool>();
result.Data = false;
result.Result = false;
MessageQueueTransaction mts = new MessageQueueTransaction();
try
{
mts.Begin();
queue= new MessageQueue(queueName);
queue.Send(message,mts);
result.Data = true;
result.Message = "消息存储成功";
result.Result = true;
mts.Commit();
}
catch (Exception ex)
{
mts.Abort();
throw ex;
}
finally
{
queue.Close();
queue.Dispose();
}
return result;
}
/// <summary>
/// 读取消息
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>读取到的消息</returns>
public ResultInfo<string> ReadMessage(string queueName)
{
ResultInfo<string> result = new ResultInfo<string>();
result.Data = string.Empty;
result.Result = false;
MessageQueue queue = new MessageQueue();
try
{
queue = new MessageQueue(queueName);
Message message = new Message();
message = queue.Receive();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
result.Data = message.Body.ToString();
result.Result = true;
}
catch(Exception ex)
{
result.Message = ex.Message;
}
finally
{
queue.Close();
queue.Dispose();
}
return result;
}
}
/// 队列操作类
/// </summary>
public class OperateQueue:Interface.IStore
{
/// <summary>
/// 建立队列
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>是否成功</returns>
private ResultInfo<bool> CreateQueue(string queueName)
{
ResultInfo<bool> result = new ResultInfo<bool>();
result.Data = false;
result.Result = false;
try
{
MessageQueue.Create(queueName, true);
result.Data = true;
result.Result = true;
}
catch(Exception ex)
{
throw ex;
}
return result;
}
/// <summary>
/// 判断队列是否存在
/// </summary>
/// <param name="queueName">队列名字</param>
/// <returns></returns>
private bool IsQueueExists(string queueName)
{
return MessageQueue.Exists(queueName);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>是否成功</returns>
public ResultInfo<bool> StoreMessage(string queueName, string message)
{
if (!this.IsQueueExists(queueName))
{
this.CreateQueue(queueName);
}
MessageQueue queue = new MessageQueue() ;
ResultInfo<bool> result = new ResultInfo<bool>();
result.Data = false;
result.Result = false;
MessageQueueTransaction mts = new MessageQueueTransaction();
try
{
mts.Begin();
queue= new MessageQueue(queueName);
queue.Send(message,mts);
result.Data = true;
result.Message = "消息存储成功";
result.Result = true;
mts.Commit();
}
catch (Exception ex)
{
mts.Abort();
throw ex;
}
finally
{
queue.Close();
queue.Dispose();
}
return result;
}
/// <summary>
/// 读取消息
/// </summary>
/// <param name="queueName">队列名称</param>
/// <returns>读取到的消息</returns>
public ResultInfo<string> ReadMessage(string queueName)
{
ResultInfo<string> result = new ResultInfo<string>();
result.Data = string.Empty;
result.Result = false;
MessageQueue queue = new MessageQueue();
try
{
queue = new MessageQueue(queueName);
Message message = new Message();
message = queue.Receive();
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
result.Data = message.Body.ToString();
result.Result = true;
}
catch(Exception ex)
{
result.Message = ex.Message;
}
finally
{
queue.Close();
queue.Dispose();
}
return result;
}
}
采用了队列的事务操作,istore用来与上层调用进行隔离,这样做是为了保证,当需要使用其它消息队列时,做到最小改动。
webservice和队列部署在同一台服务器上,本打算利用
System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(@"FormatName:DIRECT=tcp:192.168.16.85\private$\Action");
去进行局域网内的访问,但是没有成功,所以暂时放弃。