Skip to content

Instantly share code, notes, and snippets.

@woozer
Created December 13, 2016 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woozer/bc49ec0e2561d7ae13c687f1a509525e to your computer and use it in GitHub Desktop.
Save woozer/bc49ec0e2561d7ae13c687f1a509525e to your computer and use it in GitHub Desktop.
Sample remote atleast once
public class AtLeastOnceMessageDelivery : AtLeastOnceDeliveryActor
{
public ActorPath DeliveryPath { get; private set; }
public override string PersistenceId => "AtLeastOnce";
private const string DeliveryId = "DeliveryId";
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
public AtLeastOnceMessageDelivery(ActorPath deliveryPath)
{
DeliveryPath = deliveryPath;
}
protected override void Unhandled(object message)
{
if (message is UnconfirmedWarning)
{
var unconfirmed = message as UnconfirmedWarning;
unconfirmed.UnconfirmedDeliveries.Each(u =>
{
var result = ConfirmDelivery(u.DeliveryId);
});
}
}
public override void AroundPreRestart(Exception cause, object message)
{
}
protected override bool ReceiveCommand(object message)
{
var remoteMessage = message as IRemoteMessage;
if (remoteMessage != null)
{
Persist(remoteMessage, m =>
{
Deliver(DeliveryPath, id => ToJson(
new Confirmable(
id, DeliveryPath.ToString(),
m.Guid,
m.Data.GetType().Name,
m.Data))
);
});
}
else if (message is string)
{
// parse response message.
var jsonMessage = JObject.Parse((string)message);
long deliveryId;
if (long.TryParse(jsonMessage.GetValue(DeliveryId)?.ToString(), out deliveryId))
{
Persist(new Confirmation(deliveryId), m => ConfirmDelivery(m.DeliveryId));
}
else
{
return false;
}
}
else
{
return false;
}
return true;
}
protected override bool ReceiveRecover(object message)
{
var remoteMessage = message as IRemoteMessage;
if (remoteMessage != null)
{
var dataMessage = remoteMessage;
Deliver(DeliveryPath, id => ToJson(
new Confirmable(
id, DeliveryPath.ToString(),
dataMessage.Guid,
dataMessage.Data.GetType().Name,
dataMessage.Data))
);
}
else if (message is Confirmation)
{
var deliveryId = ((Confirmation)message).DeliveryId;
ConfirmDelivery(deliveryId);
}
else
return false;
return true;
}
private static string ToJson(object o)
{
return JsonConvert.SerializeObject(o, Settings);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment