How to use the MQ?
2006-10-17 10:32
531 查看
How to use the MQ?
===================================================================================
Here is an example to use the MQ:
using System;
using System.Messaging;
using System.Drawing;
using System.IO;
namespace ConsoleApplication1
{
---------------------------------------------------
public class Order
{
public int orderId;
public DateTime orderTime;
};
---------------------------------------------------
public class MyNewQueue
{
public static void Main()
{
MyNewQueue myNewQueue = new MyNewQueue();
CreateQueue(".\\myQueue");
myNewQueue.SendMessage();
// Receive a message from a queue.
myNewQueue.ReceiveMessage();
return;
}
public static void CreateQueue(string queuePath)
{
try
{
if(!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(@".\private$\myQueue");
}
else
{
Console.WriteLine(queuePath + " already exists.");
}
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
}
public void SendMessage()
{
try
{
Order sentOrder = new Order();
sentOrder.orderId = 3;
sentOrder.orderTime = DateTime.Now;
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
Message myMessage = new Message(sentOrder);
myQueue.Send(myMessage);
}
catch(ArgumentException e)
{
Console.WriteLine(e.Message);
}
return;
}
public void ReceiveMessage()
{
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
myQueue.Formatter = new XmlMessageFormatter(new Type[]{typeof(ConsoleApplication1.Order)});
try
{
Message myMessage = myQueue.Receive();
Order myOrder = (Order)myMessage.Body;
Console.WriteLine("Order ID: " +
myOrder.orderId.ToString());
Console.WriteLine("Sent: " +
myOrder.orderTime.ToString());
}
catch (MessageQueueException)
{
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
return;
}
}
---------------------------------------------------
}
===================================================================================
Here is an example to use the MQ:
using System;
using System.Messaging;
using System.Drawing;
using System.IO;
namespace ConsoleApplication1
{
---------------------------------------------------
public class Order
{
public int orderId;
public DateTime orderTime;
};
---------------------------------------------------
public class MyNewQueue
{
public static void Main()
{
MyNewQueue myNewQueue = new MyNewQueue();
CreateQueue(".\\myQueue");
myNewQueue.SendMessage();
// Receive a message from a queue.
myNewQueue.ReceiveMessage();
return;
}
public static void CreateQueue(string queuePath)
{
try
{
if(!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(@".\private$\myQueue");
}
else
{
Console.WriteLine(queuePath + " already exists.");
}
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
}
public void SendMessage()
{
try
{
Order sentOrder = new Order();
sentOrder.orderId = 3;
sentOrder.orderTime = DateTime.Now;
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
Message myMessage = new Message(sentOrder);
myQueue.Send(myMessage);
}
catch(ArgumentException e)
{
Console.WriteLine(e.Message);
}
return;
}
public void ReceiveMessage()
{
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
myQueue.Formatter = new XmlMessageFormatter(new Type[]{typeof(ConsoleApplication1.Order)});
try
{
Message myMessage = myQueue.Receive();
Order myOrder = (Order)myMessage.Body;
Console.WriteLine("Order ID: " +
myOrder.orderId.ToString());
Console.WriteLine("Sent: " +
myOrder.orderTime.ToString());
}
catch (MessageQueueException)
{
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
return;
}
}
---------------------------------------------------
}
相关文章推荐
- How to use C# code to get the windows components
- the "find" which you should kown that how to use it
- How to use Ruijie to contact to the Internet on Ubuntu
- How To Use the C Run-Time
- how to use the MySQL subquery to write complex queries
- How to use the Service Bus relay service
- HowTo use the CDT to navigate Linux kernel source
- How To Use the Linux Fuser
- How to use the ZooKeeper driver for ServiceGroup in OpenStack Nova
- How to use isInEditMode() to see layout with custom View in the editor
- [XAF] How to use the Allow/Deny permissions policy in the existing project
- 图像分割crop设置 caffe fully convolutional cnn - how to use the crop parameters
- Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?
- [Ubuntu] 如何在VirtualBox OSE中使用share folders -- How to use the share folders in VirtualBox OSE
- How to use the Segmented Control
- How to use the non-modifiable buffer descriptor — TBufC
- How to use the function of assembly.
- How to Use the restrict Qualifier in C
- [转载]如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- How to Use the Format Fortran Indent Plugin for Code::Blocks ?