C # Message Queue application-1

Source: Internet
Author: User
Tags msmq

Introduction

Microsoft recently launched a new platform for Generating integrated applications-Microsoft
. NET Framework .. NET framework allows developers to use any programming language to quickly generate and deploy Web
Services and applications. Microsoft Intermediate Language (MSIL) and real-time
(JIT) compilers enable this language-independent framework.

A new Programming Language C # (read "C sharp") is also available with the. NET Framework ").
C # is a simple, novel, object-oriented, and type-safe programming language. Using the. NET Framework
And C # (except Microsoft? Visual Basic? And Managed C ++), user
Can I write a powerful Microsoft Windows? And Web applications and services. This article
Provides such a solution, which focuses on the. NET Framework and C # Instead of programming languages.
Statement. The introduction to C # can be found in "C # Introduction and Overview (English.

Recent article "MSMQ: scalable and highly available Load Balancing solutions (English )"
This paper introduces a solution for scalable load balancing of high availability Message Queue (MSMQ ).
Solution architecture. This solution involves the use of Windows Services as smart consumption
Information router development solution. In the past, only Microsoft Visual C ++
Programmers can achieve this, but the emergence of the. NET Framework has changed this situation. From the Solution Below
You can see this.

. NET Framework Application

The solution described here is a Windows service used to process several message queues;
Each queue is processed by multiple threads (receiving and processing messages ). Processing Procedure
Use the loop technique or application-specific values (Message AppSpecific attribute) from the target queue Column
Route messages in the table and call the component method using message attributes. (The sample process also belongs to this type.
Situation .) In the latter case, the component must be able to implement the given interface IWeb
To handle errors, the application must send messages that cannot be processed to the error queue.

The structure of the Message application is similar to that of the previous Active Template Library (ATL) application.
The main difference between them is the code encapsulation for managing services and the use of. NET Framework components.
. To create a Windows Service, the. NET Framework user only needs to create
(From System. ServiceControl assembly) inherited class. This is not surprising because. NET
The framework is object-oriented.

Application Structure

The main class in the application is ServiceControl, which inherits from ServiceBase
. Therefore, it must implement the OnStart and OnStop methods, as well as optional OnPause and
OnContinue method. In fact, classes are constructed in the static method Main:

Using System;
Using System. ServiceProcess;

Public class ServiceControl: ServiceBase
{
// Create the primary entry point of the service object
Public static void Main ()
{
ServiceBase. Run (new ServiceControl ());
}

// Define the construction object of service parameters
Public ServiceControl ()
{
CanPauseAndContinue = true;
ServiceName = "MSDNMessageService ";
AutoLog = false;
}

Protected override void OnStart (string [] args ){...}
Protected override void OnStop (){...}
Protected override void OnPause (){...}
Protected override void OnContinue (){...}
}

ServiceControl class creates a series of CWorker objects, that is, for each
MQ creates an instance of the CWorker class. Number of threads required for queue processing in Definition
The CWorker class creates a series of CWorkerThread objects in sequence. CWorkerThread
A processing thread created by class will execute the actual service work.

The main purpose of using the CWorker and CWorkerThread classes is to confirm the service control Start,
Stop, Pause, and Continue commands. Because these processes must be non-blocking and run commands
Will be executed on the background processing thread.

