Example #1
0
            public override void WhenConnected()
            {
                _shutdownCallback = GetAsyncCallback <ShutdownEventArgs>(args =>
                {
                    var exception = ShutdownSignalException.FromArgs(args);
                    _promise.SetException(exception);
                    FailStage(exception);
                });

                Channel.ModelShutdown += OnChannelShutdown;

                Pull(_stage.In);
            }
Example #2
0
        public override void PreStart()
        {
            try
            {
                var factory = ConnectionFactoryFrom(Settings.ConnectionSettings);
                Connection       = NewConnection(factory, Settings.ConnectionSettings);
                Channel          = Connection.CreateModel();
                ShutdownCallback = GetAsyncCallback <ShutdownEventArgs>(args =>
                {
                    if (args.Initiator != ShutdownInitiator.Application)
                    {
                        FailStage(ShutdownSignalException.FromArgs(args));
                    }
                });
                Connection.ConnectionShutdown += OnConnectionShutdown;
                Channel.ModelShutdown         += OnChannelShutdown;

                foreach (var declaration in Settings.Declarations)
                {
                    switch (declaration)
                    {
                    case QueueDeclaration queueDeclaration:
                        Channel.QueueDeclare(queueDeclaration.Name, queueDeclaration.Durable,
                                             queueDeclaration.Exclusive,
                                             queueDeclaration.AutoDelete, queueDeclaration.Arguments.ToDictionary(key => key.Key, val => val.Value));
                        break;

                    case BindingDeclaration bindingDeclaration:
                        Channel.QueueBind(bindingDeclaration.Queue, bindingDeclaration.Exchange,
                                          bindingDeclaration.RoutingKey ?? "", bindingDeclaration.Arguments.ToDictionary(key => key.Key, val => val.Value));
                        break;

                    case ExchangeDeclaration exchangeDeclaration:
                        Channel.ExchangeDeclare(exchangeDeclaration.Name, exchangeDeclaration.ExchangeType,
                                                exchangeDeclaration.Durable, exchangeDeclaration.AutoDelete,
                                                exchangeDeclaration.Arguments.ToDictionary(key => key.Key, val => val.Value));
                        break;
                    }
                }

                WhenConnected();
            }
            catch (Exception e)
            {
                OnFailure(e);
                throw;
            }
        }
Example #3
0
            public override void WhenConnected()
            {
                var shutdownCallback = GetAsyncCallback <(string[] consumerTags, ShutdownEventArgs args)>(tuple =>
                {
                    var(consumerTags, args) = tuple;
                    if (args != null)
                    {
                        var exception = ShutdownSignalException.FromArgs(args);
                        var ex        = new ApplicationException($"Consumer {_queueName} with consumerTag(s) {string.Join(",", consumerTags)} shutdown unexpectedly", exception);
                        _promise.SetException(ex);
                        FailStage(ex);
                    }
                    else
                    {
                        var ex = new ApplicationException($"Consumer {_queueName} with consumerTag(s) {string.Join(",", consumerTags)} shutdown unexpectedly");
                        _promise.SetException(ex);
                        FailStage(ex);
                    }
                });

                Pull(_stage.In);

                // we have only one consumer per connection so global is ok
                Channel.BasicQos(0, (ushort)_stage.BufferSize, true);

                var consumerCallback = GetAsyncCallback <CommittableIncomingMessage>(HandleDelivery);

                var commitCallback = GetAsyncCallback <CommitCallback>(callback =>
                {
                    switch (callback)
                    {
                    case AckArguments args:
                        try
                        {
                            Channel.BasicAck(args.DeliveryTag, args.Multiple);
                            if (--_unackedMessages == 0 &&
                                (IsClosed(_stage.Out) || (IsClosed(_stage.In) && _queue.Count == 0 && _outstandingMessages == 0)))
                            {
                                CompleteStage();
                            }
                            args.Commit();
                        }
                        catch (Exception ex)
                        {
                            args.Fail(ex);
                        }

                        break;

                    case NackArguments args:
                        try
                        {
                            Channel.BasicNack(args.DeliveryTag, args.Multiple, args.Requeue);
                            if (--_unackedMessages == 0 &&
                                (IsClosed(_stage.Out) || (IsClosed(_stage.In) && _queue.Count == 0 && _outstandingMessages == 0)))
                            {
                                CompleteStage();
                            }
                            args.Commit();
                        }
                        catch (Exception ex)
                        {
                            args.Fail(ex);
                        }

                        break;
                    }
                });

                _amqpSourceConsumer = new DefaultConsumer(consumerCallback, commitCallback, shutdownCallback);

                // Create an exclusive queue with a randomly generated name for use as the replyTo portion of RPC
                _queueName = Channel.QueueDeclare("", false, true, true).QueueName;
                Channel.BasicConsume(_queueName, false, _amqpSourceConsumer);
                _promise.TrySetResult(_queueName);
            }
Example #4
0
            public override void WhenConnected()
            {
                // we have only one consumer per connection so global is ok
                Channel.BasicQos(0, (ushort)_stage.BufferSize, false);
                var consumerCallback = GetAsyncCallback <CommittableIncomingMessage>(HandleDelivery);

                var shutdownCallback = GetAsyncCallback <ShutdownEventArgs>(args =>
                {
                    if (args != null)
                    {
                        FailStage(ShutdownSignalException.FromArgs(args));
                    }
                    else if (_unackedMessages == 0)
                    {
                        CompleteStage();
                    }
                });

                var commitCallback = GetAsyncCallback <CommitCallback>(callback =>
                {
                    switch (callback)
                    {
                    case AckArguments args:
                        try
                        {
                            Channel.BasicAck(args.DeliveryTag, args.Multiple);
                            if (--_unackedMessages == 0 && IsClosed(_stage.Out))
                            {
                                CompleteStage();
                            }
                            args.Commit();
                        }
                        catch (Exception ex)
                        {
                            args.Fail(ex);
                        }

                        break;

                    case NackArguments args:
                        try
                        {
                            Channel.BasicNack(args.DeliveryTag, args.Multiple, args.Requeue);
                            if (--_unackedMessages == 0 && IsClosed(_stage.Out))
                            {
                                CompleteStage();
                            }
                            args.Commit();
                        }
                        catch (Exception ex)
                        {
                            args.Fail(ex);
                        }

                        break;
                    }
                });

                _amqpSourceConsumer = new DefaultConsumer(consumerCallback, commitCallback, shutdownCallback);

                switch (Settings)
                {
                case NamedQueueSourceSettings _:
                    var namedSourceSettings = (NamedQueueSourceSettings)Settings;
                    SetupNamedQueue(namedSourceSettings);
                    break;

                case TemporaryQueueSourceSettings _:
                    var tempSettings = (TemporaryQueueSourceSettings)Settings;
                    SetupTemporaryQueue(tempSettings);
                    break;
                }
            }