Sterling Graham
Sterling Graham

Reputation: 564

Fire and forget messaging with actors?

Service Fabric's actor model is only supporting "request-reply" style communication as far as I can tell. Is there any way I can achieve "fire-and-forget" style communication? (Eg: Like the "ask" vs "tell" messaging in Akka?)

I've ran into this blog post (http://mikhail.io/2016/01/fire-and-forget-in-service-fabric-actors/), but am unsure if this will have any negative consequences or if there is a recommended way of accomplishing this?

Upvotes: 2

Views: 811

Answers (1)

Ryan Durham
Ryan Durham

Reputation: 331

Have an initialize method on your actor that seeds whatever is required for your task. Setup a timer and return from the initialize method. When the timer fires, shut down the timer, and run your logic Fire and Forget with the guarantee that your work is performed. Rough sample below.

public class SomeActor : Actor, ISomeActor
{
    private IActorTimer _sendTimer;

    protected override async Task OnActivateAsync()
    {
        await base.OnActivateAsync();
    }

    protected override async Task OnDeactivateAsync()
    {
        await this.ExecuteAgents();
        await base.OnDeactivateAsync();
    }

    public Task Initialize()
    {
        if (_sendTimer == null)
        {
            //setup timer
            _sendTimer = this.RegisterTimer((obj) =>
            {
                return this.Execute();
            },
                null,
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(30));
        }
    }

    private async Task ExecuteAgents()
    {
        this.UnegisterTimer(_sendTimer);

        //Perform workload
    }
}

Upvotes: 2

Related Questions