简体   繁体   English

Akka演员和未来:通过例子来理解

[英]Akka actors and futures: Understanding by example

I'm trying to learn Akka actors and futures but after reading the docs at http://akka.io and doing http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first-java.html I'm still having some issues with understanding. 我正在尝试学习Akka演员和未来,但在阅读了http://akka.io上的文档并做了http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first -java.html我仍然有一些理解问题。 I guess calculate the value of Pi is a thing a lot of people can relate too, but not me =). 我想计算Pi的值是很多人也可以联系的事情,但不是我=)。 I have search around a bit but haven't found any examples that suits me. 我搜索了一下,但没有找到任何适合我的例子。 Therefore I thought that I would take some real-life code of mine and throw it in here and exchange it for an example of how to do this with Akka. 因此,我认为我会采用我的一些现实代码并将其放入此处并交换它以获取如何使用Akka执行此操作的示例。

Ok so here we go: 好的,我们走了:

I have an java play2 application where I need to take some data from my DB and index it in my elasticsearch instance. 我有一个java play2应用程序,我需要从我的数据库中获取一些数据并将其编入我的elasticsearch实例中。

  1. I call the DB and get the ids for the venues. 我打电话给DB并获取场地的ID。

  2. I then split the list and create a couple of callable indextasks. 然后我拆分列表并创建几个可调用的indextasks。

  3. After that I invoke all tasks where each task collects the venues for the assigned ids from the db. 之后,我调用所有任务,其中每个任务从数据库收集指定ID的场地。

  4. For each venue index it to the elasticsearch instance and make it searchable. 对于每个场地,将其索引到elasticsearch实例并使其可搜索。

  5. Done. 完成。

Application.java: Application.java:

public class Application extends Controller {

  private static final int VENUE_BATCH = 1000;
  private static int size;

  public static Result index() {

      List<Long> venueIds = DbService.getAllVenueIds();
      size = venueIds.size();
      Logger.info("Will index " + size + " items in total.");
      ExecutorService service = Executors.newFixedThreadPool(getRuntime().availableProcessors());
      int startIx = 0;
      Collection<Callable<Object>> indexTasks = new ArrayList<Callable<Object>>();
      do {
          int endIx = Math.min(startIx + VENUE_BATCH, size);
          List<Long> subList = venueIds.subList(startIx, endIx);
          VenueIndexTask indexTask = new VenueIndexTask(subList);
          indexTasks.add(indexTask);
      } while ((startIx += VENUE_BATCH) < size);

      Logger.info("Invoking all tasks!");
      try {
          service.invokeAll(indexTasks);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }

      return ok(index.render("Done indexing."));
  } 
}

VenueTask: VenueTask:

public class VenueIndexTask implements Callable<Object> {

    private List<Long> idSubList;

    public VenueIndexTask(List<Long> idSubList){
        this.idSubList = idSubList;
        Logger.debug("Creating task which will index " + idSubList.size() + " items. " +
                "Range: " + rangeAsString() + ".");
    }

    @Override
    public Object call() throws Exception {
        List<Venue> venues = DbService.getVenuesForIds(idSubList);
        Logger.debug("Doing some indexing: "+venues.size());

        for(Venue venue : venues) {
            venue.index();
        }
        return null;
    }
    private String rangeAsString() {
        return "[" + idSubList.get(0) + "-" + idSubList.get(idSubList.size() - 1) + "]";
    }
}

Venue: 地点:

@IndexType(name = "venue")
public class Venue extends Index {

    private String name;

    // Find method static for request
    public static Finder<Venue> find = new Finder<Venue>(Venue.class);

    public Venue() {
    }

    public Venue(String id, String name) {
        super.id = id;
        this.name = name;
    }

    @Override
    public Map toIndex() {
        HashMap map = new HashMap();
        map.put("id", super.id);
        map.put("name", name);
        return map;
    }

    @Override
    public Indexable fromIndex(Map map) {
        if (map == null) {
            return this;
        }
        this.name = (String) map.get("name");
        return this;
    }
}

So all you Akka people out there go nuts! 所以你们阿克卡人都疯了! And please do as much as you can, propose cool futures functionality that could be used or any other knowledge/code that I could use to learn this stuff. 请尽可能多地做,提出可以使用的酷期货功能或我可以用来学习这些东西的任何其他知识/代码。

How I like to think of Akka (or any other message based systems) is to think like a conveyor belt, like in factories. 我如何想到Akka(或任何其他基于消息的系统)就像传送带一样思考,就像工厂一样。 A simplified way of thinking in Actors could be taking a pizza Order. Actors中一种简化的思维方式可能是采取披萨命令。

  • You, the hungry customer (Actor/Role) sends a order (A Message) to Pizza Shop 您,饥饿的客户 (演员/角色)向Pizza Shop发送订单(A Message)

  • Customer service (Actor/Role) takes your order, gives you the order number (Future) 客户服务(演员/角色)接受您的订单,为您提供订单号(未来)

  • If you were impatient, you might've waited on the phone/internet/shop till you got your pizza (A synchronous/blocking transaction) otherwise you would be happy with the order number an check up on it later (non-blocking) 如果你不耐烦,你可能已经等到手机/网络/商店,直到你拿到披萨(同步/阻止交易),否则你会对订单号感到满意,以后再检查(非阻塞)

  • Customer service sends the message to the Chefs (Actor) under the supervision of Kitchen Manager (Actor). 客户服务在厨房经理(演员)的监督下将消息发送给厨师(演员)。 This is a very process heavy kitchen, with hierarchy. 这是一个非常重的厨房,层次分明。 Akka likes that. 阿卡喜欢这样。 See Supervision 监督

  • Chef creates a new Pizza and attaches the details of the order (A new message) and passes that to the delivery boy (Actor) via the delivery manager (Supervisor Actor). Chef创建一个新的比萨并附上订单的详细信息(新消息)并通过交付经理(主管演员)将其传递给交付男孩(演员)。

  • During this process, your order details haven't changed, that would be a nightmare. 在此过程中,您的订单详细信息没有改变,这将是一场噩梦。 You would not be happy if you had pepperoni if you wanted plain cheese! 如果你想吃普通奶酪,你会不会感到高兴! All messages should be immutable! 所有消息都应该是不可变的! However, it may be that the message might be different for different actors. 但是,对于不同的参与者,消息可能不同。 A delivery boy would expect a Pizza and the order details attached, the chef would expect an order. 送货员会期待比萨饼和订单详情,厨师会期待订单。 When a message needs to change, a new message is created. 当消息需要更改时,会创建一条新消息。

  • Each actor is good at one role, how effective would it be if one guy had to do all the tasks? 每个演员都擅长一个角色,如果一个人必须完成所有任务,效果会如何? It may be that some actors out number others (eq 10 threads for Chefs, 2 for Delivery boys, 1 Customer Service). 可能是某些演员出了其他人(厨师为10个主题,送货男孩为2个,客户服务为1个)。

  • Blocking behavior is a pain, imagine customer service is waiting for the chef and delivery boy before seeing the next customer? 阻止行为是一种痛苦,想象客户服务在看到下一位客户之前等待厨师和送货员?

Hopefully I've helped you a little, this is a huge topic and large change of mind. 希望我能帮助你一点点,这是一个巨大的话题和大脑的改变。 Good luck 祝好运

Coursera currently runs a course on reactive programming which has the 3 last lectures on Akka and the actor model. Coursera目前正在开设一门关于反应式编程的课程,其中包括关于Akka和演员模型的最后三个讲座。 This includes video lectures and homework (in Scala though not Java). 这包括视频讲座和家庭作业(虽然不是Java,但在Scala中)。 While you are too late to receive the full certificate, you can still join the course and just check the last three weeks. 虽然您来不及收到完整的证书,但您仍然可以加入课程,只需查看过去三周。

https://class.coursera.org/reactive-001/class https://class.coursera.org/reactive-001/class

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

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