Akka及其错误内核

编程入门 行业动态 更新时间:2024-10-10 23:17:54
本文介绍了Akka及其错误内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在阅读Akka( Java lib )文档,需要澄清一些他们自己宣称的 Akka / Actor Best Practices 。

I am reading the Akka (Java lib) docs and need clarification on some of their own proclaimed Akka/Actor Best Practices.

在一些外部实体上,参与者不应该阻塞(即在占用线程时被动地等待)...阻塞操作应该是在一些特殊的线程中完成的,该线程向演员发送消息在他们身上。

Actors should not block (i.e. passively wait while occupying a Thread) on some external entity...The blocking operations should be done in some special-cased thread which sends messages to the actors which shall act on them.

那么这个代码示例在Akka / Java中看起来是什么样的?如果 Actor 不是将 的代码置于阻止的适当位置,那么 满足定义一些特殊线程的线程?

So what does a code example of this look like in Akka/Java? If an Actor isn't an appriote place to put code that has to block, then what does satisfy the definition of "some special-cased thread"?

不要在actor之间传递可变对象。为了确保,更喜欢不可变的消息。

Do not pass mutable objects between actors. In order to ensure that, prefer immutable messages.

我熟悉如何创建不可变的类(没有公共的setter,没有公共字段,使类最终等)。但Akka是否有自己的不可变类的定义,如果是,它是什么?

I'm familiar with how to make immutable classes (no public setters, no public fields, make the class final, etc.). But does Akka have its own definition of an "immutable class", and if so, what is it?

顶级演员是错误内核最里面的部分......

Top-level actors are the innermost part of your Error Kernel...

我甚至不知道这意味着什么!我理解顶级演员的意思(演员/经理/主管层次结构中最高),但是什么是错误内核,它与演员有什么关系?

I don't even know what this means! I understand what they mean by "top-level" actors (highest in the actor/manager/supervisor hierarchy), but what's an "Error Kernel", and how does it relate to actors?

推荐答案

我只能回答第一个问题(将来,请在帖子中只放一个问题)。 例如,考虑一个本质上阻塞的数据库连接。为了允许actor连接到数据库,程序员应该创建一个带有数据库请求队列的专用线程(或线程池)。请求包含数据库语句和对要接收结果的actor的引用。专用线程在循环中读取请求,访问数据库,将结果发送给引用的actor等。请求队列阻塞 - 当没有请求时,连接线程在 queue.take中被阻塞()操作。

I an able to answer only the first question (and in future, please place only one question in a post). Consider, for example, a database connection, which is inherently blocking. In order to allow actors to connect to a database, programmer should create a dedicated thread (or a thread pool) with a queue of database requests. A request contains a database statement and a reference to the actor which is to receive the result. The dedicated thread reads requests in a loop, accesses the database, sends the result to the referenced actor etc. The request queue is blocking - when there are no requests, the connection thread is blocked in the queue.take() operation.

因此,对数据库的访问分为两个参与者 - 一个将请求放入队列,另一个处理结果。

So the access to a database is split in two actors - one places a request to the queue, and the other handles the result.

更新:Java代码草图(我在Scala中不强)。

UPDATE: Java code sketch (I am not strong in Scala).

class Request { String query; ActorRef handler; } class DatabaseConnector implements Runnable { LinkedBlockingQueue<Request> queue=new LinkedBlockingQueue<Request>(); Thread t = new Thread(this); {t.start();} public void sendRequest(Request r) { queue.put(r); } public void run() { for (;;) { Request r=queue.take(); ResultSet res=doBlockingCallToJdbc(r.query); r.handler.sendOneWay(res); } }

更多推荐

Akka及其错误内核

本文发布于:2023-11-25 21:13:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内核   错误   Akka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!