Skip to content

Commit

Permalink
(#129) Expose Mandatory Option For Publish
Browse files Browse the repository at this point in the history
The mandatory option has been exposed to RawRabbit by adding a new
property to the PublishConfiguration and a new method,
WithMandatoryDelivery, on PublishConfigurationBuilder. This method also
allows the user to pass a function to the BasicReturn event handler.
Publisher has been updated to use the madatory flag in the BasicPublish
method and the BasicReturn event.
The Rider IDE user files have also been ignored.
  • Loading branch information
ritasker committed Nov 9, 2016
1 parent 331e174 commit 4b6e57e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ docs/*build*/
#VS Code
.vscode/**

#Rider
.idea/**

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
**ncrunch**
Expand Down
2 changes: 1 addition & 1 deletion sample/RawRabbit.Messages.Sample/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

"frameworks": {
"netstandard1.5": {},
"net451": {},
"net451": {}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RawRabbit.Configuration.Exchange;

namespace RawRabbit.Configuration.Publish
Expand All @@ -9,5 +10,6 @@ public interface IPublishConfigurationBuilder
IPublishConfigurationBuilder WithExchange(Action<IExchangeConfigurationBuilder> exchange);
IPublishConfigurationBuilder WithRoutingKey(string routingKey);
IPublishConfigurationBuilder WithProperties(Action<IBasicProperties> properties);
IPublishConfigurationBuilder WithMandatoryDelivery(EventHandler<BasicReturnEventArgs> basicReturn);
}
}
3 changes: 3 additions & 0 deletions src/RawRabbit/Configuration/Publish/PublishConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RawRabbit.Configuration.Exchange;

namespace RawRabbit.Configuration.Publish
Expand All @@ -9,5 +10,7 @@ public class PublishConfiguration
public ExchangeConfiguration Exchange { get; set; }
public string RoutingKey { get; set; }
public Action<IBasicProperties> PropertyModifier { get; set; }
public bool Mandatory { get; set; }
public EventHandler<BasicReturnEventArgs> BasicReturn { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RawRabbit.Configuration.Exchange;
using RawRabbit.Configuration.Request;

Expand All @@ -11,12 +12,16 @@ public class PublishConfigurationBuilder : IPublishConfigurationBuilder
private string _routingKey;
private Action<IBasicProperties> _properties;
private const string _oneOrMoreWords = "#";
private bool _mandatory;
private EventHandler<BasicReturnEventArgs> _basicReturn;

public PublishConfiguration Configuration => new PublishConfiguration
{
Exchange = _exchange.Configuration,
RoutingKey = _routingKey,
PropertyModifier = _properties ?? (b => {})
PropertyModifier = _properties ?? (b => {}),
Mandatory = _mandatory,
BasicReturn = _basicReturn
};

public PublishConfigurationBuilder(ExchangeConfiguration defaultExchange = null, string routingKey =null)
Expand Down Expand Up @@ -48,5 +53,12 @@ public IPublishConfigurationBuilder WithProperties(Action<IBasicProperties> prop
_properties = properties;
return this;
}

public IPublishConfigurationBuilder WithMandatoryDelivery(EventHandler<BasicReturnEventArgs> basicReturn)
{
_mandatory = true;
_basicReturn = basicReturn;
return this;
}
}
}
9 changes: 6 additions & 3 deletions src/RawRabbit/Operations/Publisher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RawRabbit.Channel.Abstraction;
using RawRabbit.Common;
using RawRabbit.Configuration;
Expand Down Expand Up @@ -57,15 +56,19 @@ public Task PublishAsync<TMessage>(TMessage message, Guid globalMessageId, Publi
{
throw t.Exception;
}

channelTask.Result.BasicReturn += config.BasicReturn;

lock (_publishLock)
{
var ackTask = _acknowledger.GetAckTask(channelTask.Result);
channelTask.Result.BasicPublish(
exchange: config.Exchange.ExchangeName,
routingKey: _config.RouteWithGlobalId ? $"{config.RoutingKey}.{globalMessageId}" : config.RoutingKey,
basicProperties: props,
body: _serializer.Serialize(message)
);
body: _serializer.Serialize(message),
mandatory: config.Mandatory
);
return ackTask;
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using RawRabbit.Configuration;
using RawRabbit.Exceptions;
using RawRabbit.IntegrationTests.TestMessages;
using RawRabbit.vNext;
using Xunit;
using ExchangeType = RawRabbit.Configuration.Exchange.ExchangeType;

Expand Down Expand Up @@ -389,6 +388,26 @@ public async Task Should_Be_Able_To_Publish_Message_After_Failed_Publish()
Assert.True(true);
}
}

[Fact]
public async Task Should_Run_Basic_Return_When_The_Manatory_Set_After_Failed_Publish()
{
/* Setup */
using (var client = TestClientFactory.CreateNormal())
{
var tcs = new TaskCompletionSource<bool>();
/* Test */
await client.PublishAsync(new SimpleMessage(),
configuration: cfg => cfg
.WithMandatoryDelivery((obj, evt) =>
{
tcs.SetResult(true);
}));

/* Assert */
Assert.True(tcs.Task.Result);
}
}
}
}

0 comments on commit 4b6e57e

Please sign in to comment.