WinForm实现Rabbitmq官网6个案例-Hello World
先上代码
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 <= )
{
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}\r\n", txtPublisher.Text);
if (queueName == queueName2)
txtConsumer2.Text += string.Format("{0}\r\n", 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; }
}
}
}
界面如下:

大致流程是
生产者发送消息到队列,然后队列(rabbitmq)把消息发送给消费者(消费者向rabbitmq索取消息)
两个消费者:



WinForm实现Rabbitmq官网6个案例-Hello World的更多相关文章
- WinForm实现Rabbitmq官网6个案例-RPC
获取源码 客户端代码: namespace RabbitMQDemo { public partial class RPC : Form { private readonly static RPC _ ...
- WinForm实现Rabbitmq官网6个案例-Publishe/Subscribe
代码: namespace RabbitMQDemo { public partial class PublishSubscribe : Form { private string exchangeN ...
- WinForm实现Rabbitmq官网6个案例-Work Queues
代码: namespace RabbitMQDemo { public partial class WorkQueues : Form { private string queueName = &qu ...
- WinForm实现Rabbitmq官网6个案例-Topics
代码: namespace RabbitMQDemo { public partial class Topics : Form { private string exchangeName = &quo ...
- WinForm实现Rabbitmq官网6个案例-Routing
代码: namespace RabbitMQDemo { public partial class Routing : Form { private string exchangeName = &qu ...
- 官网英文版学习——RabbitMQ学习笔记(一)认识RabbitMQ
鉴于目前中文的RabbitMQ教程很缺,本博主虽然买了一本rabbitMQ的书,遗憾的是该书的代码用的不是java语言,看起来也有些不爽,且网友们不同人学习所写不同,本博主看的有些地方不太理想,为此本 ...
- 2022年官网下安装RabbitMQ最全版与官网查阅方法
目录 一.Erlang环境部署 1.百度搜索"Erlang",或者访问网址:https://www.erlang.org/,找到DOWNLOAD双击进入. 2.找到支持的windo ...
- Yeoman 官网教学案例:使用 Yeoman 构建 WebApp
STEP 1:设置开发环境 与yeoman的所有交互都是通过命令行.Mac系统使用terminal.app,Linux系统使用shell,windows系统可以使用cmder/PowerShell/c ...
- MXNet官网案例分析--Train MLP on MNIST
本文是MXNet的官网案例: Train MLP on MNIST. MXNet所有的模块如下图所示: 第一步: 准备数据 从下面程序可以看出,MXNet里面的数据是一个4维NDArray. impo ...
随机推荐
- SDN定义网络
http://edu.51cto.com/course/course_id-4466.html http://edu.51cto.com/course/course_id-4497.html
- C#生成验证码类
using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;us ...
- Linux 服务器开发常用命令操作
1)查看网络端口 netstat -na --ip 2)查看特定应用程序进程 ps -ef | grep vsftp or ps aux | grep xxx.exe 3)查看系统日志 vi /et ...
- WebDriver中的Actions对象
我们可以利用Actions对象来模拟鼠标的操作以及页面的拖拽 1.模拟鼠标的双击操作: 1)模拟双击一个div,验证点击之前的字体为14号 2)点击后字体为20号 Actions builder = ...
- typeof, offsetof 和container_of
要理解Linux中实现的双向循环链表("侵入式"链表),首先得弄明白宏container_of. 本文尝试从gcc的关键字typeof和宏offsetof入手,循序渐进地剖析宏co ...
- JavaScript自动化构建工具grunt、gulp、webpack介绍
前端开发自动化工作流工具,JavaScript自动化构建工具grunt.gulp.webpack介绍 前端自动化,这样的一个名词听起来非常的有吸引力,向往力.当今时代,前端工程师需要维护的代码变得及为 ...
- Hadoop深入浅出实战经典视频教程(共22讲)
该视频教程共22讲,由王家林老师主讲. --------------------------------------------------------- 第01讲:为什么会有第一代大数据技术Hado ...
- vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制
一.定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图 ...
- 【angular5项目积累总结】消息订阅服务
code import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable ...
- yii 页面加载完成后弹出模态框
<?php $js = <<<JS $('#page-modal').modal('show');//页面加载完显示模态框 $('.modal-dialog').css('wi ...