c#操作RabbitMQ使用json格式寫入消息隊列


發送消息

     //rabbitmqctl stop_app 停止服務,會清除queue
    //rabbitmqctl start_app 開啟服務
    //rabbitmqctl list_queues 查詢當前隊列
    //rabbitmqctl purge_queue kibaQueue 清空指定queue隊列的數據
    /// <summary>
    /// 發送消息
    /// </summary> 
public class TempSpfxjgLst
        {
            public List<TempSpfxjg> Records { get; set; }
        }

        public class TempSpfxjg
        {
            public string algorithm_types { get; set; }
            public string camera_id { get; set; }
            public string current_time { get; set; }
            public string image_url { get; set; }
            public string stay_time { get; set; }
            public string target_position { get; set; }
            public string target_type { get; set; }
            public string task_id { get; set; }
            public string task_type { get; set; }
        }   
static void Main(string[] args)
        {

            var factory = new ConnectionFactory();
            factory.HostName = "localhost";//主機名,Rabbit會拿這個IP生成一個endpoint,這個很熟悉吧,就是socket綁定的那個終結點。
            factory.UserName = "guest";//默認用戶名,用戶可以在服務端自定義創建,有相關命令行
            factory.Password = "guest";//默認密碼 


            TempSpfxjg tmpJson1 = new TempSpfxjg();
            tmpJson1.algorithm_types = "9";
            tmpJson1.camera_id = "1004";
            tmpJson1.current_time = "2019-05-08 23:29:47";
            tmpJson1.image_url = "http://192.168.31.115/images/1004/20190107T092411_398729662.jpg";
            tmpJson1.stay_time = "0.000000";
            tmpJson1.target_position = "617,81,58,79";
            tmpJson1.target_type = "2";
            tmpJson1.task_id = "123456";
            tmpJson1.task_type = "4";

            TempSpfxjgLst lst = new TempSpfxjgLst();
            lst.Records = new List<TempSpfxjg>();
            lst.Records.Add(tmpJson1);
           string jsontext = JsonConvert.SerializeObject(lst);
            //byte[] buffer = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jsonText));

            using (var connection = factory.CreateConnection())//連接服務器,即正在創建終結點。
            {
                //創建一個通道,這個就是Rabbit自己定義的規則了,如果自己寫消息隊列,這個就可以開腦洞設計了
                //這里Rabbit的玩法就是一個通道channel下包含多個隊列Queue
                using (var channel = connection.CreateModel())
                {
                    //for (int i = 0; i < 100; i++)
                    //{
                        channel.QueueDeclare("SPFXJG", false, false, false, null);//創建一個名稱為""的消息隊列
                        var properties = channel.CreateBasicProperties();
                        properties.DeliveryMode = 1;
                        string message = "Hello Word"; //傳遞的消息內容 
                        channel.BasicPublish(exchange: "", routingKey: "SPFXJG", properties, Encoding.UTF8.GetBytes(jsontext)); //生產消息
                        Console.WriteLine($"Send:{message}");
                       //  Thread.Sleep(3000);
                   // }
                }
            }
            Console.ReadLine();
        }

  接收消息

        static void Main(string[] args)
        {
            var factory = new ConnectionFactory();
            factory.HostName = "localhost";
            factory.UserName = "guest";
            factory.Password = "guest";

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare("SPFXJG", false, false, false, null);

                    /* 這里定義了一個消費者,用於消費服務器接受的消息
                     * C#開發需要注意下這里,在一些非面向對象和面向對象比較差的語言中,是非常重視這種設計模式的。
                     * 比如RabbitMQ使用了生產者與消費者模式,然后很多相關的使用文章都在拿這個生產者和消費者來表述。
                     * 但是,在C#里,生產者與消費者對我們而言,根本算不上一種設計模式,他就是一種最基礎的代碼編寫規則。
                     * 所以,大家不要復雜的名詞嚇到,其實,並沒那么復雜。
                     * 這里,其實就是定義一個EventingBasicConsumer類型的對象,然后該對象有個Received事件,該事件會在服務接收到數據時觸發。
                     */ 
                    var consumer = new EventingBasicConsumer(channel);//消費者 
                    channel.BasicConsume("SPFXJG", true, consumer);//消費消息 autoAck參數為消費后是否刪除
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine("Received: {0}", message);
                    };
                    Console.ReadLine();
                }
            }
        }

在本地查看隊列效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM