Skip to content

Commit

Permalink
added DistributedPubSub support
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Apr 1, 2022
1 parent 253cf8e commit 494a555
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Akka.Cluster.Hosting/AkkaClusterHostingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,32 @@ public static AkkaConfigurationBuilder WithShardRegionProxy<TKey>(this AkkaConfi
registry.TryRegister<TKey>(shardRegionProxy);
});
}

/// <summary>
/// Starts <see cref="DistributedPubSub"/> on this node immediately upon <see cref="ActorSystem"/> startup.
/// </summary>
/// <param name="builder">The builder instance being configured.</param>
/// <param name="role">Specifies which role <see cref="DistributedPubSub"/> will broadcast gossip to. If this value
/// is left blank then ALL roles will be targeted.</param>
/// <returns>The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.</returns>
/// <remarks>
/// Stores the mediator <see cref="IActorRef"/> in the registry using the <see cref="DistributedPubSub"/> key.
/// </remarks>
public static AkkaConfigurationBuilder WithDistributedPubSub(this AkkaConfigurationBuilder builder,
string role)
{
var middle = builder.AddHocon(DistributedPubSub.DefaultConfig());
if (!string.IsNullOrEmpty(role)) // add role config
{
middle = middle.AddHocon($"akka.cluster.pub-sub = \"{role}\"");
}

return middle.WithActors((system, registry) =>
{
// force the initialization
var mediator = DistributedPubSub.Get(system).Mediator;
registry.TryRegister<DistributedPubSub>(mediator);
});
}
}
}

0 comments on commit 494a555

Please sign in to comment.