简体   繁体   English

最佳实践-Akka演员中的辅助方法

[英]Best practices- Helper methods in akka actor

I recently refactored my actor and ended up in a lot of helper methods which are part of the actor. 最近,我重构了演员,并最终获得了很多帮助者的帮助方法,这些方法是演员的一部分。 Is this the right thing to do? 这是正确的做法吗? Should all helper methods be an actor of their own? 所有辅助方法都应该成为自己的参与者吗? How do I make the seperation? 如何进行分隔?

  public class MyActor extends UntypedActor {



        public MyActor () {

        }

        @Override
        public void onReceive(Object msg) throws Exception {


        if ( msg instanceof doSomethingComplex) {
                doSomethingComplex message = (doSomethingComplex) msg;
               doSimple(message.prop);
}

private void  doSimple(String prop){
doSimpler(prop);
....
}

private void  doSimpler(String prop){
...
....
}

} }

Where should the helper methods be? 辅助方法应该在哪里? Should these be actors too? 这些人也应该是演员吗?

Should all helper methods be an actor of their own? 所有辅助方法都应该成为自己的参与者吗? How do I make the separation? 如何进行分离?

In addressing these questions, it's helpful to think about what an actor is. 在解决这些问题时,考虑一下演员是有帮助的。 Derek Wyatt, in his book Akka Concurrency , draws an analogy between an actor and a person (the following is an excerpt from the freely available fourth chapter of his book): 德里克·怀亚特(Derek Wyatt)在他的《 Akka Concurrency》一书中,在演员和人之间进行了类比(以下摘录自他的书的第四章 ):

Your day-to-day world is full of concurrency. 您的日常世界充满了并发性。 You impose it on yourself as well as the people around you, and they impose it on you. 您将它强加给自己以及周围的人,而他们强加给了你。 The real-world equivalents of critical sections and locks as well as synchronized methods and data are all naturally handled by yourself and the people in your world. 关键部分和锁的真实世界等价物以及同步的方法和数据都由您自己和您所在世界中的人们自然地处理。 People manage this by literally doing only one thing at a time. 人们通过一次只做一件事来解决这个问题。 We like to pretend that we can multi-task, but it's simply not true. 我们想假装我们可以执行多任务,但这不是真的。 Anything meaningful that we do requires that we do just that one thing. 我们要做的任何有意义的事情都要求我们只做一件事。 We can pause that task and resume it later, switch it out for something else to work on and then return to it, but actually doing more than one thing at a time just isn't in our wheelhouse. 我们可以暂停该任务,然后再继续执行,将其切换为其他工作然后再返回,但是实际上一次做不止一件事情就不在我们的驾驶室里。

So what if we want to do more than one thing at a time? 那么,如果我们想一次做一件以上的事情,该怎么办? The answer is pretty obvious: we just use more than one person. 答案很明显:我们只使用了一个以上的人。 There's not much in the world that we've benefited from that wasn't created by a gaggle of talented people. 世界上没有多少是不是由一群才华横溢的人从中受益。

This is why actors make our application development more intuitive and our application designs easier to reason about: they're modeled after our day-to-day lives. 这就是为什么参与者使我们的应用程序开发更直观,我们的应用程序设计更容易推理的原因:他们是按照我们的日常生活建模的。

And later in the same chapter, he writes: 在同一章的后面,他写道:

Actors only do one thing at a time; 演员一次只能做一件事; that's the model of concurrency. 这就是并发模型。 If you want to have more than one thing happen simultaneously, then you need to create more than one actor to do that work. 如果您希望同时发生多个事情,那么您需要创建多个演员来完成这项工作。 This makes pretty good sense, right? 这很有道理,对吗? We've been saying all along that actor programming draws a lot on your day-to-day life experiences. 我们一直在说,演员编程会从您的日常生活经验中汲取很多东西。 If you want work done faster, put more people on the job. 如果您想更快地完成工作,请更多的人来工作。

