When and Where I should use MSMQ?

  • Thread starter Thread starter Jet Leung
  • Start date Start date
J

Jet Leung

Hi all,
Where is the MSMQ use for? For example, I have a client program to control
the database and how can I use MSMQ in my program? When I should use it?
For example I have a method to insert new record to the database and how can
I use MSMQ in this method?
 
MSMQ is typically used when you need to provide message-oriented
communication somewhere in a system. I typically use it when I'm developing
a truly asynchronous app, and I need to transactionally accept messages and
queue them up for a receiver.

But aren't you putting the cart before the horse? If you need MSMQ, use it.
It's really not the sort of thing where you look for a way to use it in the
app before establishing a need.
 
Where is the MSMQ use for? For example, I have a client program to
control
I've also wondered about this.

Would a typical example be the pattern where you register on a website, and
after you hit the Register button, it sends you an email sometime later on
with an autogenerated password to the email you indicated in your
registration? Is that one example of using a message queue?
 
control
I've also wondered about this.

Would a typical example be the pattern where you register on a website,
and
after you hit the Register button, it sends you an email sometime later on
with an autogenerated password to the email you indicated in your
registration? Is that one example of using a message queue?

Yes indeed, and it's the "sometime later" in your post which is important.
E.g. if you have an eCommerce system where people can purchase goods with a
credit card, you will typically want to authorise the purchase in real-time
and provide a response to the customer in as close to real-time as possible
i.e. your purchase has been authorised, your bank has declined the
transaction etc. That would not be a good candidate for MSMQ because you
don't actually want to queue this message up - you want to tell the person
there and then.

However, your warehouse might arrange the packaging and dispatch of goods
all through the day but only send out the dispatch notification emails as a
batch process through the night when the site is less busy, so that part of
the process would be a prime candidate for MSMQ because the clients don't
really need to know the precise minute that their goods were dispatched.
 
The canonical exmaple is an ecomerce website where you want to verify the order in terms of stock and credit card clearance. So you make the user wait until you have verified these and give them a confirmation number (and maybe send an email). However, the dispatch of the product needs to go to the shipping department who only work 9-5 maybe. So you can't make the user wait for the shipping department to OK the order being shipped, SO you queue the shipping request on to a reliable queue (one that won't lose its state if the machine crashes) like MSMQ or MQSeries and the shipping department dequeue the messages in the morning when they come into work.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

control
I've also wondered about this.

Would a typical example be the pattern where you register on a website, and
after you hit the Register button, it sends you an email sometime later on
with an autogenerated password to the email you indicated in your
registration? Is that one example of using a message queue?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.770 / Virus Database: 517 - Release Date: 27/09/2004



[microsoft.public.dotnet.languages.csharp]
 
One use of MSMQ is to get round the performance hit of writing a record to
database or disk. These operations (relatively speaking) are quite slow.
However, if you have an object containing data you wish to persist in this
way, you can dump it on a queue and have a separate "receiver" program which
will read the objects off the queue and write them to wherever they should
go.

When creating a message queue, I believe you can specify whether or not the
messages written should be recoverable in the event of a reboot or crash -
obviously if they are, then it takes longer to write them onto the queue in
the first place, but it is still faster than writing to database.

Jon
 
Hi,
Sorry,I can't clearly get what your mean. Could you give me an
example code?
As you said, if an object include data, I can send the object on
queue right? So if I have an dataset or only a command object how can I
send it on queue? After I send it on queue and the revicer get it how
does the revicer know how to handler these data?
e.g an dataset just send it on queue is ok?
e.g an command object how to send it on queue?
 
We do it something like this:

To add item to queue:

MessageQueue queue = new MessageQueue(path);
queue.DefaultPropertiesToSend.Recoverable = false;
queue.DefaultPropertiesToSend.Priority = MessagePriority.Normal;
queue.Formatter = new BinaryMessageFormatter();
queue.Send(myobject);

Note that "myobject" must be serializable for this to work.

When reading from the queue you can do it synchronously or asynchronously -
depends on your app. If you have a dedicated app reading a single queue you
could go for synchronously. We have a dedicated app but it reads multiple
queues, so we use the asynchronous method which. This, however, is how to do
it synchronously:

MessageQueue queue = new MessageQueue(path);
queue.Formatter = new BinaryMessageFormatter();
Object received = queue.Receive();

At this point, you can determine what type of object you are dealing with
using the normal is, as or casting methods (eg "if (received is MyClass)"
etc. You can then take the appropriate action for the specific object you
have received.

Looking at MSDN, DataSet is marked as serializable so I don't see why this
wouldn't work with that. SqlCommand isn't serializable so it wouldn't work
(what would be your aim in doing this, as I can't think of a use for it?).

Hope this helps,
Jon
 
No. You can use the simple SMTPServer class to send the auto-reply upon
sucessful insertion of the new user record into the database.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top