/// <summary> /// Event handler for the ReceiveCompleted event /// - The messages received asynchronously are processed /// </summary> private void QueueReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult) { try { // End the Asynchronous Receive Operation System.Messaging.Message message = this.mqdemoAck.EndReceive(asyncResult.AsyncResult); //Only Process Messages that contain the CD Orders if (message.Body is CDOrder) { CDOrder cdOrder = message.Body as CDOrder; object[] orders = new object[1]; orders[0] = cdOrder; //call to modify listbox UI on main thread this.listBoxProc.Invoke(new UpdateListBoxDelegate(UpdateListBox), orders); } else { MessageBox.Show("Message in Queue is not a CD Order"); } } catch (MessageQueueException e) { MessageBox.Show (String.Format(System.Globalization.CultureInfo.CurrentCulture, "Failed to receive Message: {0} ", e.ToString())); } //Begin the next Asynchronous Receive Operation this.mqdemoAck.BeginReceive(); }
/// <summary> /// Click event handler for the Display button /// - Displays the Details about the CDs that have been /// processed by the server. /// </summary> private void btnDisplay_Click(object sender, EventArgs e) { CDOrder cdOrder = (CDOrder)listBoxProc.SelectedItem; //nothing selected if (cdOrder == null) { return; } string display = "Title :" + cdOrder.Product.Title + "\n" + "Quantity: " + cdOrder.Quantity.ToString(System.Globalization.CultureInfo.CurrentUICulture) + "\n" + "Company: " + cdOrder.Customer.Company + "\n" + "Contact: " + cdOrder.Customer.Contact + "\n" + "Shipped: " + cdOrder.OrderTime.ToString ("g", System.Globalization.CultureInfo.CurrentUICulture); MessageBox.Show(display); listBoxProc.Items.Remove(cdOrder); if (listBoxProc.Items.Count == 0) { this.BackColor = Color.Red; } }
/// <summary> /// SelectedIndex Changed event handler for ListBox /// - Peeks at the Message selected in the Queue /// and Display information about the CD order in the queue /// </summary> private void lBOrders_SelectedIndexChanged(object sender, EventArgs e) { LabelIdMapping labelID = (LabelIdMapping)lBOrders.SelectedItem; if (labelID == null) { return; } MessagePropertyFilter filter = new MessagePropertyFilter(); filter.SetDefaults(); filter.Priority = true; mqOrderQueue.MessageReadPropertyFilter = filter; System.Messaging.Message message = mqOrderQueue.PeekById(labelID.Id); if (message.Body is CDOrder) { if (message.Priority > MessagePriority.Normal) { lblFast.Visible = true; } else { lblFast.Visible = false; } CDOrder cdOrder = message.Body as CDOrder; tBCDTitle.Text = cdOrder.Product.Title; tBCount.Text = cdOrder.Quantity.ToString(); tBCompany.Text = cdOrder.Customer.Company; tBContact.Text = cdOrder.Customer.Contact; tBSent.Text = cdOrder.OrderTime.ToString("f"); btnProcess.Enabled = true; } else { MessageBox.Show("The selected Item is not a book order"); } }
private void btnProcess_Click(object sender, EventArgs e) { //The LabelIdMapping helps to map the Label of a message with the message Id. //This enables each message to be uniquely identified LabelIdMapping labelID = (LabelIdMapping)lBOrders.SelectedItem; System.Messaging.Message message = mqOrderQueue.ReceiveById(labelID.Id); lBOrders.Items.Remove(labelID); lBOrders.SelectedIndex = -1; btnProcess.Enabled = false; System.Messaging.Message response = new System.Messaging.Message(); CDOrder ackOrder = message.Body as CDOrder; if (null != ackOrder) { ackOrder.OrderTime = DateTime.Now; response.Body = ackOrder; response.Label = message.Label; response.CorrelationId = message.Id; message.ResponseQueue.Send(response); tBCDTitle.Text = String.Empty; tBCount.Text = String.Empty; tBCompany.Text = String.Empty; tBContact.Text = String.Empty; tBSent.Text = String.Empty; MessageBox.Show("The order was shipped"); } else { MessageBox.Show("There is an unexpected item in the Queue"); } }
/// <summary> /// Click event handler for the Submit button /// </summary> private void btnSubmit_Click(object sender, EventArgs e) { try { this.btnSubmit.Enabled = false; //Check if server name has been specified if (textBoxServer.Text == String.Empty) { MessageBox.Show("Enter a name for the Server"); return; } //Retrieve the IP Address of the device. string deviceIP = String.Empty; IPAddress[] addresses = Dns.GetHostByName(Dns.GetHostName()).AddressList; for (int i = 0; i < addresses.Length; ++i) { IPAddress addr = addresses[i]; if (addr.AddressFamily == AddressFamily.InterNetwork) { deviceIP = addr.ToString(); break; } } if (deviceIP == String.Empty) { MessageBox.Show("Unable to retrieve Device IP"); return; } //Create a CD object that will be sent to the Server's queue CDOrder cdOrder = new CDOrder(); cdOrder.Quantity = Convert.ToInt32(tBQty.Text, System.Globalization.CultureInfo.CurrentUICulture); cdOrder.Product = new CompactDisc(this.comboCD.SelectedItem.ToString()); cdOrder.Customer = new Customer(tBCompName.Text, tBContact.Text); cdOrder.OrderTime = dateTimePicker1.Value; // Send the CD object to a Queue on the server //Create an instance of the MessageQueue class that abstracts a //connection to a queue specified on the server MessageQueue orderQueue = new MessageQueue (String.Format(System.Globalization.CultureInfo.InvariantCulture, @"FORMATNAME:DIRECT=OS:{0}\private$\mqsample", textBoxServer.Text)); //A recoverable message is created and the priority of the message //is set accordingly. System.Messaging.Message message = new System.Messaging.Message(cdOrder); message.Recoverable = true; //The response Queue is specified to receive acknowledgement from the // server message.ResponseQueue = new MessageQueue (String.Format(System.Globalization.CultureInfo.InvariantCulture, @"FORMATNAME:DIRECT=TCP:{0}\private$\mqdemoAck", deviceIP)); if (chkFast.Checked) { message.Priority = MessagePriority.High; } // Send the Message to the Queue orderQueue.Send (message, String.Format(System.Globalization.CultureInfo.InvariantCulture, "CD Order ({0})", cdOrder.Customer.Contact)); this.ClearControls(); } catch (FormatException ex) { MessageBox.Show(ex.Message, "Conversion Error"); this.ClearControls(); } catch (System.Net.Sockets.SocketException ex) { MessageBox.Show (String.Format(System.Globalization.CultureInfo.CurrentCulture, "Failed to resolve Host Name:\n {0}", ex.Message)); this.ClearControls(); } catch (MessageQueueException ex) { MessageBox.Show(ex.Message, "Error sending Message"); this.ClearControls(); } finally { this.btnSubmit.Enabled = true; } }
/// <summary> /// Function to update the ListBox with incoming CD Orders /// </summary> private void UpdateListBox(CDOrder order) { listBoxProc.Items.Add(order); this.BackColor = Color.Green; }