Sending Priority and Recoverable Messages C# Help

Messages can be prioritized by setting the Priority property of the Message class. If messages are specially configured, a Message object must be created where the body of the message is passed in the constructor.

In the example, the priorityisset to MessagePriority. High if the checkBoxPriority check box is checked. MessagePriority is an enumeration that allows you to set values from Lowest (0) to Highest (7). The default value, Normal, has a priorityvalue of 3.

, .
To make the message recoverable, the property Recoverable IS set to true:

Capture

Figure 45-11

Figure 45-11

Course Order Message Receiver

The design view of the Course Order receiving application that reads messages from the queue is shown in Figure 45-12.This application displays labels of every order in the listOrders list box. When an order is selected, the content of the order is displayed with the controls on the right side of the application.

Figure 45-12

Figure 45-12

In the constructor of the window class CourseOrderReceiverWindow, the MessageQueue object is created that references the same queue that was used with the sending application. For reading messages, the XmlMessageFormatter with the types that are read is associated with the queue using the Formatter property.

To display the available messages in the list, a new thread is created that peeks at messages in the background the thread’s main method is PeekMessages .

You can read more about threads In Chapter 19, “Threading and Synchronization.”

sa

The thread’s main method, PeekMessages(),uses the enumerator of the message queue to display all messages. Within the while loop,the messages Enumera tor checks to see if there is a new message in .the queue. If there is no message in the queue, the thread waits three hours for the next message to arrive before it exits.

To display every message from the queue in the listbox, the thread cannot directly write the text to the listbox, but needs to forward the call to the listbox’s creator thread.Because Windows Forms controls are bound to a single thread,only the creator thread is allowed to access methods and properties.The Invoke() method forwards the request to the creator thread:

Capture

Capture

The ListBox control has the SelectedlndexChanged event associated with the method listOrders SelectionChanged().this method gets the LabelId Mapping object from the current selection,and – uses the ID to peek at the message once more with the PeekByld() method. Then the content of the message is displayed in the TextBox control.Because by default the priority of the message is not read, the property MessageReadPropertyFil ter must be set to receive the Priority:

Capture

When the Process Order button is clicked, the handler method OnProcessOrder() is invoked. Here again, the currently selected message from the list box is referenced, and the message is removed from the queue by calling the method ReceiveByld():

Capture

Figure 45-13 shows the running receiving application that lists three orders in the queue, and one order is currently selected.

Figure 45-13

Figure 45-13

Posted on November 2, 2015 in Message Queuing

Share the Story

Back to Top