An easy to use frontend for MQ system (now supporting RabbitMq only, but would be nice to extend to other systems) using Reactive Extensions and a Publish/Subscribe paradigm.
Usage of a mailman is dead simple:
using AliceMQ.MailMan; //..
var source = new Source("A", "A.q");
var endPoint = new EndPoint();
var sink = new Sink(source);
var serialization = new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Error
};
var p = new Mailman(endPoint, source.Exchange, s => JsonConvert.SerializeObject(s, serialization));
//first message published creates exchange if non existent
p.PublishOne(new Msg(-1),"");
Now let's see the simplest form of consumer, which is just a thin layer from the real MQ system...
Consumer subscription is identical for every type, giving an istance of an IObservable (rx).
using AliceMQ.Mailbox;
var mb = new SimpleMailbox(endPoint, sink);
var d = mb.Subscribe(am =>
{
Console.WriteLine("A - " + Encoding.UTF8.GetString(am.EventArgs.Body));
am.Channel.BasicAck(am.EventArgs.DeliveryTag, false);
});
//...
d.Dispose();
let's consider an example DTO class Msg, the typed consumer is build upon the common consumer, which is enhanced with message body deserialization into an istance of a generic T type.
var sfm = new Mailbox<Msg>(endPoint, sink, s => JsonConvert.DeserializeObject<Msg>(s, serialization));
var d = sfm.Subscribe(am =>
{
if (am.IsOk<Msg>())
{
var msg = am.AsOk<Msg>().Message;
Console.WriteLine("ok - " + msg.Bla);
am.Confirm();
}
else
{
Console.WriteLine("error - " + am.AsError().Ex.Message);
am.Reject();
}
},
ex => Console.WriteLine("COMPLETE ERROR"),
() => Console.WriteLine("COMPLETE"));
//...
d.Dispose();
Both Mailman and Mailbox need that you provide some basic parameters for configuring the Endpoint, the Source (namely Exchange and Queue), and the Mailbox (with more sofisticated configurations).
string ConnectionUrl
bool AutomaticRecoveryEnabled
TimeSpan NetworkRecoveryInterval
IExchange Exchange
IQueueArgs QueueArgs
string ExchangeName
string ExchangeType
bool Durable
bool AutoDelete
IDictionary<string, object> Properties
string QueueName
bool Durable
bool Exclusive
bool AutoDelete
string DeadLetterExchangeName
IDictionary<string, object> QueueDeclareArguments
Source Source
BasicQualityOfService BasicQualityOfService
ConfirmationPolicy ConfirmationPolicy
QueueBind QueueBind
string RoutingKey
IDictionary<string, object> Arguments
ushort PrefetchCount
bool Global
bool AutoAck
bool Multiple
bool Requeue