基本信息
源码名称:RabbitMQ案例(入门级)
源码大小:12.74M
文件格式:.7z
开发语言:C#
更新时间:2020-08-14
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
/// <summary>
/// --------------
/// 生产者
/// --------------
/// </summary>
class Program
{
const string ExchangeName = "test.exchange";
const string QueueName = "test.queue";
/// <summary>
/// 生产者
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
ConnectionFactory rabbitMqFactory = new ConnectionFactory { HostName = "localhost", UserName = "test01", Password = "test01", VirtualHost = "VH" };
using (IConnection conn = rabbitMqFactory.CreateConnection())
using (IModel channel = conn.CreateModel())
{
//声明消息队列,且为可持久化的
channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
//声明消息队列,且为可持久化的
channel.QueueDeclare(QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
//绑定队列
channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName);
//将队列设置为持久化之后,还需要将消息也设为可持久化的
var props = channel.CreateBasicProperties();
// props.SetPersistent(true);
props.DeliveryMode = 2;
for (int i = 0; i < 18000; i )
{
// System.Threading.Thread.Sleep(5000);
props.Priority = (i == 5) ? (byte)10 : (byte)i;
string message = "测试测试" i.ToString();
var body = Encoding.UTF8.GetBytes(message);
//发布信息
channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: body);
Console.WriteLine("Program Sent {0}", message);
}
channel.Close();
conn.Close();
}
Console.WriteLine("OK-生产者...............");
}
}
}