private void DeclareBindings(RC.IModel channel, params IBinding[] bindings) { foreach (var binding in bindings) { _logger?.LogDebug("Binding destination [{destination} ({type})] to exchange [{exchange}] with routing key [{routingKey}]", binding.Destination, binding.Type, binding.Exchange, binding.RoutingKey); try { if (binding.IsDestinationQueue) { if (!IsDeclaringImplicitQueueBinding(binding)) { channel.QueueBind(binding.Destination, binding.Exchange, binding.RoutingKey, binding.Arguments); } } else { channel.ExchangeBind(binding.Destination, binding.Exchange, binding.RoutingKey, binding.Arguments); } } catch (Exception e) { LogOrRethrowDeclarationException(binding, "binding", e); } } }
} // AmqpClient #region ConnectionHandlerProgram private Exception MakeNewConnection(ref RmqCl.IConnection connection, ref RmqCl.IModel channel, bool reconnecting) { // This method attempts to make a new connection. Returns true if success, otherwise false. var connRequest = ConnectionRequestObj; var factory = new RmqCl.ConnectionFactory() { HostName = connRequest.Host, UserName = connRequest.Username, Password = connRequest.Password }; // Secure connection? if (connRequest.Secure) { factory.Ssl.Enabled = true; factory.Ssl.ServerName = connRequest.Host; factory.Ssl.Version = System.Security.Authentication.SslProtocols.Tls12; } try { // Create connection and channel connection = factory.CreateConnection(); channel = connection.CreateModel(); // Declare the exchange channel.ExchangeDeclare(exchange: connRequest.Exchange, type: "topic", autoDelete: false, durable: true, arguments: null); // Create a queue to receive messages var queueName = channel.QueueDeclare(queue: "", // Use a generated queue name durable: false, // The queue does not survive a broker restart exclusive: true, // The queue is only for this application, and it will be deleted on app exit autoDelete: true, // The queue is deleted when no one is bound to it arguments: null ).QueueName; // Bind the queue to the topic pattern channel.QueueBind(queue: queueName, exchange: connRequest.Exchange, routingKey: connRequest.TopicPattern, arguments: null); // Set up a consumer for messages m_messageConsumer = new RmqCl.Events.EventingBasicConsumer(channel); m_messageConsumer.Received += MessageReceived; channel.BasicConsume(queue: queueName, noAck: true, consumerTag: "", noLocal: false, exclusive: false, arguments: new Dictionary <string, object> { }, consumer: m_messageConsumer); // Sign up for the shutdown event channel.ModelShutdown += ModelShutdown; // This event will fire if the connection is lost return(null); } catch (Exception e) { // Clean the connection DestroyConnection(ref connection, ref channel); return(e); } } // MakeNewConnection
private void InitializeDeadLetterQueue(IModel channel) { var dlqQueueArgs = new Dictionary <string, object> { { "x-dead-letter-exchange", $"{_serviceName}.Input.E.Direct.{Env}" }, { "x-message-ttl", (int)TimeSpan.FromMinutes(1).TotalMilliseconds } }; channel.QueueDeclare( queue: $"{_serviceName}.Dlq.Queue.{Env}", durable: true, exclusive: false, autoDelete: false, arguments: dlqQueueArgs); channel.QueueBind(queue: $"{_serviceName}.Dlq.Queue.{Env}", exchange: $"{_serviceName}.Dlx.E.Fanout.{Env}", routingKey: ""); }
public void ReviceMessage(string queueName, string someone, Action <string> eventHandel) { EventingBasicConsumer consumer = new EventingBasicConsumer(_chanel); _chanel.ExchangeDeclare(exchangeName, exchangeType, false, false, null); //_chanel.ExchangeDeclare(exchangeName, exchangeType); _chanel.QueueDeclare(queueName, false, false, false, null);//持久化 排他性 自动删除 _chanel.QueueBind(queueName, exchangeName, routingKey, null); //_chanel.QueueBind(queueName, exchangeName, routingKey + someone);//交换机与 consumer.Received += (ch, ea) => { var message = Encoding.UTF8.GetString(ea.Body); eventHandel?.Invoke(message); _chanel.BasicAck(ea.DeliveryTag, false);//确认该消息已被消费 }; _chanel.BasicConsume(queueName, false, consumer); //_chanel.BasicConsume(queueName, false, consumer); //启动消费者 // Console.WriteLine("客户端2已启动"); }