Message Queuing tool Class (MSMQ)

Source: Internet
Author: User
Tags serialization msmq

To do is simplify the calling code for MSMQ and make it fungible, and when implemented, the calling Message Queuing code becomes the following:

Queueservice srv = Queueservice.instance ();
    
Checks whether the storage DTO1 queue exists, and automatically establishes the SRV if it does not exist
. Prepare<dto1> ();
    
Send message SRV of type DTO1
. Send<dto1> (New DTO1 () {  p1= "1",  p2= "2" 

});
    
Sends a message of type DTO1 and saves the sent message ID to string msgid=srv in the msgId variable
. Send<dto1> (New DTO1 () {p1 = "1", p2 = "2"});
    
Receive End message
DTO1 msg = srv. Receive<dto1> ();
    
Receives the end message and saves the message ID in the MSGID variable
DTO1 msg = srv. Receive<dto1> (ref msgId);
    
Sends a reply message and specifies that the reply message is specific to the reply message SRV for a particular message ID
. Sendresponse<dto1> (msg, msgId);
    
A reply message that receives a specific message ID
msg=srv. Receiveresponse<dto1> (MSGID);

The main areas are 2:

MSMQ Message size limit breakthrough (4M breakthrough)

serialization, deserialization of generic T-objects

Break the size limit

If the size is within 4M, direct MSMQ Encapsulation (messagelocation=inqueue)

If outside 4M, it is encapsulated by a network share file (messagelocation=innetwork)

serialization, deserialization of generic T-objects

Fixed residence The object type to be passed is Messagewrapper

Embed the other objects that the user wants to pass inside the Messagewrapper and the corresponding type, module name, so that Messagewrapper can be automatically xml and anti-XML

The Messagewrapper code is as follows:

public class Messagewrapper {private Sharefilebroker filebroker;
            Public Messagewrapper () {persistencetype = Messagelocation.inqueue;
        Filebroker = new Sharefilebroker (FileService.FileService.Instance ());
        public string Realobjecttype {get; set;}
        public string Realobjectmodule {get; set;}
        public string Realobjectxml {get; set;}
        public string NetworkLocation {get; set;}
    
        Public messagelocation Persistencetype {get; set;} public void inject<t> (T obj) {this. Realobjecttype = typeof (T).
            FullName; This. Realobjectmodule = typeof (T).
            Module.name;
            string xml = Serializeutils.serialize2xml (typeof (T), obj);
        SaveXML (XML); The public T extract<t> () {Assembly Assembly = AppDomain.CurrentDomain.Load (this. Realobjectmodule.trimend (". dll").
            ToCharArray ())); Type type = assembly. GetType (this.
            Realobjecttype);
            string xml = GetXML ();
        Return (T) serializeutils.deserializefromxml (type, XML);
            private String GetXML () {String xml = ""; if (this. Persistencetype = = messagelocation.inqueue) XML = this.
            Realobjectxml; else if (this. Persistencetype = = messagelocation.innetwork) XML = Filebroker.getcontentanddelete (this.
            NetworkLocation);
        return XML; } private void SaveXML (string xml) {if (XML). Length > QueueConfiguration.QueueConfiguration.MaxQueueBodyLength) {this.
                NetworkLocation = Filebroker.save (XML); This.
            Persistencetype = Messagelocation.innetwork; } else {this.
                Realobjectxml = XML; This.
            Persistencetype = Messagelocation.inqueue; }
        }
    }

The code is relatively simple, it is not introduced.

Introspection drives progress and vision determines the future.

With lofty ideals.

Work for the happiness of the family.

A2D technology to serve the community.

A2d Framework (Alpha)

1. Cache System (local cache coexists with distributed cache, supports Memcache and Redis, supports labeling (similar to spring 3.x cache form))

2. Event System (local event and distributed event distribution)

3. IoC (automatic matching function, instance quantity limit function)

4. SQL Dispatcher System (support for ado.net and EF)

5. Session system (distributed session systems)

6. Distributed Command Bus (MSMQ implementation, resolution 4M limit, support session read)

7. Rule Engine

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

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.