public static RabbitMqUtil NewMqClient(int number) { RabbitMqUtil rabbit = new RabbitMqUtil(); var conn = rabbit.CreateConnection(new ConnectionFactory { HostName = "192.168.1.11", Port = 5676, UserName = "******", Password = "******", VirtualHost = "/", //Protocol = Protocols.DefaultProtocol, AmqpUriSslProtocols = SslProtocols.Tls, AutomaticRecoveryEnabled = true, //自动重连 RequestedFrameMax = UInt32.MaxValue, RequestedHeartbeat = TimeSpan.FromSeconds(UInt16.MaxValue) //心跳超时时间 }); string queue = $"queue{number}"; IModel model = rabbit.CreateModel(conn); model.DeclareQueue(queue, "ChatExchange", "", false, false, true); model.AddConsumer(queue, RcvMsg); System.Threading.Tasks.Task.Factory.StartNew(async() => { for (int i = 0; i < 500; i++) { byte[] bs = Encoding.UTF8.GetBytes($"我是{queue},第{i}次"); await System.Threading.Tasks.Task.Delay(3000); model.BasicPublish("ChatExchange", "", false, null, bs); } }); return(rabbit); }
public IConnection GetConnection() { IConnection curConn = null; if (connections.Count < LastIndex + 1 || connections[LastIndex] == null) { RabbitMqUtil mqUtil = new RabbitMqUtil(); IConnection connection = mqUtil.CreateConnection(Factory); connections.Add(connection); curConn = connection; } else { curConn = connections[LastIndex]; } var channel = curConn.CreateModel(); var instance = channel.CreateBasicPublishBatch(); return(curConn); }