CWorkerThread is an abstract class, which is,
CWorkerThreadRoundRobin and CWorkerThreadAssembly inherit. These classes do not
Process messages in the same way. The first two classes process messages by sending messages to the other Queue (
The same is the method used to determine the path of the receiving queue. The last class uses the message attribute to call
Use the component method.

The error handling in the. NET Framework is based on the Exception of the base class. When the system
When sending or capturing errors, these errors must be Classes exported from exceptions. CWorker
The ThreadException class is implemented by attaching additional attributes (used to define
Whether the Service should continue running) to expand the base class.

Finally, the application contains two structures. These value types define
Runtime parameters to simplify the structure of CWorker and CWorkerThread objects. Use Value Type
Structure (rather than reference type classes) ensures that these runtime parameters maintain values (instead
Is referenced ).

IWebMessage Interface

One of the implementations of CWorkerThread is a class that calls component methods. This name is
The class of CWorkerThreadAssembly uses the IWebMessage interface to define services and components
.

With the current version of Microsoft Visual Studio? Different, the C # interface can be
Explicit definition in the language, without the need to create and compile the IDL file. C # IWebMessage Interface
Definition:
Public interface IWebMessage
{
WebMessageReturn Process (string sMessageLabel, string sMessage
Body, int iAppSpecific );
Void Release ();
}

The Process method in the ATL code is specified for message processing. Return of the Process Method
The return code is defined as the enumeration type WebMessageReturn:

Public enum WebMessageReturn
{
ReturnGood,
ReturnBad,
ReturnAbort
}

The enumeration is defined as follows: "Good" indicates that the message is processed continuously, and "Bad" indicates that the message is written to the error queue,
Abort indicates that the processing is terminated. The Release method provides a way for services to easily clear class instances.
Because only the destructor of the class instance is called during garbage collection, make sure that all
Classes with expensive resources (such as database connections) have a method that can be called before the analysis structure,
It is a good idea to release these resources.

Namespace

Here, we will briefly introduce the namespace. The namespace is allowed in both internal and external representations.
Organizes applications into logical elements. All codes in the service are included in the MSDNMessage
Service. Service namespace. Although the service code is contained in several files
Because they are included in the same namespace, you do not need to reference other files.

Because the IWebMessage Interface is included in the MSDNMessageService. Interface Name
So the Thread class using this interface has an interface namespace.

Service

The purpose of an application is to monitor and process a message queue.
Different processes. Applications are implemented as Windows Services.

ServiceBase class

As mentioned above, the basic structure of a service is a class inherited from ServiceBase. Important Methods
Including OnStart, OnStop, OnPause, and OnContinue.
A service control operation corresponds directly. The OnStart method is used to create a CWorker object,
The CWorker class creates a CWorkerThread object, and then creates and executes the service in the object.
The working thread.

The service runtime configuration (and the attributes of the CWorker and CWorkerThread objects) is
Maintained in the XML-based configuration file. It has the same name as the. exe file,
With a. cfg suffix. The configuration example is as follows:
<〈? Xml version = "1.0 "? > 〉
<Configuration> 〉
<ProcessList> 〉
<ProcessDefinition
ProcessName = "Worker1"
ProcessDesc = "Message Worker with 2 Threads"
ProcessType = "AppSpecific"
ProcessThreads = "2"
InputQueue = ". private $ est_load1"
ErrorQueue = ". private $ est_error"> "〉
<OutputList> 〉
<OutputDefinition OutputName = ". private $ est_out11"/> "/〉
<OutputDefinition OutputName = ". private $ est_out12"/> "/〉
</OutputList> 〉
</ProcessDefinition> 〉
<ProcessDefinition
ProcessName = "Worker2"
ProcessDesc = "Assembly Worker with 1 Thread"
ProcessType = "Assembly"
ProcessThreads = "1"
InputQueue = ". private $ est_load2"
ErrorQueue = ". private $ est_error"> "〉
<OutputList> 〉
<OutputDefinition OutputName = "C: MSDNMessageServiceMessage
Example. dll "/> "/〉
<OutputDefinition OutputName = "MSDNMessageService. Message
Sample. ExampleClass "/> "/〉
</OutputList> 〉
</ProcessDefinition> 〉
</ProcessList> 〉
</Configuration> 〉

Access to this information through the Config from the System. Configuration assembly
Manager class. Set of information returned by the static Get method. These sets will be enumerated
Obtain a single attribute. The setting of these attribute sets determines the runtime features of the Secondary object. Besides
1. In the configuration file, you should also create an element file that defines the XML file structure and reference
The Metafile located in the machine. cfg configuration file on the server:

<〈? Xml version = "1.0 "? > 〉
<MetaData xmlns = "x-schema: CatMeta. xms"> "〉
<DatabaseMeta InternalName = "MessageService"> "〉
<ServerWiring Interceptor = "Core_XMLInterceptor"/> "/〉
<Collection
InternalName = "Process" PublicName = "ProcessList"
PublicRowName = "ProcessDefinition"
SchemaGeneratorFlags = "EMITXMLSCHEMA"> "〉
<Property Interna

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.