• RabbitMQ驱动简单例子


    using RabbitMQ.Client;
    using RabbitMQ.Client.Events;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            string hostName = "192.168.1.61";
            string userName = "test";
            string passWord = "123456";
            //信息是否持久化
            bool durable = false;
    
            public Form1()
            {
                InitializeComponent();
                //防止多线程报错
                System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
                textBox2.ReadOnly = true;
                textBox2.ScrollBars = ScrollBars.Vertical;
                var thrd = new Thread(GetMessage);
                thrd.IsBackground = true;
                thrd.Name = "DownLoad";
                thrd.Start();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
               
                var factory = new ConnectionFactory();
                factory.HostName = hostName;
                factory.UserName = userName;
                factory.Password = passWord;
    
                //创建一个连接
                using (var connection = factory.CreateConnection())
                {
                    //创建一个通道
                    using (var channel = connection.CreateModel())
                    {
                        //创建一个队列
                        channel.QueueDeclare("hello", durable, false, false, null);
       
                        //信息持久化
                        var properties = channel.CreateBasicProperties();
                        properties.Persistent = true;
    
                        string message = textBox1.Text;
                        var body = Encoding.UTF8.GetBytes(message);
                        //发送数据
                        channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
                    }
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
    
            }
    
            private void GetMessage()
            {
                var factory = new ConnectionFactory();
                factory.HostName = hostName;
                factory.UserName = userName;
                factory.Password = passWord;
    
                //创建一个连接
                using (var connection = factory.CreateConnection())
                {          
                    //创建一个通道
                    using (var channel = connection.CreateModel())
                    {
                        //创建一个队列
                        channel.QueueDeclare("hello", durable, false, false, null);
                        channel.BasicQos(0, 1, false);
    
                        var consumer = new QueueingBasicConsumer(channel);
                        channel.BasicConsume("hello", true, consumer);
    
                        while (true)
                        {
                            //接收信息
                            var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                            var body = ea.Body;
                            var message = Encoding.UTF8.GetString(body);
                            textBox2.Text += message + "
    ";
    
                        }
    
                    }
                }
            }
        }
    }
  • 相关阅读:
    摘录一篇 这两天对SSO的认识
    获取文本框中的行
    在窗体数据源中过滤记录
    linux下访问windows的共享
    使用Form作Lookup其窗体位置设置
    窗体数据源连接技巧
    给动态创建的控件指定事件
    Object的使用技巧
    显示进度条SysOperationProgress
    如何解决下载的CHM文件无法显示网页问题
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/5505915.html
Copyright © 2020-2023  润新知