Rabbitmq. NET Message Queuing use of detailed _ practical skills

Source: Internet
Author: User

The example of this article is to share the RABBITMQ. NET Message Queue use method, for everyone's reference, the specific contents are as follows

First download the installation package, I have the environment is Win7 64-bit:
Go to the official website to download Otp_win64_19.0.exe and Rabbitmq-server-3.6.3.exe install well
And then it starts to program:
(1) Create the producer class:

Class Program {private static void Main () {//Establish RABBITMQ connection and channel var connectionfactory = new Connecti onfactory {HostName = "127.0.0.1", Port = 5672, UserName = "Guest", Password = "GUE" St ", Protocol = Protocols.defaultprotocol, automaticrecoveryenabled = true,//automatic re-connect Requestedframem 
     Ax = uint32.maxvalue, requestedheartbeat = uint16.maxvalue//Heartbeat timeout};  try {using (var connection = connectionfactory.createconnection ()) {using (var channel = Connection. Createmodel ()) {//Create a new, persistent swap area channel. 
           Exchangedeclare ("Sisoexchange", Exchangetype.direct, True, false, NULL); Create a new, persistent queue, without exclusivity, with no automatic deletion of channel. 
           Queuedeclare ("Sisoqueue", True, False, false, NULL); Bind the queue to the swap area channel. 
 
           Queuebind ("Sisoqueue", "Sisoexchange", "Optionalroutingkey"); Set message properties Var pRoperties = Channel. 
           Createbasicproperties (); Properties. DeliveryMode = 2; 
           Messages are persistent and exist that are not affected by server restart/The message ready to start a push//release can be any (serializable) byte array, such as a serialized object, an ID of an entity, or just a string 
           var encoding = new UTF8Encoding (); for (var i = 0; i < i++) {var msg = string. 
             Format ("This is the message #{0}?", i + 1); var msgbytes = encoding. 
             GetBytes (msg); The core idea of the RABBITMQ message model is that producers do not send messages directly to queues. In fact, producers in many cases do not know whether the message will be sent to a queue. Instead, the producer sends the message to the swap area. A swap is a very simple thing, one end of which accepts the producer's message, and the other end pushes them into the queue. The swap area must have clear instructions on how to handle the messages it receives. is placed in a queue, or in multiple queues, or discarded. 
             These rules can be defined by the type of the swap area. 
             The available Exchange zone types are: Direct,topic,headers,fanout. Exchange: Used to receive messages sent by message producers, with three types of exchange:direct, fanout,topic, different types of routing algorithms implemented;//routingkey: RABBITMQ implementation route distribution to each The rules of a queue, combined with binging provided in exchange use to push messages into queues;//queue: Is Message Queuing, you can define multiple queues as needed, set the properties of the queue, such as message removal, message caching, callback mechanisms, and so on, to achieve and consumer pass A letter; a channel. Basicpublish ("Sisoexchange", "OptionaLroutingkey ", properties, msgbytes); } channel. 
         Close (); '} ' catch (Exception ex) {Console.WriteLine (ex). 
     message); Console.WriteLine ("Message release!") 
     "); 
   Console.readkey (TRUE); 
 } 
 }

(1) Create consumer class:

Class Program {private static void Main () {//Establish RABBITMQ connection and channel var connectionfactory = new Co nnectionfactory {HostName = "127.0.0.1", Port = 5672, UserName = "Guest", pass Word = "Guest", Protocol = protocols.amqp_0_9_1, Requestedframemax = Uint32.maxvalue, requested 
 
      Heartbeat = Uint16.maxvalue}; using (var connection = connectionfactory.createconnection ()) using (var channel = connection. Createmodel ()) {//This indicates that the channel does not prefetch more than 1 messages channel. 
 
        Basicqos (0, 1, false); Create a new, persistent swap area channel. 
        Exchangedeclare ("Sisoexchange", Exchangetype.direct, True, false, NULL); Create a new, persistent queue channel. 
        Queuedeclare ("Sample-queue", True, False, false, NULL); Bind the queue to the swap area channel. 
        Queuebind ("Sisoqueue", "Sisoexchange", "Optionalroutingkey"); using (var subscription = new Subscription (channel, "Sisoqueue", false)
        {Console.WriteLine ("Wait for Message ..."); 
          var encoding = new UTF8Encoding (); while (channel. 
            IsOpen) {Basicdelivereventargs EventArgs; var success = Subscription. 
            Next (Watts, out EventArgs); 
            if (success = = false) continue; 
            var msgbytes = eventargs.body; var message = encoding. 
            GetString (msgbytes); 
            Console.WriteLine (message); Channel. 
          Basicack (Eventargs.deliverytag, false); 
 } 
        } 
      } 
    } 
  }

Consumer--As the result shows:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.