Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

JMS and MSMQ

.pdf
Скачиваний:
11
Добавлен:
17.02.2016
Размер:
198.38 Кб
Скачать

MSMQ

Microsoft Message Queuing (abbreviated as MSMQ) is a messaging system that has been part of the Windows operating system almost since its inception. It is perhaps not very common for users to know that a full-fledged messaging system is readily available in that operating system, but the fact is that applications can rely on MSMQ to implement asynchronous message exchanges with other local and remote applications.

In MSMQ, the application that produces a message is simply called the sender, and the application that consumes it is called the receiver. The communication channel between sender and receiver is always a queue, and the most common scenario is to use queues for one-to-one communication. MSMQ also supports one-to-many interactions through a reliable IP multicast protocol.

In each machine, there is a component known as Queue Manager that checks whether the message is to be delivered to a local or to a remote queue. If the message is intended for a local queue, the Queue Manager

KBTU, Almaty

MSMQ

inserts the message in that queue; otherwise, the message is placed in an outgoing queue from which it will be dispatched to another machine. Finally, administration queues are used to store message acknowledgments.

KBTU, Almaty

MSMQ

Public and Private Queues

In MSMQ, every queue has a unique name in the local machine. So if two applications on the local machine use the same queue name, it is assumed that they are referring to the same queue. To distinguish between queues in the local machine and queues in a remote machine, the queue name is often preceded by the computer name, in the form: ComputerName\QueueName. If the queue is local, one can use a shorter notation, as in: .\QueueName. Also, there are private queues and public queues, so the name for a queue may include both the computer name and an indication of whether the queue is private, as in the form ComputerName\PRIVATE$\QueueName or

.\PRIVATE$\QueueName if the queue is local. If PRIVATE$ is missing in the queue name, it is assumed that the queue is public.

KBTU, Almaty

MSMQ

Distributed Transactions in MSMQ

MSMQ relies on another external component to manage distributed transactions - known as Distributed Transaction Coordinator (DTC). The DTC service provides a transaction scope for such distributed transactions, and MSMQ can use the DTC to support message transactions between client applications and the local Queue Manager. The Structure of MSMQ Messages

Unlike JMS, where messages are clearly divided into header, properties, and body, in MSMQ the whole content of messages is a set of name–value pairs called properties. In this context, the body is just one of the properties in a message, and its value may be empty or it may contain basic data types, text, serializable objects, or simply an array of bytes. In addition to the body, a MSMQ message may carry a relatively large set of properties, where the most important are summarized next:

KBTU, Almaty

MSMQ

AcknowledgeType

AdministrationQueue specifies the queue to which the acknowledgments should be delivered.

ArrivedTime provides the time at which the message arrived at the destination

The property Authenticated indicates whether the message contains a digital signature.

The properties Body, BodyType, and BodyStream concern the body of the message.

CorrelationId specifies the message id of a previous message to which the present message is related to. This is typically usually in request– response scenarios

DestinationQueue is set by the sending application by providing a reference to the destination queue

If message authentication is being used, DigitalSignature will contain the digital signature for the present message.

KBTU, Almaty

MSMQ

Formatter specifies how the message body will be formatted after being serialized.

The Id property contains the message id that distinguishes the present message from every other. In MSMQ, the message id is generated by combining a machine identifier with a unique identifier for the message within that machine.

MessageType indicates whether the present message is a regular message, an acknowledgment, or a report. Report messages are used when reporting is enabled; in this case, MSMQ will send a report message (to a dedicated report queue)

Priority specifies the message priority, from highest to lowest, on a scale of eight levels. 0-7

In a request–response interaction, if the present message represents the request, then the ResponseQueue property specifies the queue to which the response should be sent.

KBTU, Almaty

MSMQ

The property TimeToBeReceived specifies a time limit for the message to be received from the destination queue.

TransactionId applies to messages that have been sent within a transaction.

C#

In fact, the System.Messaging namespace provides a convenient set of classes that allow .NET applications to use message queues and to send and receive messages through MSMQ.

// order using System;

namespace MSMQSolution { public class PurchaseOrder { public string Product { get; set; } public int Quantity { get; set; } public DateTime Date { get; set; }

public PurchaseOrder() { Date = DateTime.Now; }

}

}

KBTU, Almaty

MSMQ

using System;

using System.Messaging; namespace MSMQSolution { class Program {

static void Main(string[] args) { MessageQueue mq;

string queueName = @".\private$\purchaseorders"; if (MessageQueue.Exists(queueName)) {

Console.WriteLine("Opening queue: {0}", queueName); mq = new MessageQueue(queueName);

}

else {

Console.WriteLine("Creating queue: {0}", queueName); mq = MessageQueue.Create(queueName);

}

while (true) {

Console.WriteLine("Create a new purchase order"); PurchaseOrder PO = new PurchaseOrder(); Console.Write("Product: ");

PO.Product = Console.ReadLine(); Console.Write("Quantity: ");

PO.Quantity = int.Parse(Console.ReadLine()); Console.WriteLine("Date: {0}", PO.Date);

KBTU, Almaty

MSMQ

Message msg = new Message(PO); msg.Priority = MessagePriority.Normal; mq.Send(msg, "PurchaseOrder"); Console.WriteLine("Message has been sent!");

}

}

}

}

KBTU, Almaty

MSMQ

// code for the receiver of purchase orders using System;

using System.Messaging; namespace MSMQSolution { class Program {

static void Main(string[] args) {

string queueName = @".\private$\purchaseorders"; Console.WriteLine("Receiving on queue: {0}", queueName); MessageQueue mq = new MessageQueue(queueName); System.Type[] types = new Type[] { typeof(PurchaseOrder) }; mq.Formatter = new XmlMessageFormatter(types);

while (true) {

Message msg = mq.Receive(); Console.WriteLine("Received new message!"); PurchaseOrder PO = (PurchaseOrder)msg.Body; Console.WriteLine("Product: {0}", PO.Product); Console.WriteLine("Quantity: {0}", PO.Quantity); Console.WriteLine("Date: {0}", PO.Date);

}

}

}

}

KBTU, Almaty

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]