// -------------------------------- publish --------------------------------------------- public void Publish( IExchange exchange, string routingKey, bool mandatory, bool immediate, MessageProperties messageProperties, byte[] body) { // Fix me: It's very hard now to move publish logic to separate abstraction, just leave it here. var rawMessage = produceConsumeInterceptor.OnProduce(new RawMessage(messageProperties, body)); if (connectionConfiguration.PublisherConfirms) { var timeBudget = new TimeBudget(TimeSpan.FromSeconds(connectionConfiguration.Timeout)).Start(); while (!timeBudget.IsExpired()) { var confirmsWaiter = clientCommandDispatcher.Invoke(model => { var properties = model.CreateBasicProperties(); rawMessage.Properties.CopyTo(properties); var waiter = confirmationListener.GetWaiter(model); try { model.BasicPublish(exchange.Name, routingKey, mandatory, immediate, properties, rawMessage.Body); } catch (Exception) { waiter.Cancel(); throw; } return(waiter); }); try { confirmsWaiter.Wait(timeBudget.GetRemainingTime()); break; } catch (PublishInterruptedException) { } } } else { clientCommandDispatcher.Invoke(model => { var properties = model.CreateBasicProperties(); rawMessage.Properties.CopyTo(properties); model.BasicPublish(exchange.Name, routingKey, mandatory, immediate, properties, rawMessage.Body); }); } eventBus.Publish(new PublishedMessageEvent(exchange.Name, routingKey, rawMessage.Properties, rawMessage.Body)); logger.DebugWrite("Published to exchange: '{0}', routing key: '{1}', correlationId: '{2}'", exchange.Name, routingKey, messageProperties.CorrelationId); }
public virtual async Task PublishAsync( IExchange exchange, string routingKey, bool mandatory, MessageProperties messageProperties, byte[] body) { Preconditions.CheckNotNull(exchange, "exchange"); Preconditions.CheckShortString(routingKey, "routingKey"); Preconditions.CheckNotNull(messageProperties, "messageProperties"); Preconditions.CheckNotNull(body, "body"); // Fix me: It's very hard now to move publish logic to separate abstraction, just leave it here. var rawMessage = produceConsumeInterceptor.OnProduce(new RawMessage(messageProperties, body)); if (connectionConfiguration.PublisherConfirms) { var timeout = TimeBudget.Start(TimeSpan.FromSeconds(connectionConfiguration.Timeout)); while (!timeout.IsExpired()) { var confirmsWaiter = await clientCommandDispatcher.InvokeAsync(model => { var properties = model.CreateBasicProperties(); rawMessage.Properties.CopyTo(properties); var waiter = confirmationListener.GetWaiter(model); try { model.BasicPublish(exchange.Name, routingKey, mandatory, properties, rawMessage.Body); } catch (Exception) { waiter.Cancel(); throw; } return(waiter); }).ConfigureAwait(false); try { await confirmsWaiter.WaitAsync(timeout).ConfigureAwait(false); break; } catch (PublishInterruptedException) { } } } else { await clientCommandDispatcher.InvokeAsync(model => { var properties = model.CreateBasicProperties(); rawMessage.Properties.CopyTo(properties); model.BasicPublish(exchange.Name, routingKey, mandatory, properties, rawMessage.Body); }).ConfigureAwait(false); } eventBus.Publish(new PublishedMessageEvent(exchange.Name, routingKey, rawMessage.Properties, rawMessage.Body)); if (logger.IsDebugEnabled()) { logger.DebugFormat( "Published to exchange {exchange} with routingKey={routingKey} and correlationId={correlationId}", exchange.Name, routingKey, messageProperties.CorrelationId ); } }
// -------------------------------- publish --------------------------------------------- public void Publish( IExchange exchange, string routingKey, bool mandatory, MessageProperties messageProperties, byte[] body) { // Fix me: It's very hard now to move publish logic to separate abstraction, just leave it here. var rawMessage = produceConsumeInterceptor.OnProduce(new RawMessage(messageProperties, body)); if (connectionConfiguration.PublisherConfirms) { var timeout = TimeBudget.Start(connectionConfiguration.GetTimeout()); while (true) { if (timeout.IsExpired()) { throw new TimeoutException($"Publish timed out after {connectionConfiguration.Timeout} seconds"); } var confirmsWaiter = clientCommandDispatcher.Invoke(model => { var properties = model.CreateBasicProperties(); rawMessage.Properties.CopyTo(properties); var waiter = confirmationListener.GetWaiter(model); try { model.BasicPublish(exchange.Name, routingKey, mandatory, properties, rawMessage.Body); } catch (Exception) { waiter.Cancel(); throw; } return(waiter); }); try { confirmsWaiter.Wait(timeout); break; } catch (PublishInterruptedException) { } } } else { clientCommandDispatcher.Invoke(model => { var properties = model.CreateBasicProperties(); rawMessage.Properties.CopyTo(properties); model.BasicPublish(exchange.Name, routingKey, mandatory, properties, rawMessage.Body); }); } eventBus.Publish(new PublishedMessageEvent(exchange.Name, routingKey, rawMessage.Properties, rawMessage.Body)); if (logger.IsDebugEnabled()) { logger.DebugFormat( "Published to exchange {exchange} with routingKey={routingKey} and correlationId={correlationId}", exchange.Name, routingKey, messageProperties.CorrelationId ); } }