i got an assignment asking me to fill in the blanks for c# coding. basically the assignment is all about message queueing. here i paste the code that need to be filled up through the code as follows:
// TODO : Step N. <what to do>
there are 5 blanks to fill up...please help me...
// this is BOClientOrders.cs file
using System;
using System.Messaging;
using ITEE.OrderMQ.Messages;
namespace ITEE.OrderMQ.Client.BusinessObjects
{
/// <summary>Client Business object to process orders</summary>
public class BOClient_Orders
{
/// <summary>Message queue path name</summary>
string _mqPath;
public BOClient_Orders()
{
_mqPath = Config.QueuePath;
}
/// <summary>Send a MSMQ message to the Order server.</summary>
/// <param name="msg"></param>
protected void SendMessage(Message msg)
{
// TODO: Step 1. Send msg message to message queue path _mqPath.
System.Messaging.MessageQueue mq =
new System.Messaging.MessageQueue (@_mqPath);
// Sends a message to queue path _mqPath.
mq.Send(msg);
}
/// <summary>
/// Place a new Order
/// </summary>
/// <param name="orderNo"></param>
/// <param name="accountNo"></param>
/// <param name="customerName"></param>
/// <param name="stockNo"></param>
/// <param name="stockQty"></param>
/// <param name="stockDescription"></param>
/// <param name="totalPrice"></param>
public void PlaceAnOrder
(
int orderNo,
string customerName,
string stockDescription,
decimal totalPrice
)
{
Message msg = new Message();
msg.AppSpecific = (int)MessageTypes.PlaceAnOrder;
// Message Label that appears in MQ admin tools
msg.Label = "PlaceOrder: " + orderNo;
msg.Body = new Order(orderNo, customerName, stockDescription, totalPrice);
SendMessage(msg);
}
/// <summary>
/// Update an existing order
/// </summary>
/// <param name="orderNo"></param>
/// <param name="accountNo"></param>
/// <param name="customerName"></param>
/// <param name="stockNo"></param>
/// <param name="stockQty"></param>
/// <param name="stockDescription"></param>
/// <param name="totalPrice"></param>
public void UpdateAnOrder
(
int orderNo,
string customerName,
string stockDescription,
decimal totalPrice
)
{
Message msg = new Message();
msg.AppSpecific = (int)MessageTypes.UpdateAnOrder;
// Message Label that appears in MQ admin tools
msg.Label = "UpdateOrder: " + orderNo;
msg.Body = new Order(orderNo, customerName, stockDescription, totalPrice);
SendMessage(msg);
}
/// <summary>Cancel an existing order.</summary>
/// <param name="orderNo"></param>
public void CancelAnOrder(int orderNo)
{
Message msg = new Message();
msg.AppSpecific = (int)MessageTypes.CancelAnOrder;
msg.Label = "CancelOrder: " + orderNo;
msg.Body = orderNo;
SendMessage(msg);
}
}
}
// this is OrderMQServer.cs file
using System;
using System.Messaging;
using ITEE.OrderMQ.Messages;
namespace ITEE.OrderMQ.Server
{
/// <summary>Message Queue Server for Customer Orders.</summary>
public class OrderMQServer : IDisposable
{
/// <summary>Message queue path name</summary>
string _mqPath;
/// <summary>Message queue used to register for callbacks.</summary>
MessageQueue _mq;
/// <summary>The main entry point for the application.</summary>
[STAThread]
static void Main(string[] args)
{
OrderMQServer service = new OrderMQServer(Config.QueuePath);
// Service opens message queue connection and starts listening for messages.
service.Start();
Console.WriteLine("** Press ENTER to stop the MQ Order Service **");
Console.ReadLine();
// Service stops listening for messages and closes message queue connection.
service.Stop();
}
/// <summary>Create a Message Queue Server for Customer Orders</summary>
/// <param name="mqPath">Message Queue Path</param>
public OrderMQServer(string mqPath)
{
if (mqPath == null)
{
throw new ArgumentNullException("mqPath");
}
_mqPath = mqPath;
}
/// <summary>Sart the service.</summary>
/// <remarks>Opens the message queue and register callback listener.</remarks>
public void Start()
{
// TODO: Step 2. Create message queue if it does not exist
// The path to the message queue is in: _mqPath
if (!MessageQueue.Exists(@_mqPath))
{
MessageQueue.Create(@_mqPath); // Path created.Path to message queue is mqPath.
}
// TODO: Step 3a. Open and configure the message queue and assign it to: _mq
_mq = new System.Messaging.MessageQueue(@_mqPath); // Assigning the message queue to the mqPath
// TODO: Step 3b. Register a callback method to be invoked
// when new messages arrives.
_mq.PeekCompleted += new PeekCompletedEventHandler(OnPeekCompleted); // Register receive Event Callback for when message arrives
_mq.BeginReceive(); // Begin async Receive()
}
/// <summary>IDispose interface.</summary>
public void Dispose()
{
Stop();
}
/// <summary>Stop Server</summary>
public void Stop()
{
if (_mq != null)
{
// No need to deregister the callback event in this case.
// Just Close or Dispose the message queue to stop listening for messages.
_mq.Dispose();
_mq = null;
}
}
/// <summary>Called when a new message arrives on the queue.</summary>
/// <param name="source"></param>
/// <param name="asyncReceive"></param>
private void OnPeekCompleted (Object source, PeekCompletedEventArgs asyncReceive)
{
// TODO: Step 4a. Receive messages from the message queue
Message msg = _mq.EndPeek(asyncReceive.AsyncResult);
// TODO: Step 4b. Call ProcessReceivedMessage() to process each message.
try
{
ProcessReceivedMessage(msg); // Process each received message
}
catch(Exception)
{
Console.WriteLine();
}
// TODO: Step 4c. Prepare to receieve the next message.
finally
{
_mq.BeginPeek();
}
}
/// <summary>Process a received message.</summary>
/// <param name="msg"></param>
private void ProcessReceivedMessage(Message msg)
{
// Get the message type from the "AppSpecific" message property.
MessageTypes msgType = (MessageTypes) msg.AppSpecific;
// TODO: Step 5. Based on the message type in msgType call one of ...
//
// a. BusinessObjects.BOServer_Orders.PlaceAnOrder()
if (msgType.Equals(MessageTypes.PlaceAnOrder))
{
msg.Formatter = new XmlMessageFormatter(new Type[] {typeof(Order)});
Order order = (Order)msg.Body;
BusinessObjects.BOServer_Orders.PlaceAnOrder(order.OrderNo, order);
};
// OR
// b. BusinessObjects.BOServer_Orders.UpdateAnOrder()
if (msgType.Equals(MessageTypes.UpdateAnOrder))
{
msg.Formatter = new XmlMessageFormatter(new Type[] {typeof(Order)});
Order order = (Order)msg.Body;
BusinessObjects.BOServer_Orders.UpdateAnOrder(order.OrderNo, order);
};
// OR
// c. BusinessObjects.BOServer_Orders.CancelAnOrder()
if (msgType.Equals(MessageTypes.CancelAnOrder))
{
msg.Formatter = new XmlMessageFormatter(new Type[] {typeof(int)});
BusinessObjects.BOServer_Orders.CancelAnOrder((int)msg.Body);
};
}
}
}