WinForm实现Rabbitmq官网6个案例-Hello World

作者: 365bet体育官网  发布:2019-09-06

先上代码

365bet体育官网 1365bet体育官网 2

namespace RabbitMQDemo
{
    public partial class HelloWorld : Form
    {
        string queueName1 = "hello_queue1";//消费者1
        string queueName2 = "hello_queue2";//消费者2
        Action<string> SetText;
        /// <summary>
        /// 单线程实例
        /// </summary>
        private static readonly HelloWorld _helloWorld;
        static HelloWorld()
        {
            _helloWorld = new HelloWorld();
        }
        /// <summary>
        /// 单例模式
        /// </summary>
        public static HelloWorld SingleForm
        { get { return _helloWorld; } }
        private HelloWorld()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            ReseiveMsg(queueName1);
            ReseiveMsg(queueName2);
            SetText += OnSetText;
        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            SendMsg();
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        private void SendMsg()
        {
            string message = txtPublisher.Text;
            if (message.Trim().Length <= 0)
            {
                MessageBox.Show("请输入要发送的消息");
            }
            string queueName = cbBoxQueues.SelectedValue.ToString();
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: queueName,
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                var body = Encoding.UTF8.GetBytes(message);

                channel.BasicPublish(exchange: "",
                                     routingKey: queueName,
                                     basicProperties: null,
                                     body: body);
            }
        }

        /// <summary>
        /// 接收消息
        /// </summary>
        private void ReseiveMsg(string queueName)
        {
            //string queueName = cbBoxQueues.SelectedText;
            try
            {
                var factory = new ConnectionFactory() { HostName = "localhost" };

                //connection和channel不能使用using,否则会被dispose掉
                var connection = factory.CreateConnection();
                var channel = connection.CreateModel();
                //声明队列 生产者和消费者都需要QueueDeclare
                channel.QueueDeclare(queue: queueName,
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);

                    txtConsumer1.Invoke(SetText, message);
                };
                channel.BasicConsume(queue: queueName,
                                     noAck: true,
                                     consumer: consumer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void OnSetText(string txtContent)
        {
            string queueName = cbBoxQueues.SelectedValue.ToString();
            if (queueName == queueName1)
                txtConsumer1.Text += string.Format("{0}rn", txtPublisher.Text);
            if (queueName == queueName2)
                txtConsumer2.Text += string.Format("{0}rn", txtPublisher.Text);
        }

        private void HelloWorld_Load(object sender, EventArgs e)
        {
            List<DataSource> lst = new List<DataSource>();
            lst.Add(new DataSource("消费者1", "hello_queue1"));
            lst.Add(new DataSource("消费者2", "hello_queue2"));

            cbBoxQueues.DataSource = lst;
            cbBoxQueues.DisplayMember = "DisplayMember";
            cbBoxQueues.ValueMember = "DisplayValue";
        }

        private class DataSource
        {
            public DataSource(string displayMember,string displayValue)
            {
                DisplayMember = displayMember;
                DisplayValue = displayValue;
            }
            public string DisplayMember { get; set; }
            public string DisplayValue { get; set; }
        }
    }
}

View Code

界面如下:

365bet体育官网 3

大致流程是

365bet体育官网,生产者发送消息到队列,然后队列(rabbitmq)把消息发送给消费者(消费者向rabbitmq索取消息)

 两个消费者:

365bet体育官网 4

365bet体育官网 5

365bet体育官网 6

 

本文由365bet体育官网发布于365bet体育官网,转载请注明出处:WinForm实现Rabbitmq官网6个案例-Hello World

关键词: