diff --git a/src/core/Akka.API.Tests/verify/CoreAPISpec.ApprovePersistenceQuery.DotNet.verified.txt b/src/core/Akka.API.Tests/verify/CoreAPISpec.ApprovePersistenceQuery.DotNet.verified.txt index fc98e247614..84c81eb1408 100644 --- a/src/core/Akka.API.Tests/verify/CoreAPISpec.ApprovePersistenceQuery.DotNet.verified.txt +++ b/src/core/Akka.API.Tests/verify/CoreAPISpec.ApprovePersistenceQuery.DotNet.verified.txt @@ -81,8 +81,10 @@ namespace Akka.Persistence.Query public Akka.Event.ILoggingAdapter Log { get; } public static Akka.Persistence.Query.PersistenceQuery Get(Akka.Actor.ActorSystem system) { } public static Akka.Configuration.Config GetDefaultConfig() { } + public static Akka.Configuration.Config GetDefaultConfig(System.Type journalType) { } public TJournal ReadJournalFor(string readJournalPluginId) where TJournal : Akka.Persistence.Query.IReadJournal { } + public Akka.Persistence.Query.IReadJournal ReadJournalFor(System.Type readJournalType, string readJournalPluginId) { } } public class static PersistenceQueryExtensions { diff --git a/src/core/Akka.API.Tests/verify/CoreAPISpec.ApprovePersistenceQuery.Net.verified.txt b/src/core/Akka.API.Tests/verify/CoreAPISpec.ApprovePersistenceQuery.Net.verified.txt index e598732f711..b5df93f1d03 100644 --- a/src/core/Akka.API.Tests/verify/CoreAPISpec.ApprovePersistenceQuery.Net.verified.txt +++ b/src/core/Akka.API.Tests/verify/CoreAPISpec.ApprovePersistenceQuery.Net.verified.txt @@ -81,8 +81,10 @@ namespace Akka.Persistence.Query public Akka.Event.ILoggingAdapter Log { get; } public static Akka.Persistence.Query.PersistenceQuery Get(Akka.Actor.ActorSystem system) { } public static Akka.Configuration.Config GetDefaultConfig() { } + public static Akka.Configuration.Config GetDefaultConfig(System.Type journalType) { } public TJournal ReadJournalFor(string readJournalPluginId) where TJournal : Akka.Persistence.Query.IReadJournal { } + public Akka.Persistence.Query.IReadJournal ReadJournalFor(System.Type readJournalType, string readJournalPluginId) { } } public class static PersistenceQueryExtensions { diff --git a/src/core/Akka.Persistence.Query/PersistenceQuery.cs b/src/core/Akka.Persistence.Query/PersistenceQuery.cs index df7f1fe101a..7e17018942f 100644 --- a/src/core/Akka.Persistence.Query/PersistenceQuery.cs +++ b/src/core/Akka.Persistence.Query/PersistenceQuery.cs @@ -16,6 +16,8 @@ namespace Akka.Persistence.Query { public sealed class PersistenceQuery : IExtension { + private static readonly Type ReadJournalType = typeof(IReadJournal); + private readonly ExtendedActorSystem _system; private readonly ConcurrentDictionary _readJournalPluginExtensionIds = new(); private ILoggingAdapter _log; @@ -34,18 +36,24 @@ public PersistenceQuery(ExtendedActorSystem system) } public TJournal ReadJournalFor(string readJournalPluginId) where TJournal : IReadJournal + => (TJournal) ReadJournalFor(typeof(TJournal), readJournalPluginId); + + public IReadJournal ReadJournalFor(Type readJournalType, string readJournalPluginId) { + if(!ReadJournalType.IsAssignableFrom(readJournalType)) + throw new ArgumentException("Must implement IReadJournal interface", nameof(readJournalType)); + if(_readJournalPluginExtensionIds.TryGetValue(readJournalPluginId, out var plugin)) - return (TJournal)plugin; + return plugin; lock (_lock) { if (_readJournalPluginExtensionIds.TryGetValue(readJournalPluginId, out plugin)) - return (TJournal)plugin; + return plugin; - plugin = CreatePlugin(readJournalPluginId, GetDefaultConfig()).GetReadJournal(); + plugin = CreatePlugin(readJournalPluginId, GetDefaultConfig(readJournalType)).GetReadJournal(); _readJournalPluginExtensionIds[readJournalPluginId] = plugin; - return (TJournal)plugin; + return plugin; } } @@ -79,8 +87,11 @@ private IReadJournalProvider CreateType(Type pluginType, object[] parameters) } public static Config GetDefaultConfig() + => GetDefaultConfig(typeof(TJournal)); + + public static Config GetDefaultConfig(Type journalType) { - var defaultConfigMethod = typeof(TJournal).GetMethod("DefaultConfiguration", BindingFlags.Public | BindingFlags.Static); + var defaultConfigMethod = journalType.GetMethod("DefaultConfiguration", BindingFlags.Public | BindingFlags.Static); return defaultConfigMethod?.Invoke(null, null) as Config; } }