Using MSMQ in Dot Net Using VB.Net Sending and Receiving Messages Using MSMQ in Dot Net: author: Sreedhar Koganti
This example will help you to know how to send and receive messages using MSMQ.
In Dot net, MSMQ provides a powerful set of services. These you can get from
System.Messaging name space. In order to use MSMQ explorer our
Dot Net IDE can also be used to add and remove messages form MSMQ.
How you can use MSMQ:
In order to use MSMQ first you have to install MSMQ in your machine.
Since we are looking at MSMQ in Dot Net,
you need to have Windows 2000 + Service Pack1, MSMQ, .NET Framework Beta1 and
Visual Studio.NET .
How to Use MSMQ:
MSMQ Enterprise can be managed through the MSMQ Explorer.
The MSMQ Explorer provides an interface for managing all machines in a
MSMQ environment from a single point of control. This MSMQ Explorer
you can get from Computer Management/Services and Applications/MessageQueing.
Here I am trying to show a simple Example in VS.Net(VB).
In this example I am using a Windows Application in the C# Environment.
This example will help you to know how to send and receive messages using MSMQ.
In this example I am basically using System.Messaging name space to manage the
messages using msmq.
To do that created a Windows application in VB using VS.net.
In that i take a form module and I kept two buttons one for sending
data and one for receiving data. And I also used a text box.While sending
message i am taking message form the textbox.Button one is used to send message.
Other button will be used to receive the data.
To use the MSMQ name space System.Messaging, you have to go to Tools and
Component Tab, then Drag and Drop the MessageQueue to the Form (Design View).
Once you add it, as shown below, code will be added to your application
Private WithEvents MessageQueue1 As System.Messaging.MessageQueue
The above code will be added in public class Form1 : System.WinForms.Form
and one more important line
Me.MessageQueue1 = New System.Messaging.MessageQueue()
will be added in Private Sub InitializeComponent()
While your application initializes it will create the messageQueue1 .
Once your object has been Created or Instantiated you can use it's methods and
Properties.
In our example we are trying to Send a message to "Smile" Pirvate Queue.
"Smile"is a Queue name which we are using in the example. It's not a system Queue.
The code below is used for sending the message to a queue when you click Button1
button.
****************************
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim q As MessageQueue
If MessageQueue.Exists(".Private$Smile") Then
q = New MessageQueue(".Private$Smile")
Else
q = MessageQueue.Create(".Private$Smile")
End If
Dim BW As New BinaryWriter(New MemoryStream())
bw.Write(textbox1.Text)
Dim m As New Messaging.Message()
m.BodyStream = BW.BaseStream
m.Label = "Sri Test Message"
q.Send(m)
msgbox("Message sent successfully", Microsoft.VisualBasic.MsgBoxStyle.Information,
"MSMQ Message")
End Sub
******************************
When you click the command one first i am checking for Queue if queue is not
there
then i am creating the Queue , if queue is the i am sending the message to the
queue
usingn Send method.
Send Can basically will have two arguments a) Message b) Label for that message.
Button Two will help you to receive the messages:
******************************
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim q As MessageQueue
Dim m As Messaging.Message
Dim Br As BinaryReader
Dim x As String
If MessageQueue.Exists(".Private$Smile") Then
q = New MessageQueue(".Private$Smile")
Else
q = MessageQueue.Create(".Private$Smile")
End If
Try
m = q.Receive(New TimeSpan(0, 0, 3))
Br = New BinaryReader(m.BodyStream)
x = New String(Br.ReadChars(CInt(m.BodyStream.Length)))
Catch
x = "(no message)"
End Try
msgbox(x, Microsoft.VisualBasic.MsgBoxStyle.Information, "MSMQ Message")
End Sub
********************************
If you look into the code, basically I am testing for the existence of the Smile
Private Queue.
If it is not there, I am popping up a message box. If it is there, then I am
instantiating
a PostMessage as a Message Queue.
Here I have to explain one thing. The message object holds the result of the
received message.
So here I am using m as the message object, and we also created a TimeSpan object
from the
System name space. The TimeSpan object will tell how long to wait for the Receive
method before timing out (hours, minutes, seconds).
Before .Net, we could simply set a message object Body property to a string value.
But in Dot Net the data will be automatically wrapped in XML using SOAP encoding.
It makes consistent with other languages with in Dot Net.
Note:
Take care with the following Steps:
1. Must install MSMQ on your machine.
2. Drag and Drop the Message Queue into your Design Form.
3. You must be using System.Messaging;
4. Must declare a message for receiving messages. Here we declared as m.
5. Build the code before running.
Author:
Sreedhar Koganti
Working in Unisys as a Technical Team Lead
Expertixe in ASP,COM+,XML,SOAP,NTService,VB, Drop of .Net
Article Writer in asptoday.com,Aspalliance.com,aspfree.com
***********************************************************
|
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Articles More By aspfree developerWorks - FREE Tools! | Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
| | | | Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations. FREE! Go There Now!
| | | | WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
| | | | Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
| | | | Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
| | | | The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
| | | | User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
| | | | Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
| | | | With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |