C # Queue RabbitMQ with both good and bad (with source end of the text): Q and simple application message queue MQ (a)

First, we simply look at what the heap, stacks, queues.

Heap is running, rather than at compile time, an application memory size. That dynamic allocation of memory, and it is no different for general access memory access.

Stack is a container, after put to acquire it, it would have some of the following things have to wait to come out after it came out. (Last out or LIFO)

Queue can only do the team head deletion, insertion operation done in the tail. The stack can only do insertions and deletions in the top of the stack. (First In First Out)


 

Note: The current is shown queue (Queue), the next chapter display message queue MQ (Message Queue)

Queue (Queue) represents a FIFO collection of objects. When you need access to the FIFO, then use the queue.

When you add a list, called into the team , when you remove the item from the list, called a team .

  1. Enqueue is added to the tail of the queue data, plus a number of queue data, the tail pointer is moved; 
  2. The team is to take data in the head of the queue and then delete the data, the head pointer is moved.

 

& Described common attributes

  1. Count: Count property returns the number of elements in the queue.
  2. Enqueue: Enqueue () method adds an element at an end of the queue.
  3. Dequeue: Dequeue () method for reading and deleting elements in the head of the queue. If you call Dequeue () method, there are no elements in the queue, it throws an InvalidOperationException exception of type.
  4. Peek: Peek () method reads an element from the head of the queue, but does not remove it.
  5. TrimExcess: TrimExcess () resets the queue capacity. Dequeue () method removes the element from the queue, but it does re-set the capacity of the queue. Removing from the head of the queue empty elements, use TrimExcess () method.
  6. Clear: Clear () method removes all elements from the queue.
  7. ToArray: ToArray () to copy a new queue array.

 

Here we combine a simple example to explain:

Example: the single-user information to the queue and the queue is read.

the using the System;
 the using the System.Collections.Generic;
 the using the System.Linq;
 the using the System.Text;
 the using System.Threading.Tasks; 

namespace QueueConsoleApplication 
{ 
   
   public  class Program 
    { 

        // Example: the user order information is added to the queue and read queue. 
       static  void the Main ( String [] args) 
       { 
           // create a queue 
           Queue <the UserInfo> Queue = new new Queue <the UserInfo> (); 

           // Get a list of single-user 
           List <the UserInfo> userList = GetUserList (); 

           //Use Enqueue () method of the user order information is added to the queue (enqueue) 
           the foreach ( var User in userList) 
           { 
               queue.Enqueue (User); 
           } 

           // use the Count property obtaining the number of elements in the queue 
           int queueCount = Queue. COUNT; 
           Console.WriteLine ( String .Format ( " queue information under a single user {0}. " , queueCount));   // output 

           // use the Dequeue () method for reading and deleting an element from the head of the queue ( dequeue) 
           for ( int I = 0 ; I <queueCount; I ++ ) 
           { 
               the UserInfo User =queue.Dequeue (); 
               Console.WriteLine ( String .Format ( " \ n-order number: {0}; User Name: {1}; phone number: {2}; Delivery Address: {3}; trade name: { 4}; price: {}. 5 " , 
                   user.ID, user.name, user.Phone, user.Address, user.Commodity, user.Price)); 
           } 

           // use the Count property acquisition queue element formats 
           queueCount = queue.Count; 
           Console.WriteLine ( String .Format ( " \ n-queue 0} {have information in the subscriber unit. " , queueCount));   // output 

       } 

       ///  <Summary> 
       /// Get a list of users
        / //  </ Summary> 
       ///  <Returns> </ Returns>
        public static List<UserInfo> GetUserList()
        {
            List<UserInfo> userList = new List<UserInfo>();
            userList.Add(new UserInfo("201906031010", "王母", "1821234****", "昆仑山玉虚宫", "9万年蟠桃", 136.00));
            userList.Add(new UserInfo("201906031011", ""Radiant, " 1821235 **** " , " green grassland sheep village " , " Wolfsburg brand non-toxic pesticides " , 198.00 )); 
            userList.Add ( new new UserInfo ( " 201 906 031 012 " , " bald strong " , " 1,821,236 * *** " , " dog Xiongling Guang head strong family " , " animal trap " , 346.00 )); 
            userList.Add ( new new UserInfo ( " 201 906 031 013 " ," Monkey King" , " 1821237 **** " , " Huaguoshan " , " delousing powder " , 245.00 ));
             return userList; 
        } 
    } 

    ///  <Summary> 
    /// single class of user information entity
     ///  < / Summary> 
    public  class the UserInfo 
    { 
        public the UserInfo ( String ID, String name, String Phone, String address, String Commodity, Double . price) 
        {
            this.ID = id;  
            this.Name = name;
            this.Phone = phone;
            this.Address = address;
            this.Commodity = commodity;
            this.Price = price;
                
        }
        public string ID { get; set; }   //单号
        public string Name { get; set; }  //姓名
        public string Phone { get; set;}   // phone number 
        public  String Address { GET ; the SET ;}   // delivery address 
        public  String Commodity { GET ; the SET ;}   // trade names 
        public  Double Price { GET ; the SET ;}   // price 
    } 

    
}

 

First prepare a queue of content, combined with the FIFO principle, we read the news print queue inside.

Information can be dynamically inserted into the team, we are here to demonstrate to write to the specified data.

After running the code we print the relevant information in the console, as shown below:

 

to sum up:

C # class queue is provided, represented by Queue class, the class represents the collection of objects represented FIFO, which is located under the System.Collections namespace.

Queue useful news stored in the order received, to facilitate sequential processing. Queue class queue implemented as a circular array, objects stored in the Queue class is inserted at one end, is removed from the other end.

In this way without the bells and whistles of other things, the new good projects directly writing code.

 This source code that has been on the inside, on the can directly copy operation,

Below we explain the message queue MQ (Message Queue),

这个就需要下载RabbitMQ、Erlang环境并安装,下载RabbitMQ.Client客户端进行引用,附带源码。

 

Guess you like

Origin www.cnblogs.com/xiongze520/p/10967270.html