コード例 #1
0
ファイル: RabbitMQProducer.cs プロジェクト: Poepon/JK
        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));
        }
コード例 #2
0
    public IRabbitMqMessageConsumer Create(ExchangeDeclareConfiguration exchange, QueueDeclareConfiguration queue, string connectionName = default)
    {
        var consumer = ServiceScope.ServiceProvider.GetRequiredService <RabbitMqMessageConsumer>();

        consumer.Initialize(exchange, queue, connectionName);
        return(consumer);
    }
コード例 #3
0
 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();
 }
コード例 #4
0
ファイル: AutoRabbitMQConsumer.cs プロジェクト: Poepon/JK
        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");
            }
        }
コード例 #5
0
ファイル: RabbitMQProducer.cs プロジェクト: Poepon/JK
 public async Task PublishAsync <T>(ExchangeDeclareConfiguration exchange, string routingKey, T message)
 {
     await PublishAsync(exchange, routingKey, message, false);
 }