How to: Send Simple Messages
Using an instance of thecomponent, you can send simple messages to a queue in as little as two lines of code. When you send a simple message, you must perform the following actions:
Create a connection to the queue to which you want to send a message.
Specify the appropriate formatter for the data you want to send. The formatter controls what type of data can be sent in a message and how that data is persisted. In simple cases, the default formatter is acceptable. For more information, see.
Call themethod, passing the object to be sent.
Creating a Connection to the Queue
After you decide which queue you want to communicate with, you need to create an instance of the MessageQueue component that references the queue you want to use. You can create this component either from the Toolbox, from Server Explorer to your project, or by using theconstructor.
To create a connection to the queue you want to communicate with
Create an instance of the MessageQueue component. For more information, see.
Use the component'sproperty to connect to the queue with which you want to communicate by the queue path, format name, or label.
If you created your component from Server Explorer, the Path property is set automatically to the queue path for that queue.
Providing the Data to Be Sent
Each MessageQueue component contains a series of default properties that are applied to all messages sent by that component, unless you specify otherwise in an individual message. In the simplest scenario, you can send a simple message to the queue using the default properties set for the component. For more information on these properties, see.
You use the Send method to specify a message and send it to the queue. You can send objects, primitive data types, streams, and other kinds of data in a simple message.
The MessageQueue component takes the data you specify in the Send method's argument, persists it into a message, and sends the message to the specified queue.
You can also use theobject to send far more complex messages to a queue. In addition, you can send messages as part of a transaction. For more information, see .
To send a simple message
In the Properties window for your MessageQueue component, accept the default value for the, or set the property to if you have changed the default value.
Use the Send method to send a simple message to your queue, specifying the message as an argument of the method.
// Create a connection to the queue. System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(@".\YourQueue"); // Send an integer. mq.Send(1); // Send a string. mq.Send("Hello world");
// Create a connection to the queue. System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(".\\YourQueue"); // Send an integer. mq.Send(new java.lang.Integer(1)); // Send a string. mq.Send("Hello world");
This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Operating System > Message Queues. For more information, see.