Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.