protected override bool PublishInternal(CronusMessage message) { try { if (ReferenceEquals(null, connection) || connection.IsOpen == false) { lock (connectionFactory) { if (ReferenceEquals(null, connection) || connection.IsOpen == false) { connection?.Abort(); connection = connectionFactory.CreateConnection(); connection.AutoClose = false; } } } if (publishModel == null || publishModel.IsClosed) { publishModel = connection.CreateModel(); } IBasicProperties props = publishModel.CreateBasicProperties(); props.Headers = new Dictionary <string, object>() { { message.Payload.GetType().GetContractId(), string.Empty } }; props.Persistent = true; props.Priority = 9; byte[] body = this.serializer.SerializeToBytes(message); var publishDelayInMiliseconds = message.GetPublishDelay(); if (publishDelayInMiliseconds < 1000) { var exchangeName = RabbitMqNamer.GetExchangeName(message.Payload.GetType()); publishModel.BasicPublish(exchangeName, string.Empty, false, props, body); } else { var exchangeName = RabbitMqNamer.GetExchangeName(message.Payload.GetType()) + ".Scheduler"; props.Headers.Add("x-delay", message.GetPublishDelay()); publishModel.BasicPublish(exchangeName, string.Empty, false, props, body); } return(true); } catch (Exception ex) { publishModel?.Abort(); connection?.Abort(); connection = null; publishModel = null; return(false); } }
void RecoverModel() { if (aborting) { return; } if (model == null || model.IsClosed) { model?.Abort(); model = connection.CreateModel(); var routingHeaders = new Dictionary <string, object>(); routingHeaders.Add("x-match", "any"); var messageTypes = subscriberCollection.Subscribers.SelectMany(x => x.GetInvolvedMessageTypes()).Distinct().ToList(); foreach (var msgType in messageTypes.Select(x => x.GetContractId())) { routingHeaders.Add(msgType, null); } model.QueueDeclare(queueName, true, false, false, routingHeaders); var exchanges = messageTypes.GroupBy(x => RabbitMqNamer.GetExchangeName(boundedContext.Name, x)).Distinct(); foreach (var item in exchanges) { model.ExchangeDeclare(item.Key, PipelineType.Headers.ToString(), true); var args = new Dictionary <string, object>(); args.Add("x-delayed-type", PipelineType.Headers.ToString()); model.ExchangeDeclare(item.Key + ".Scheduler", "x-delayed-message", true, false, args); var bindHeaders = new Dictionary <string, object>(); bindHeaders.Add("x-match", "any"); foreach (var msgType in item.Select(x => x.GetContractId())) { bindHeaders.Add(msgType, null); } model.QueueBind(queueName, item.Key, string.Empty, bindHeaders); model.QueueBind(queueName, item.Key + ".Scheduler", string.Empty, bindHeaders); model.BasicQos(0, 1, false); } } }