龙8官网long8

当前位置:龙8官网long8 > 龙8官网long8 > WinForm实现Rabbitmq官网6个案例-Hello World

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

来源:http://www.sketchydesignstudio.com 作者:龙8官网long8 时间:2019-10-05 15:59

龙8官网long8 1

龙8官网long8 2龙8官网long8 3

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; }
        }
    }
}

先上代码

龙8官网long8 4

生产者发送消息到队列,然后队列(rabbitmq)把消息发送给消费者(消费者向rabbitmq索取消息)

龙8官网long8, 两个消费者:

龙8官网long8 5

界面如下:

 

龙8官网long8 6

大致流程是

View Code

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

关键词:

上一篇:你知道萧大侠的知名作品吗

下一篇:没有了