我正在开发一个使用Akka Persistence作为事件源的服务。到目前为止,我们已经成功地将事件存储在Cassandra日志中。现在,我们希望通过利用Akka Persistence Query来实现CQRS。作为第一种方法,我们尝试遵循集群单例模式,让一个参与者按标签对存储的事件进行流处理。现在,我们将这个相当简单的参与者 Package 为单例:
public class EventProcessor extends AbstractLoggingActor {
private static final Logger LOG = LoggerFactory.getLogger(EventProcessor.class);
private final CassandraReadJournal journal;
public EventProcessor(ActorSystem system) {
journal =
PersistenceQuery.get(system)
.getReadJournalFor(CassandraReadJournal.class, CassandraReadJournal.Identifier());
journal
.eventsByTag(OnBoardingBehavior.ENTITY_TYPE_KEY.name(), Offset.noOffset())
.map(EventEnvelope::persistenceId)
.to(Sink.foreach(this::logMessage))
.run(system);
}
private void logMessage(String id) {
LOG.info(String.format("########## Received persistenceId %s", id));
}
@Override
public Receive createReceive() {
return null;
}
}
这就是我们如何把演员 Package 在守护者里面:
akka.actor.ActorSystem classicSystem = context.getSystem().classicSystem();
ClusterSingletonManagerSettings settings =
ClusterSingletonManagerSettings.create(classicSystem);
Props clusterSingletonManagerProps =
ClusterSingletonManager.props(
Props.create(EventProcessor.class, classicSystem),
PoisonPill.getInstance(),
settings);
classicSystem.actorOf(clusterSingletonManagerProps, "clusterSingletonManager");
当我们运行服务时,会得到以下异常(在actorOf行上):
java.lang.UnsupportedOperationException: cannot create top-level actor [clusterSingletonManager] from the outside on ActorSystem with custom user guardian
at akka.actor.ActorSystemImpl.actorOf(ActorSystem.scala:900)
at r.service.onboarding.actor.Guardian.initializeEventProcessor(Guardian.java:95)
at r.service.onboarding.actor.Guardian.<init>(Guardian.java:56)
at r.service.onboarding.actor.Guardian.lambda$create$745d95f3$1(Guardian.java:66)
at akka.actor.typed.javadsl.Behaviors$.$anonfun$setup$1(Behaviors.scala:47)
at a.a.t.i.BehaviorImpl$DeferredBehavior$$anon$1.apply(BehaviorImpl.scala:118)
at akka.actor.typed.Behavior$.start(Behavior.scala:168)
at a.a.t.i.InterceptorImpl$$anon$1.start(InterceptorImpl.scala:48)
at akka.actor.typed.BehaviorInterceptor.aroundStart(BehaviorInterceptor.scala:55)
at a.a.typed.internal.InterceptorImpl.preStart(InterceptorImpl.scala:71)
at a.a.typed.internal.InterceptorImpl$.$anonfun$apply$1(InterceptorImpl.scala:28)
at a.a.t.i.BehaviorImpl$DeferredBehavior$$anon$1.apply(BehaviorImpl.scala:118)
at akka.actor.typed.Behavior$.start(Behavior.scala:168)
at akka.actor.typed.Behavior$.interpret(Behavior.scala:275)
at akka.actor.typed.Behavior$.interpretMessage(Behavior.scala:230)
at a.a.t.internal.adapter.ActorAdapter.handleMessage(ActorAdapter.scala:126)
at a.a.t.internal.adapter.ActorAdapter.aroundReceive(ActorAdapter.scala:106)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:573)
at akka.actor.ActorCell.invoke(ActorCell.scala:543)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:269)
at akka.dispatch.Mailbox.run(Mailbox.scala:230)
at akka.dispatch.Mailbox.exec(Mailbox.scala:242)
... 5 frames excluded
我想提一下,我们可以从同一个守护者中产生其他演员,比如:
ActorRef<Command> actorRef =
context.spawn(OnBoardingBehavior.create(uuid), "OnBoardingBehavior-" + uuid);
我对Akka完全陌生,所以任何帮助都将不胜感激!
1条答案
按热度按时间piok6c0g1#
终于找到了。显然当在类型化系统中运行时,我不能使用
ClusterSingletonManager.props
的旧的非类型化方法。我在这里找到了在类型化系统中的正确方法:https://doc.akka.io/docs/akka/current/typed/cluster-singleton.html因此,我的演员现在是一个AbstractBehavior:
这就是我把它 Package 成一个单例的方式: