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

發送消息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(); } } }

在本地查看隊列效果服務器

相關文章
相關標籤/搜索