using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RabbitMqTest
{
public abstract class RabbitmqBase
{
///
/// 创建通道
///
///
protected virtual IModel GetChannel() {
var factory = new ConnectionFactory { HostName="localhost" };
var connection = factory.CreateConnection();
return connection.CreateModel();
}
private static string MessageChars = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>''""";
///
/// 生成随机消息
///
protected virtual string RandomString(int len = 10) {
var random = new Random();
int charslen = MessageChars.Length;
StringBuilder sb = new(len);
for(int i = 0;i
/// 生成随机消息字节数组
///
///
///
protected virtual Byte[] RandomMsgBytes(int len = 10) {
var str = RandomString(len);
return Encoding.UTF8.GetBytes(str);
}
///
/// 创建队列
///
protected virtual void QueueDeclare(IModel channel,string queueName) {
channel.QueueDeclare(queueName,exclusive: false);
}
///
/// 创建交换机
///
protected virtual void ExchangeDeclare(IModel channel,string exchangeName) {
channel.ExchangeDeclare(exchangeName,ExchangeType.Fanout,autoDelete: true);
}
}
}