IBM MQSeries Accessing Issue from .NET
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("´╗┐"))
{
m开发者_如何学JAVAessage = 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();
}
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
queuePutMessageOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING
is what your missing.
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.
精彩评论