IBM MQSeries 从 .NET 访问问题
我对 IBM MQSeries 不是很熟悉,但我正在编写 C# 脚本,用于从队列服务器写入和读取文件。问题是我的读取有效,但我的写入无效。请注意,我正在使用相同的队列,因此不必费心朝那个方向走。
我的代码首先使用以下代码访问 MQserver:
MQQueueManager qManager;
MQQueue queue;
MQMessage queueMessage;
MQGetMessageOptions queueGetMessageOptions;
MQPutMessageOptions queuePutMessageOptions;
string QueueName;
public MQAccess(string queueName, string queueManager, string connection, string channel)
{
QueueName = queueName;
qManager = new MQQueueManager(queueManager, channel, connection);
queue = qManager.AccessQueue(QueueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
}
我能够使用以下代码从队列中读取文件:
public bool NextMessage(ref string message, ref DateTime putDateTime)
{
queueMessage = new MQMessage();
queueMessage.Format = MQC.MQFMT_STRING;
queueGetMessageOptions = new MQGetMessageOptions();
queueGetMessageOptions.Options = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING;
try
{
queue.Get(queueMessage, queueGetMessageOptions);
}
catch (MQException mqex)
{
if (mqex.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE)
{
message = "";
return false;
}
else
throw mqex;
}
message = queueMessage.ReadString(queueMessage.MessageLength);
putDateTime = queueMessage.PutDateTime;
if (message.StartsWith("´╗┐"))
{
message = message.Substring(3, message.Length - 3);
}
return true;
}
但是,如果我尝试使用以下代码编写,它会给出错误:
public void WriteMessage(string message)
{
queueMessage = new MQMessage();
queueMessage.WriteString(message);
queueMessage.Format = MQC.MQFMT_STRING;
queuePutMessageOptions = new MQPutMessageOptions();
queue.Put(queueMessage, queuePutMessageOptions);
}
我的错误捕获给出了错误:
应用程序错误
当然这并没有显示太多。所以我检查了服务器上的事件日志,这显示了错误:
从以下位置接收数据时发生错误 stx041774 (192.168.225.51) 超过 TCP/IP。这可能是由于 通讯故障。
来自 TCP/IP 的返回代码 (recv) 呼叫号码为 10054 (X'2746')。记录这些 值并告诉系统 管理员。
我查了10054,意思是:
现有连接被强制 被远程主机关闭。
有谁知道我可以做些什么来使这项工作成功?我是否必须设置一个 MQC 选项才能进行写入?因为我不知道如何处理这些选项,所以我什至不确定这是否是问题所在。
请记住,我每次都会通过以下方式关闭连接:
public void Close()
{
queueMessage = null;
queue.Close();
qManager.Close();
}
~MQAccess()
{
queueMessage = null;
queue.Close();
qManager.Close();
}
I'm not extremely familiar with IBM MQSeries, but I am writing c# scripts which write and read files from my queue server. The problem is my read works but my write doesn't. Please notice that I am using the same queue so don't bother going in that direction.
My code firstly accesses the MQserver with the following code:
MQQueueManager qManager;
MQQueue queue;
MQMessage queueMessage;
MQGetMessageOptions queueGetMessageOptions;
MQPutMessageOptions queuePutMessageOptions;
string QueueName;
public MQAccess(string queueName, string queueManager, string connection, string channel)
{
QueueName = queueName;
qManager = new MQQueueManager(queueManager, channel, connection);
queue = qManager.AccessQueue(QueueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
}
I am able to read files from my queue with this code:
public bool NextMessage(ref string message, ref DateTime putDateTime)
{
queueMessage = new MQMessage();
queueMessage.Format = MQC.MQFMT_STRING;
queueGetMessageOptions = new MQGetMessageOptions();
queueGetMessageOptions.Options = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING;
try
{
queue.Get(queueMessage, queueGetMessageOptions);
}
catch (MQException mqex)
{
if (mqex.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE)
{
message = "";
return false;
}
else
throw mqex;
}
message = queueMessage.ReadString(queueMessage.MessageLength);
putDateTime = queueMessage.PutDateTime;
if (message.StartsWith("´╗┐"))
{
message = message.Substring(3, message.Length - 3);
}
return true;
}
If I however try to write with the following code it gives me errors:
public void WriteMessage(string message)
{
queueMessage = new MQMessage();
queueMessage.WriteString(message);
queueMessage.Format = MQC.MQFMT_STRING;
queuePutMessageOptions = new MQPutMessageOptions();
queue.Put(queueMessage, queuePutMessageOptions);
}
My error catch gives me the error:
Error in the application
Which doesn't show much of course. So I checked the event log on the server and this showed me the error:
An error occurred receiving data from
stx041774 (192.168.225.51) over
TCP/IP. This may be due to a
communications failure.The return code from the TCP/IP (recv)
call was 10054 (X'2746'). Record these
values and tell the systems
administrator.
I looked up 10054 and means:
An existing connection was forcibly
closed by the remote host.
Does anyone have any idea what I can do to make this work? Is there perhaps an MQC option I have to set for writing? Because I have no idea what to do with the options, I'm not even sure if this is the issue.
Please keep in mind that I also close my connection every time with:
public void Close()
{
queueMessage = null;
queue.Close();
qManager.Close();
}
~MQAccess()
{
queueMessage = null;
queue.Close();
qManager.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
斯奎格很接近,但没有雪茄。当您打开队列时,如果您想同时读取和写入消息,则需要在打开选项上指定输入和输出。示例代码仅指定了输入选项。
Squig was close but no cigar. When you open the queue, you need to specify both input and output on the open options if you want to both read and write messages. The example code has only input options specified.
正如您在获取消息时设置获取选项一样,您也需要设置放置
当您发送消息时的选项
是您所缺少的。
Just as you set your get options when getting messages you also need to set put
options when you put a message
is what your missing.
也许可以看看 CodeProject 上与 MSMQ 协议相关的文章,文章实现了一个聊天系统。
希望这有帮助,
此致,
汤姆.
Maybe having a look at this article on CodeProject, in relation to MSMQ protocol, the article implements a chat system.
Hope this helps,
Best regards,
Tom.