public void SetExchangeType() { RabbitExchangeType expected = RabbitExchangeType.fanout; var actual = subject.SetExchangeType(expected); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual.ExchangeType); }
public static IRabbitExchange DeclareExchange(this IRabbitChannel source, string name, RabbitExchangeType exchangeType) { return source.DeclareExchange(name, new ExchangeOptions() { ExchangeType = exchangeType, // defaults from the original api: Durable = false, AutoDelete = false }); }
private void CreateExchange(string exchangeName, RabbitExchangeType localExchangeType) { if (!_persistentConnection.IsConnected) { _persistentConnection.TryConnect(); } var exchangeType = ConvertToRabbitMqExchangeType(localExchangeType); _logger.LogInformation($"Creating exchange {exchangeName} of type {exchangeType} "); _consumerChannel.ExchangeDeclare(exchange: exchangeName, type: exchangeType, durable: true); }
private string ConvertToRabbitMqExchangeType(RabbitExchangeType customExchangeType) { switch (customExchangeType) { case RabbitExchangeType.DirectExchange: return(ExchangeType.Direct); case RabbitExchangeType.FanoutExchange: return(ExchangeType.Fanout); default: throw new ArgumentException($"No corresponding RabbitMq exchange type exists for {customExchangeType}", nameof(RabbitExchangeType)); } }
public ProducerHarness() { exchange = config.GetProperty("EXCHANGE"); routingKey = config.GetProperty("ROUTING_KEY", ""); sleepPeriodMs = config.GetPropertyInteger("SLEEP_PERIOD_MS"); errorSleepPeriodMs = config.GetPropertyInteger("ERROR_SLEEP_PERIOD_MS", 15000); type = Enum.Parse <RabbitExchangeType>(config.GetProperty("EXCHANGE_TYPE")); repeatCount = config.GetPropertyInteger("REPEAT_COUNT", 1); producerCount = config.GetPropertyInteger("PRODUCER_COUNT", 1); contentType = config.GetProperty("CONTENT_TYPE", DEFAULT_CONTENT_TYPE); message = GetMessage(config, contentType); msg = Encoding.UTF8.GetBytes(message); }
//private async Task HandleMessage(string dictionaryKey, byte[] message) //{ // if (subscriptionInfoDictionary.TryGetValue(dictionaryKey, out Subscription subscription)) // { // var messageHandler = subscription.MessageHandlerInstance; // if (!subscription.MessageResultInBytes) // { // var messageType = subscription.MessageType; // var json = Encoding.UTF8.GetString(message); // var messageToSend = (IMessage)JsonConvert.DeserializeObject(json, messageType); // await messageHandler.HandleAsync(messageToSend); // } // else await messageHandler.HandleAsync(message); // } //} public void Publish(IMessage messageToSend, string exchangeName, RabbitExchangeType exchangeType, string routingKey) { if (!existingExchanges.Contains(exchangeName)) { CreateExchange(exchangeName, exchangeType); existingExchanges.Add(exchangeName); } var message = JsonConvert.SerializeObject(messageToSend); var body = Encoding.UTF8.GetBytes(message); var properties = _consumerChannel.CreateBasicProperties(); properties.Persistent = true; _logger.LogInformation("Publishing message to RabbitMQ"); _consumerChannel.BasicPublish(exchange: exchangeName, routingKey: routingKey, basicProperties: properties, body: body); }
/// <summary> /// 定义交换机 /// </summary> /// <param name="name">交换机名称</param> /// <param name="type">交换机类型</param> /// <param name="alternateExchangeName">当消息投递不成功时的转发到的备用交换机名称(由此交换机进行转投)</param> /// <param name="arguments">其它参数</param> public void ExchangeDeclare(string name, RabbitExchangeType type, string alternateExchangeName = null, IDictionary <string, object> arguments = null) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } if (!string.IsNullOrEmpty(alternateExchangeName)) { if (arguments == null) { arguments = new Dictionary <string, object>(); } string altKey = "alternate-exchange"; arguments[altKey] = alternateExchangeName; } ManageChannel.ExchangeDeclare(name, type.ToString().ToLower(), true, false, arguments); }
public void Run() { if (rabbit == null) { rabbit = Rabbit.Connect(); } var config = new ConfigSettings(); var exchange = config.GetProperty("EXCHANGE"); var queue = config.GetProperty("QUEUE"); string routingKey = config.GetProperty("ROUTING_KEY", ""); RabbitExchangeType type = Enum.Parse <RabbitExchangeType>(config.GetProperty("EXCHANGE_TYPE")); builder = rabbit.ConsumerBuilder() .SetExchange(exchange) .SetExchangeType(type) .AddQueue(queue, routingKey); var queueType = Enum.Parse <RabbitQueueType>(config.GetProperty("QUEUE_TYPE")); builder.UseQueueType(queueType); if (config.GetPropertyBoolean("LAZY_QUEUE", true)) { //builder. } if (config.GetPropertyBoolean("SINGLE_ACTIVE_CONSUMER", false)) { builder = builder.SetSingleActiveConsumer(); } var consumer = builder.Build(); consumer.RegisterReceiver(Reciever); while (true) { Thread.Sleep(10000); } }
protected RabbitExchange( IRabbitProto proto, string name, RabbitExchangeType type, bool durable, bool autoDelete, ReadOnlyDictionary <string, object>?arguments) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException( $"{nameof(Name)} cannot be empty."); } Proto = proto; Name = name; Type = type; Durable = durable; AutoDelete = autoDelete; Arguments = arguments; }
public void Subscribe <TMessage, TMessageHandler>(TMessageHandler handlerInstance, string exchangeName, RabbitExchangeType exchangeType, string routingKey, bool messageResultInBytes) where TMessage : IMessage, new() where TMessageHandler : IMessageHandler { if (!existingExchanges.Contains(exchangeName)) { CreateExchange(exchangeName, exchangeType); existingExchanges.Add(exchangeName); } var dictionaryKey = exchangeName + routingKey; var messageType = typeof(TMessage); if (!subscriptionInfoDictionary.ContainsKey(dictionaryKey)) { var queueName = _consumerChannel.QueueDeclare(durable: true, exclusive: true, autoDelete: false).QueueName; subscriptionInfoDictionary.TryAdd(dictionaryKey, new Subscription(queueName, handlerInstance, messageType, messageResultInBytes)); _logger.LogInformation("Subscribing to message {MessageName}", messageType.Name); StartConsumingMessages(queueName); _consumerChannel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routingKey); } }
public static IRabbitExchange DeclareExchange(this IRabbitChannel source, string name, RabbitExchangeType exchangeType) { return(source.DeclareExchange(name, new ExchangeOptions() { ExchangeType = exchangeType, // defaults from the original api: Durable = false, AutoDelete = false })); }
public RabbitPublisherBuilder SetExchangeType(RabbitExchangeType type) { ExchangeType = type; return(this); }