public Task <bool> PublishAsync <T>(ExchangeDeclareConfiguration exchange, string routingKey, T message, bool waitForConfirms) { bool result = !waitForConfirms; var body = Serializer.Serialize(message); using (var channel = ConnectionPool.Get(ConnectionName).CreateModel()) { channel.ExchangeDeclare( exchange.ExchangeName, exchange.Type, durable: exchange.Durable ); var properties = channel.CreateBasicProperties(); properties.DeliveryMode = RabbitMqConsts.DeliveryModes.Persistent; channel.BasicPublish( exchange: exchange.ExchangeName, routingKey: routingKey, mandatory: true, basicProperties: properties, body: body ); if (waitForConfirms) { result = channel.WaitForConfirms(); } } return(Task.FromResult(result)); }
public IRabbitMqMessageConsumer Create(ExchangeDeclareConfiguration exchange, QueueDeclareConfiguration queue, string connectionName = default) { var consumer = ServiceScope.ServiceProvider.GetRequiredService <RabbitMqMessageConsumer>(); consumer.Initialize(exchange, queue, connectionName); return(consumer); }
public void Initialize( [NotNull] ExchangeDeclareConfiguration exchange, [NotNull] QueueDeclareConfiguration queue, string connectionName = null) { Exchange = Check.NotNull(exchange, nameof(exchange)); Queue = Check.NotNull(queue, nameof(queue)); ConnectionName = connectionName; Timer.Start(); }
public void Subscribe() { var consumers = typeFinder.Find(t => t.IsClass && !t.IsAbstract && typeof(IRabbitMQConsumer).IsAssignableFrom(t)); foreach (var item in consumers) { var obj = iocResolver.Resolve(item); Type messageType = (Type)item.InvokeMember("GetMessageType", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); ExchangeDeclareConfiguration exchange = (ExchangeDeclareConfiguration)item.InvokeMember("GetExchangeDeclare", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); QueueDeclareConfiguration queue = (QueueDeclareConfiguration)item.InvokeMember("GetQueueDeclare", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); QOSConfiguration qOS = (QOSConfiguration)item.InvokeMember("GetQOSConfiguration", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); string ConnectionName = (string)item.InvokeMember("GetConnectionName", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); var consumer = consumerFactory.Create( exchange, //new ExchangeDeclareConfiguration( // ExchangeName, // ExchangeType, // durable: Durable //), queue, //new QueueDeclareConfiguration( // QueueName, // durable: Durable, // exclusive: Exclusive, // autoDelete: AutoDelete //), qOS, ConnectionName ); consumer.OnMessageReceived((model, ea) => { var eventName = ea.RoutingKey; var eventData = objectSerializer.Deserialize(messageType, ea.Body); var task = (Task)item.InvokeMember("ConsumeAsync", System.Reflection.BindingFlags.InvokeMethod, null, obj, new[] { eventData }); return(task); }); string routingKey = (string)item.InvokeMember("GetRoutingKey", System.Reflection.BindingFlags.InvokeMethod, null, obj, null); consumer.BindAsync(routingKey); Consumers.Add(item, consumer); Console.WriteLine("1"); } }
public async Task PublishAsync <T>(ExchangeDeclareConfiguration exchange, string routingKey, T message) { await PublishAsync(exchange, routingKey, message, false); }