Receiving plain text messages in MSMQ

Yesterday, I spent some time working with the Resource Adapter for MSMQ that ships with IBM WebSphere DataStage TX Extender for Message Broker. It has been some time since I did any work with MSMQ – probably since COM+ days, I don’t remember having worked natively with it in .NET before. Incidentally, I was using SharpDevelop for the C# coding.

All I wanted to do was to write a simple piece of code that took messages off a queue – rather like the amqsget sample that ships with WebSphere MQ would do for me. The DSTX map I was running was putting the messages onto the queue just fine, and I wanted to read them off again.

This turned out to be remarkably awkward… I’m not sure quite how this happened, but the messages that were being put on the queue were pretty much just plain text. MSMQ by default seems to want to wrap everything that gets put onto a queue as XML (using an XMLMessageFormatter). In MQ terms I suppose this is similar to the parser serialising a message to the queue at the end of a message flow in Message Broker. Since I wasn’t working with XML, this was irrelevant to me.

What I ended up doing was to receive the BodyStream of the message, not the Body. Here’s what I had to do in C# to read and display the plain text messages:

message.Formatter = new ActiveXMessageFormatter();
reader = new StreamReader(message.BodyStream);
msgBody = reader.ReadToEnd();
Console.WriteLine("Label: "+ message.Label + " Content: " + msgBody);

That doesn’t look like a lot of code, but it took me quite a while to get my head around the fact that I couldn’t dispense with all the Formatter stuff and just write:

Console.WriteLine("Label: "+ message.Label + " Content: " + message.Body);

I’m putting this here as an aide-memoire in case I ever have to do it again.

Technorati tags:

2 thoughts on “Receiving plain text messages in MSMQ”

  1. I just ran into this as well. I understand why they did it this way, but it’s still kind of annoying that they wouldn’t leave in some kind of support for plain text…perhaps a PlainTextFormatter?

Leave a Reply