예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="message"></param>
        /// <param name="exchange"></param>
        /// <param name="routingKey"></param>
        /// <param name="type"></param>
        public void Publish <TMessage>(TMessage message, string exchange, string routingKey, string type = ExchangeType.Topic)
        {
            string body = message.Serialize();

            if (_publishChannel?.IsOpen != true)
            {
                if (_persistentConnection.IsConnected)
                {
                    _persistentConnection.TryConnect();
                }
                _publishChannel              = _persistentConnection.ExchangeDeclare(exchange, type: type);
                _publishChannel.BasicReturn += async(se, ex) => await Task.Delay((int)_persistentConnection.Configuration.ConsumerFailRetryInterval.TotalMilliseconds).ContinueWith(t => Publish(body, ex.Exchange, ex.RoutingKey));
            }
            IBasicProperties properties = _publishChannel.CreateBasicProperties();

            properties.DeliveryMode = 2; // persistent
            _publishChannel.BasicPublish(exchange: exchange,
                                         routingKey: routingKey,
                                         mandatory: true,
                                         basicProperties: properties,
                                         body: body.GetBytes());
            _logger.WriteLog(_persistentConnection.Configuration.Level, $"{DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss")}\t{exchange}\t{routingKey}\t{body}");
            _eventHandlerFactory?.PubliushEvent(new EventBusArgs(_persistentConnection.Endpoint, exchange, "", routingKey, type, _persistentConnection.Configuration.ClientProvidedName, body, true));
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="message"></param>
        /// <param name="exchange"></param>
        /// <param name="routingKey"></param>
        /// <param name="type"></param>
        public void Publish <TMessage>(TMessage message, string exchange, string routingKey, string type = ExchangeType.Topic)
        {
            if (_publishChannel?.IsOpen != true)
            {
                if (_persistentConnection.IsConnected)
                {
                    _persistentConnection.TryConnect();
                }
                _publishChannel              = _persistentConnection.ExchangeDeclare(exchange, type: type);
                _publishChannel.BasicReturn += async(se, ex) => await Task.Delay(1000).ContinueWith(t => Publish(ex.Body, ex.Exchange, ex.RoutingKey));
            }
            IBasicProperties properties = _publishChannel.CreateBasicProperties();

            properties.DeliveryMode = 2; // persistent
            string body = message.Serialize();

            _publishChannel.BasicPublish(exchange: exchange,
                                         routingKey: routingKey,
                                         mandatory: true,
                                         basicProperties: properties,
                                         body: body.GetBytes());
            _logger.Information(body);
            _eventHandlerFactory?.PubliushEvent(new EventBusArgs(_persistentConnection.Endpoint, exchange, "", routingKey, type, _persistentConnection.Configuration.ClientProvidedName, body));
        }