예제 #1
0
 public MessageProcessedAwaiter(string serverName, string queueName, Func <TMessage, Task> processMessage, MessageProcessedSettings settings = null)
 {
     m_context        = new { queue = $"http://{serverName}:15672/#/queues/%2f/{queueName}" };
     m_processMessage = processMessage;
     m_settings       = settings ?? new MessageProcessedSettings();
     m_rabbitMq       = new RabbitMqWrapper(serverName, queueName, m_settings.Priority, autoAck: false, onError: HandleSubscriberException, setup: model =>
     {
         model.BasicQos(prefetchSize: 0, prefetchCount: m_settings.PrefetchCount, global: false);
         model.QueueDeclarePassive(queueName);
     });
 }
        public MessagePublishedAwaiter(string serverName, string exchangeName, string routingKeyName, TimeSpan timeout)
        {
            m_timeout = timeout;
            m_context = new
            {
                exchange   = $"http://{serverName}:15672/#/exchanges/%2f/{exchangeName}",
                routingKey = routingKeyName,
            };

            var queueName = $"{exchangeName}_{routingKeyName}_awaiter_{Environment.MachineName}_{Guid.NewGuid():N}";

            m_rabbitMq = new RabbitMqWrapper(serverName, queueName, priority: 0, autoAck: true, onError: e => m_exception = e, setup: model =>
            {
                model.QueueDeclare(
                    queue: queueName,
                    durable: false,
                    exclusive: true,
                    autoDelete: true,
                    arguments: new Dictionary <string, object> {
                    { "x-ha-policy", "all" }
                });

                model.QueueBind(queueName, exchangeName, routingKeyName, null);
            });

            var messages = Channel.CreateUnbounded <string>();

            m_messages = messages.Reader;

            // Wait synchronously for the consumer to start before returning.
            m_rabbitMq.StartConsumer(
                consumerTag: null,
                onReceived: (_, message) => messages.Writer.TryWrite(message),
                onCancelled: () => { })
            .GetAwaiter()
            .GetResult();

            Task.Run(SubscriberLoop, m_cancellationTokenSource.Token);
        }