public void Receive(IDictionary <string, Type> eventTypeLookup)
        {
            if (Link != null)
            {
                throw new InvalidOperationException("Already receiving.");
            }

            EventTypeLookup = eventTypeLookup;
            Logger.LogInformation($"Registering {eventTypeLookup.Count} event types:");
            foreach (var pair in eventTypeLookup)
            {
                Logger.LogInformation($"{pair.Key} = {pair.Value}");
            }

            Error = null;
            var session = CreateSession();
            var attach  = new Attach()
            {
                Source = new Source()
                {
                    Address = Settings.Address, Durable = Settings.Durable
                },
                Target = new Target()
                {
                    Address = null
                }
            };

            Link         = new ReceiverLink(session, Settings.AppName, attach, null);
            Link.Closed += OnClosed;
            Link.SetCredit(Settings.Credits, true); //Not sure if this is sufficient to renew credits...
            Link.Start(Settings.Credits, OnMessageCallback);
        }
Example #2
0
        public async Task SendAsync(string eventType, string address, string data, string correlationId)
        {
            var messageId = Guid.NewGuid().ToString();

            using (Logger.BeginScope(new Dictionary <string, object> {
                ["CorrelationId"] = correlationId,
                ["MessageId"] = messageId,
                ["MessageType"] = eventType
            })) {
                Logger.LogTrace($"Publishing message {messageId} to {address} with body: {data}");
                var session = CreateSession();
                var attach  = new Attach()
                {
                    Target = new Target()
                    {
                        Address = address, Durable = Settings.Durable
                    },
                    Source = new Source()
                };
                var sender = new SenderLink(session, Settings.AppName, attach, null);
                sender.Closed += OnClosed;
                var message = new Message(data)
                {
                    Header = new Header {
                        Durable = (Settings.Durable == 2)
                    },
                    ApplicationProperties = new ApplicationProperties(),
                    Properties            = new Properties {
                        MessageId     = messageId,
                        GroupId       = eventType,
                        CorrelationId = correlationId
                    }
                };
                message.ApplicationProperties[MESSAGE_TYPE_KEY] = eventType;

                try {
                    await sender.SendAsync(message);

                    Logger.LogInformation($"Published message {messageId}");
                } finally {
                    if (sender.Error != null)
                    {
                        Error             = new DomainEventError();
                        Error.Condition   = sender.Error.Condition.ToString();
                        Error.Description = sender.Error.Description;
                        Closed?.Invoke(this, Error);
                    }
                    if (!sender.IsClosed)
                    {
                        await sender.CloseAsync(TimeSpan.FromSeconds(5));
                    }
                    await session.CloseAsync();

                    await session.Connection.CloseAsync();
                }
            }
        }
 private void OnClosed(IAmqpObject sender, Error error)
 {
     if (sender.Error != null)
     {
         Error             = new DomainEventError();
         Error.Condition   = sender.Error.Condition.ToString();
         Error.Description = sender.Error.Description;
     }
     Closed(this, Error);
 }
 public void Close(TimeSpan?timeout = null)
 {
     timeout = timeout ?? TimeSpan.Zero;
     Link?.Session.Close(timeout.Value);
     Link?.Session.Connection.Close(timeout.Value);
     Link?.Close(timeout.Value);
     Link            = null;
     Error           = null;
     EventTypeLookup = null;
 }
Example #5
0
 private void OnReceiverClosed(IDomainEventReceiver receiver, DomainEventError error)
 {
     if (error == null)
     {
         logger.LogError("Handling OnReceiverClosed event with no error information");
     }
     else
     {
         logger.LogError($"Handling OnReceiverClosed event with error: {error.Condition} - {error.Description}");
     }
 }
Example #6
0
        public async Task InnerSendAsync(string address, Message message)
        {
            using (Logger.BeginScope(new Dictionary <string, object> {
                ["CorrelationId"] = message.Properties.CorrelationId,
                ["MessageId"] = message.Properties.MessageId,
                ["MessageType"] = message.Properties.GroupId
            })) {
                Logger.LogTrace($"Publishing message {message.Properties.MessageId} to {address} with body: {message.Body}");
                var session = CreateSession();
                var attach  = new Attach()
                {
                    Target = new Target()
                    {
                        Address = address, Durable = Settings.Durable
                    },
                    Source = new Source()
                };
                var sender = new SenderLink(session, Settings.AppName, attach, null);
                sender.Closed += OnClosed;

                try {
                    await sender.SendAsync(message);

                    Logger.LogInformation($"Published message {message.Properties.MessageId}");
                } finally {
                    if (sender.Error != null)
                    {
                        Error             = new DomainEventError();
                        Error.Condition   = sender.Error.Condition.ToString();
                        Error.Description = sender.Error.Description;
                        Closed?.Invoke(this, Error);
                    }
                    if (!sender.IsClosed)
                    {
                        await sender.CloseAsync(TimeSpan.FromSeconds(5));
                    }
                    await session.CloseAsync();

                    await session.Connection.CloseAsync();
                }
            }
        }