使用消息队列 异步插入数据,能发送消息,但是无法读取消息,该如何解决
使用消息队列 异步插入数据,能发送消息,但是无法读取消息
使用消息队列 异步插入数据,能发送消息,但是无法读取消息,配置文件中的消息对列的path设置如下:
<add key="StudentQueuePath" value="FormatName:DIRECT=OS:MachineName\Private$\RSStudents"/>
代码如下:
private static readonly string queuePath = ConfigurationManager.AppSettings["StudentQueuePath"];
protected MessageQueue queue = new MessageQueue(queuePath);
protected TimeSpan timeout = TimeSpan.FromSeconds(Convert.ToDouble(timeoutSeconds));
public virtual object Receive()
{
try
{
using (Message message = queue.Receive(timeout, transactionType))
return message;
}
catch (MessageQueueException mqex)
{
if (mqex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
throw new TimeoutException();
throw;
}
}
提示错误“远程计算机不可用。”
为什么我能将消息发送,可是读取的时候却无法读取呢?是不是StudentQueuePath的设置有错误?
------解决方案--------------------
当然首先要创建队列,不然怎么可能会有队列呢?
给个示例代码给你,我以前写的。
使用消息队列 异步插入数据,能发送消息,但是无法读取消息,配置文件中的消息对列的path设置如下:
<add key="StudentQueuePath" value="FormatName:DIRECT=OS:MachineName\Private$\RSStudents"/>
代码如下:
private static readonly string queuePath = ConfigurationManager.AppSettings["StudentQueuePath"];
protected MessageQueue queue = new MessageQueue(queuePath);
protected TimeSpan timeout = TimeSpan.FromSeconds(Convert.ToDouble(timeoutSeconds));
public virtual object Receive()
{
try
{
using (Message message = queue.Receive(timeout, transactionType))
return message;
}
catch (MessageQueueException mqex)
{
if (mqex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
throw new TimeoutException();
throw;
}
}
提示错误“远程计算机不可用。”
为什么我能将消息发送,可是读取的时候却无法读取呢?是不是StudentQueuePath的设置有错误?
------解决方案--------------------
当然首先要创建队列,不然怎么可能会有队列呢?
给个示例代码给你,我以前写的。
- C# code
/// <summary> /// 利用系统message构造的消息队列,可在不同的进程之间共享队列 /// </summary> /// <typeparam name="T"></typeparam> public class MSMQueue<T> { private MessageQueue _queue; private bool _noBlockThread = false; private string _queuePath = ""; private int _dequeueThreadCount = 0; /// <summary> /// 是否不阻塞线程 /// </summary> public bool NoBlockThread { get { return _noBlockThread; } } internal MessageQueue BaseQueue { get { return _queue; } } private const string EMTPYMESSAGE = ""; private string _queueName = ""; /// <summary> /// 队列名称 /// </summary> public string QueueName { get { if (string.IsNullOrEmpty(_queueName)) { _queueName = _queue.QueueName.Split('\\')[1]; } return _queueName; } } public MSMQueue(string queuePath) { bool isExists = CreateMessageQueue(queuePath); if (isExists) { RepairCount(); } } private const string _queueNamePrefix = @"private$\"; /// <summary> /// 构造已知msmq队列 /// </summary> /// <param name="queue"></param> internal MSMQueue(MessageQueue queue) { _queue = queue; _queuePath = string.Concat(@".\",_queueNamePrefix, QueueName); //Count属性中会去修正InnerCount值 RepairCount(); } internal MSMQueue(MessageQueue queue, int dequeueThreadCount) { _queue = queue; _queuePath = string.Concat(@".\", _queueNamePrefix, QueueName); _dequeueThreadCount = dequeueThreadCount; _noBlockThread = true; RestoreCount(); //Count属性中会去修正InnerCount值 RepairCount(); } public MSMQueue(string queuePath, int dequeueThreadCount) { if (dequeueThreadCount <= 0) { throw new ArgumentException("获取消息的线程数必须大于0!"); } //当队列中没有可用消息时,不阻塞线程 _noBlockThread = true; _dequeueThreadCount = dequeueThreadCount; bool isExists = CreateMessageQueue(queuePath); //为每个读取消息的线程定义一个默认的空消息,这样当队列为空时,不会被阻塞 if (!isExists) { for (int i = 0; i < _dequeueThreadCount; i++) { EnqueueEmptyMessage(); } } }