In designing an actor system, it's a good principle to assign the major parts of the system to actors that have distinct responsibilities. 在设计参与者系统时,将系统的主要部分分配给具有不同职责的参与者是一个很好的原则。 For example, in an ETL pipeline, one might have: 例如,在ETL管道中,可能有:

  1. An actor that subscribes to a queue for new data. 订阅队列以获取新数据的参与者。
  2. An actor that parses the raw data. 解析原始数据的参与者。
  3. An actor that filters parsed data for pieces of information in which the users are interested. 一个actor,它过滤解析的数据以获得用户感兴趣的信息。
  4. An actor that saves the results to a database. 将结果保存到数据库的参与者。
  5. An actor that publishes the results to a queue to which users can subscribe. 将结果发布到用户可以订阅的队列的参与者。

Let's say that the parser actor uses a helper method. 假设解析器参与者使用了一个辅助方法。 Whether this method should instead be encapsulated in its own actor depends on what the method does. 是否应将此方法封装在其自己的角色中取决于该方法的作用。 It's a good idea to break down tasks into sub-tasks, but at some point you have to decide that a task is too "small"--too small to be its own actor. 将任务分解为子任务是一个好主意,但是在某些时候,您必须确定任务太“小”,太小而无法成为自己的角色。

Returning to the person analogy, let's visualize each stage in the pipeline as a person. 回到这个人的类比,让我们以一个人的方式可视化管道中的每个阶段。 Let's assume that the parser person needs a sharpened pencil to do his work and that currently the parser gets the pencil himself. 让我们假设解析器人员需要一支削尖的铅笔来进行工作,并且当前解析器自己可以得到铅笔。 Would it make sense to hire a lowly intern (borrowing another illustration from Wyatt's book) whose sole job is to sharpen a pencil and give it to the parser person when requested? 雇用一个低级的实习生(从怀亚特的书中借用另一个插图)是否有意义,他的唯一工作就是削铅笔并根据要求将其交给分析人员? If a large inflow of data came in, would it suffice to hire more parser persons without hiring any interns? 如果有大量数据流入,是否足以雇用更多的分析人员而无需雇用任何实习生? It probably wouldn't be efficient to have 30 pencil-sharpening interns for ten parsers. 拥有30个削铅笔的实习生来容纳10个解析器可能不是很有效。 In other words, we probably wouldn't need to independently scale the number of interns in relation to the number of parsers. 换句话说,我们可能不需要相对于解析器数量独立地缩放实习生的数量。 If we did need to do so, that would signal that the intern's job is significant enough to justify hiring the intern. 如果确实需要这样做,则表明该实习生的工作足够重要,足以证明雇用该实习生是合理的。

To summarize this subjective food for thought: 总结一下这种主观的思想思考:

  • Decompose your system into parts with clear and distinct responsibilities. 将您的系统分解为职责明确且各不相同的部分。 Assign each part to an actor. 将每个部分分配给一个演员。
  • If necessary, decompose each part into sub-tasks. 如有必要,将每个部分分解为子任务。 Assign each sub-task to an actor. 将每个子任务分配给一个参与者。 A key way to help decide whether further decomposition is necessary is to determine whether it would be productive to independently scale the sub-task. 帮助确定是否需要进一步分解的一种关键方法是确定独立缩放子任务是否有成效。
  • Functionality that is too small to be its own actor can be placed inside helper methods, if you want to. 如果需要,可以将功能太小而不能成为其自己的参与者的功能。

Where should the helper methods be? 辅助方法应该在哪里?

A couple of ideas: 一些想法:

  • If a helper method is relevant only to a certain actor, declare it inside the actor as you're doing. 如果辅助方法仅与某个角色有关,则在执行操作时在角色内部声明它。
  • If a helper method could be used in different kinds of actors, declare it in a utility class . 如果可以在不同类型的参与者中使用辅助方法,请在实用程序类中进行声明。 Alternatively, declare it as a static method within an actor. 或者,将其声明为参与者内的静态方法

A helper method is a method that helps another method to perform it's task. 辅助方法是一种帮助另一种方法执行其任务的方法。 These are typically used when a method has to perform a complicated task that is composed of several smaller tasks. 当方法必须执行由几个较小任务组成的复杂任务时,通常使用这些方法。 The smaller tasks are often performed by helper methods. 较小的任务通常由辅助方法执行。

Helper methods are generally used just to separate a larger task into smaller more organized methods. 辅助方法通常仅用于将较大的任务分成较小的,更有组织的方法。 Helper methods should lie within the hierarchy of the class that is using them. 辅助方法应位于使用它们的类的层次结构中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM