Details on the use of RabbitMQ. NET message queues and the use of rabbitmq

Source: Internet
Author: User
Tags net message queue

Details on the use of RabbitMQ. NET message queues and the use of rabbitmq

This example shares the usage of RabbitMQ. NET message queue for your reference. The details are as follows:

First download the installation package. All environments are win7 64-bit:
Download otp_win64_19.0.exe and rabbitmq-server-3.6.3.exe from the official website.
Then start programming:
(1) create a producer class:

Class Program {private static void Main () {// establishes a RabbitMQ connection and channel var connectionFactory = new ConnectionFactory {HostName = "127.0.0.1", Port = 5672, UserName = "guest ", password = "guest", Protocol = Protocols. defaultProtocol, AutomaticRecoveryEnabled = true, // automatically reconnect RequestedFrameMax = UInt32.MaxValue, RequestedHeartbeat = UInt16.MaxValue // heartbeat timeout}; try {using (var connection = connectionFactory. C ReateConnection () {using (var channel = connection. createModel () {// create a new, persistent exchange channel. exchangeDeclare ("SISOExchange", ExchangeType. direct, true, false, null); // creates a new, persistent queue with no exclusivity, and does not automatically delete the channel. queueDeclare ("SISOqueue", true, false, false, null); // bind the queue to the channel in the SWAp area. queueBind ("SISOqueue", "SISOExchange", "optionalRoutingKey"); // set the message property var properties = channel. createBasicProperties (); p Roperties. deliveryMode = 2; // The message is persistent and cannot be affected by server restart. // the message to be pushed or published can be any (serializable) byte array, for example, the serialization object, the ID of an object, or just a string var encoding = new UTF8Encoding (); for (var I = 0; I <10; 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 the producer does not send messages directly to the queue. In fact, in many cases, the producer does not know whether the message will be sent to a queue. Instead, the producer sends messages to the swap zone. A swap zone is a very simple thing. One End accepts the messages of the producer, and the other end pushes them to the queue. The exchange zone must provide clear guidance on how to process the messages it receives. Put it in a queue, put it in multiple queues, or be discarded. These rules can be defined by the type of the SWAp zone. // Available swap areas include direct, topic, headers, and fanout. // Exchange: used to receive messages sent by the message producer. There are three types of exchange: direct, fanout, and topic. Different Types implement different routing algorithms. // RoutingKey: it is the rule that RabbitMQ implements route distribution to various queues, and is provided in conjunction with Binging for Exchange to push messages into the Queue; // Queue: it is a message Queue, you can define multiple queues as needed, set queue attributes, such as message removal, message caching, and callback, to communicate with Consumer. channel. basicPublish ("SISOExchange", "optionalRoutingKey", properties, msgBytes);} channel. close () ;}} catch (Exception ex) {Console. writeLine (ex. message);} Console. writeLine ("Message Publishing! "); Console. ReadKey (true );}}

(1) create a consumer class:

Class Program {private static void Main () {// establishes a RabbitMQ connection and channel var connectionFactory = new ConnectionFactory {HostName = "127.0.0.1", Port = 5672, UserName = "guest ", password = "guest", Protocol = Protocols. AMQP_0_9_1, RequestedFrameMax = UInt32.MaxValue, RequestedHeartbeat = UInt16.MaxValue}; using (var connection = connectionFactory. createConnection () using (var channel = connection. createModel () {// This indicates that the channel does not prefetch more than one message channel. basicQos (0, 1, false); // creates a new persistent channel in the SWAp zone. exchangeDeclare ("SISOExchange", ExchangeType. direct, true, false, null); // creates a new, persistent queue channel. queueDeclare ("sample-queue", true, false, false, null); // bind the queue to the channel in the SWAp area. queueBind ("SISOqueue", "SISOExchange", "optionalRoutingKey"); using (var subqueue = new subqueue (channel, "SISOqueue", false) {Console. writeLine ("waiting for message... "); var encoding = new UTF8Encoding (); while (channel. isOpen) {BasicDeliverEventArgs eventArgs; var success = subscribe. next (2000, out eventArgs); if (success = false) continue; var msgBytes = eventArgs. body; var message = encoding. getString (msgBytes); Console. writeLine (message); channel. basicAck (eventArgs. deliveryTag, false );}}}}}

Consumer -- result:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.