コード例 #1
0
        public void Publish(IntegrationEvent @event)
        {
            if (!_connection.IsConnected)
            {
                if (!_connection.Connect())
                {
                    _logger.LogWarning($"Couldn't publish event {@event.Id}. No available connection was found.");
                    return;
                }
            }

            using (var ch = _connection.CreateModel())
            {
                DeclareExchange(ch);
                var msg = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType());
                ch.BasicPublish(_exchangeName,
                                routingKey: _subscriptionManager.GetEventKey(@event.GetType()),
                                body: msg);
            }
        }
コード例 #2
0
        private void StartConnectionMonitor(IPersistentConnection connection)
        {
            var sleepTimeout = connection.KeepAliveInteralSeconds * 1000;

            connection.Connect();

            this.Log(connection, $"Connection established");

            while (true)
            {
                Thread.Sleep(sleepTimeout);

                if (!connection.IsConnected)
                {
                    throw new Exception("Connection lost");
                }

                connection.KeepAlive();
                this.Debug(connection, "Keep alive sent successfully");
            }
        }