如何获取所有持久化实体 [英] How to fetch all persisted entities

查看:109
本文介绍了如何获取所有持久化实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对lagom框架完全陌生,因此,我今天正在阅读文档,并开始修改其hello world示例.

I am completely new to the lagom framework, hence, i was reading the documentation today and started modifying their hello world example.

但是,我无法找到一种方法来获取所有持久化的实体(即此示例中的所有持久化问候).

However, i am unable to find a way to fetch all persisted entities (i.e. all persisted greetings in this example).

这是默认示例获取某人问候的方式:

This is how the default example fetches a person's greeting:

@Override
public ServiceCall<GreetingMessage, Done> useGreeting(String id) {
  return request -> {
    // Look up the hello world entity for the given ID.
    PersistentEntityRef<HelloCommand> ref = persistentEntityRegistry.refFor(HelloWorld.class, id);
    // Tell the entity to use the greeting message specified.
    return ref.ask(new UseGreetingMessage(request.message));
  };
}

现在, 我要获取所有实体 ,而不是使用给定的ID查找实体,例如像persistentEntityRegistry.getIds()之类的东西,然后我可以通过id逐个获取它们.但是,实体注册表似乎不存在这种方法吗?

Now, instead of looking up an entity using a given ID, i want to fetch all entities, e.g. something like persistentEntityRegistry.getIds(), then i could fetch them one by one by id. However, such a method seems not to exist for the entity registry?

推荐答案

直接使用基础的Akka Persistence框架执行

It is possible to get all of the entity IDs by using the underlying Akka Persistence framework directly to do an allPersistenceIds or currentPersistenceIds query

您可以在Lagom在线拍卖示例应用程序的

You can see an example of this in use in the Lagom Online Auction example application, in UserServiceImpl.java:

public class UserServiceImpl implements UserService {
    //...
    private final CurrentPersistenceIdsQuery currentIdsQuery;
    private final Materializer mat;

    @Inject
    public UserServiceImpl(PersistentEntityRegistry registry, ActorSystem system, Materializer mat) {
        //...
        this.mat = mat;
        this.currentIdsQuery =
                PersistenceQuery.get(system)
                    .getReadJournalFor(
                        CassandraReadJournal.class,
                        CassandraReadJournal.Identifier()
                    );
        //...
    }

    //...
    @Override
    public ServiceCall<NotUsed, PSequence<User>> getUsers() {
        // Note this should never make production....
        return req -> currentIdsQuery.currentPersistenceIds()
                .filter(id -> id.startsWith("UserEntity"))
                .mapAsync(4, id ->
                    entityRef(id.substring(10))
                        .ask(UserCommand.GetUser.INSTANCE))
                .filter(Optional::isPresent)
                .map(Optional::get)
                .runWith(Sink.seq(), mat)
                .thenApply(TreePVector::from);
    }
    //...
}

尽管可能,但这种方法很少是一个好主意.您可能已经注意到示例代码中的注释:这永远都不能生产".使用这种方法无法执行聚合命令:您只能将命令一一发送到每个实体.这可能会导致服务群集中节点之间的内存消耗和流量激增.也可能无法按照实体状态的任何条件过滤此ID列表,就像您可能习惯于从面向行的SQL数据模型中一样.

This approach, while possible, is rarely a good idea. You may have noticed the comment in the example code: "this should never make production". There is no way to perform aggregate commands using this approach: you are limited to sending commands to each entity one by one. This can cause spikes in memory consumption and traffic between nodes in your service cluster. It also isn't possible to filter this list of IDs by any criteria of the entity state, as you might be used to from row-oriented SQL data models.

定义读取侧模型几乎总是更合适获取您的数据.这采取了一个单独的读取侧"数据存储的形式,该数据存储专门针对应用程序需要的查询类型而构建,并且事件处理器在您的实体发出事件时自动调用,该事件处理器会更新读取侧数据存储反映这些变化.

It is almost always more appropriate to define a read-side model for your data. This takes the form of a a separate "read-side" data store that is built to purpose for the type of queries your application needs, and an event processor that is automatically invoked as your entities emit events, which updates the read-side data store to reflect those changes.

Lagom框架通过管理读取侧事件处理器,跟踪它们在事件日志中的位置以及在重新启动或失败时自动重新启动它们,来帮助确保应用程序中的最终一致性.否则,这种类型的弹性很难实现聚合操作.

The Lagom framework helps to ensure eventual consistency in your application by managing your read-side event processors, tracking their position in the event log, and automatically restarting them on restart or failure. This type of resilience is otherwise tricky to implement for aggregate operations.

(此答案改编自Lagom框架中相关讨论Google网上论坛.)

(This answer is adapted from a related discussion in the Lagom Framework Google Group.)

这篇关于如何获取所有持久化实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