public void Start() { var settings = RabbitMqSubscriptionSettings.CreateForPublisher(_settings.ConnectionString, _settings.ExchangeName); settings.MakeDurable(); if (_logFactory == null) { _publisher = new RabbitMqPublisher <INotificationMessage>(settings) .SetConsole(new LogToConsole()) .SetLogger(_log); } else { _publisher = new RabbitMqPublisher <INotificationMessage>(_logFactory, settings); } _publisher.DisableInMemoryQueuePersistence() .PublishSynchronously() .SetSerializer(new JsonMessageSerializer <INotificationMessage>()) .SetPublishStrategy(new DefaultFanoutPublishStrategy(settings)) .Start(); }
public IMessageProducer <TMessage> GetProducer <TMessage>(IReloadingManager <RabbitConnectionSettings> settings, bool isDurable, IRabbitMqSerializer <TMessage> serializer) { // on-the fly connection strings switch is not supported currently for rabbitMq var currSettings = settings.CurrentValue; var subscriptionSettings = new RabbitMqSubscriptionSettings { ConnectionString = currSettings.ConnectionString, ExchangeName = currSettings.ExchangeName, IsDurable = isDurable, }; return((IMessageProducer <TMessage>)_producers.GetOrAdd(subscriptionSettings, CreateProducer)); IStopable CreateProducer(RabbitMqSubscriptionSettings s) { var publisher = new RabbitMqPublisher <TMessage>(s); if (isDurable && _queueRepository.Value != null) { publisher.SetQueueRepository(_queueRepository.Value); } else { publisher.DisableInMemoryQueuePersistence(); } return(publisher .SetSerializer(serializer) .SetLogger(_logger) .Start()); } }
public void QueuePersistenceCanBeDisabled() { _publisher.DisableInMemoryQueuePersistence(); Assert.DoesNotThrow(() => _publisher.Start()); _publisher.Stop(); }
public IMessageProducer <TMessage> GetProducer <TMessage>(IReloadingManager <RabbitConnectionSettings> settings, bool isDurable, bool useMessagePack) { // on-the fly connection strings switch is not supported currently for rabbitMq var currSettings = settings.CurrentValue; var subscriptionSettings = new RabbitMqSubscriptionSettings { ConnectionString = currSettings.ConnectionString, ExchangeName = currSettings.ExchangeName, IsDurable = isDurable, }; return((IMessageProducer <TMessage>)_producers.GetOrAdd(subscriptionSettings, CreateProducer).Value); Lazy <IStopable> CreateProducer(RabbitMqSubscriptionSettings s) { // Lazy ensures RabbitMqPublisher will be created and started only once // https://andrewlock.net/making-getoradd-on-concurrentdictionary-thread-safe-using-lazy/ return(new Lazy <IStopable>(() => { var publisher = new RabbitMqPublisher <TMessage>(s); if (isDurable && _queueRepository.Value != null) { publisher.SetQueueRepository(_queueRepository.Value); } else { publisher.DisableInMemoryQueuePersistence(); } var serializer = useMessagePack ? (IRabbitMqSerializer <TMessage>) new MessagePackMessageSerializer <TMessage>(MsgPackCompatModeResolver.Instance) : new JsonMessageSerializer <TMessage>(Encoding.UTF8, JsonSerializerSettings); return publisher .SetSerializer(serializer) .SetLogger(_logger) .Start(); })); } }
public IMessageProducer <TMessage> GetProducer <TMessage>(RabbitMqSettings settings, bool isDurable, IRabbitMqSerializer <TMessage> serializer) { // on-the fly connection strings switch is not supported currently for rabbitMq var subscriptionSettings = new RabbitMqSubscriptionSettings { ConnectionString = settings.ConnectionString, ExchangeName = settings.ExchangeName, IsDurable = isDurable, }; return((IMessageProducer <TMessage>)_producers.GetOrAdd(subscriptionSettings, CreateProducer).Value); Lazy <IStopable> CreateProducer(RabbitMqSubscriptionSettings s) { // Lazy ensures RabbitMqPublisher will be created and started only once // https://andrewlock.net/making-getoradd-on-concurrentdictionary-thread-safe-using-lazy/ return(new Lazy <IStopable>(() => { var publisher = new RabbitMqPublisher <TMessage>(s); if (isDurable && _queueRepository.Value != null) { publisher.SetQueueRepository(_queueRepository.Value); } else { publisher.DisableInMemoryQueuePersistence(); } return publisher .SetSerializer(serializer) .SetLogger(_logger) .SetConsole(_consoleWriter) .Start(); })); } }