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 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(); })); } }