艺海无涯,学无止境。欢迎一起探讨技术和人生,了解我

消息队列RabbitMQ用法

消息队列RabbitMQ用法,把数据写到RabbitMQ里面,然后从RabbitMQ里面读取数据,调用接口URL使用

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RabbitMQTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        StringBuilder strb = new StringBuilder();
        private void buttonSend_Click(object sender, EventArgs e)
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "127.0.0.1";
            factory.Port = 5672;
            using (IConnection conn = factory.CreateConnection())
            {
                using (IModel model = conn.CreateModel())
                {
                    model.QueueDeclare("RabbitMQTest", true, false, false, null);
                    byte[] buffer = Encoding.UTF8.GetBytes(textBoxJsonStr.Text);
                    IBasicProperties properties = model.CreateBasicProperties();
                    properties.DeliveryMode = 2;
                    model.BasicPublish("", "RabbitMQTest", properties, buffer);

                    model.BasicQos(0, 1, false);
                    QueueingBasicConsumer consumer = new QueueingBasicConsumer(model);
                    model.BasicConsume("RabbitMQTest", false, consumer);
                    BasicDeliverEventArgs ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                    byte[] bytes = ea.Body;
                    string str = Encoding.UTF8.GetString(bytes);
                    

                    //调用接口
                    Dictionary<string, string> para = new Dictionary<string, string>();
                    para.Add("jsonStr",str);
                   string result = PostFormData(textBoxUrl.Text,para);
                   
                    strb.AppendLine(result);
                    textBoxResult.Text = strb.ToString();

                    model.BasicAck(ea.DeliveryTag, false);

                }
            }
        }
        public string PostFormData(string url, Dictionary<string, string> dicParams)
        {
            StringBuilder prestr = new StringBuilder();
            foreach (KeyValuePair<string, string> temp in dicParams)
            {
                prestr.Append("&" + temp.Key + "=" + temp.Value);
            }
            prestr.Remove(0, 1);

            string postString = prestr.ToString();
            try
            {
                string responseString;
                byte[] postBytes = Encoding.UTF8.GetBytes(postString);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.KeepAlive = false;
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = postBytes.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(postBytes, 0, postBytes.Length);
                }
                using (HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse())
                {
                    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    responseString = sr.ReadToEnd();
                    sr.Close();
                }
                return responseString;
            }
            catch (Exception ex)
            {
                strb.AppendLine(ex.Message);
            }
            return "";
        }
    }
}