Scala 远程演员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/540906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:18:40  来源:igfitidea点击:

Scala remote actors

scalaactor

提问by Alexander Stolz

Are there any guides or tutorials which explain the possibility to use scala actors remotely? All I have found until now is one example (without comments) but that's hardly enough.

是否有任何指南或教程可以解释远程使用 Scala 演员的可能性?到目前为止,我发现的只是一个例子(没有评论),但这还不够。

采纳答案by dmeister

I have written an article with an example app to explain the use of Remote Actorsa bit some time ago.

前段时间我写过一篇文章,用一个示例应用程序来解释一下 Remote Actors 的使用

Well, it has no comments inside the code (maybe you even meant that article), but there are explanations below the code.

好吧,它在代码中没有注释(也许你甚至指的是那篇文章),但是代码下面有解释。

回答by Szymon Jachim

Just be careful to send messages that are serializable (case classes and case objects are!) and be sure the opposite side can create the class. Watch out for custom ClassLoaders or missing JARs in you classpaths.

请小心发送可序列化的消息(案例类和案例对象!),并确保对方可以创建该类。注意自定义类加载器或类路径中丢失的 JAR。

回答by Daniel Spiewak

None of which I am aware. It's pretty much a "hack your way through the jungle" approach. Judging from the API though, things should work pretty much the same as regular actors, for which there exist one or two tutorials (as well as a few books now).

我不知道这些。这几乎是一种“穿越丛林”的方法。不过,从 API 来看,事情应该和普通演员几乎一样,有一个或两个教程(以及现在的几本书)。

If you do make use of remote actors, we (the community) would certainly welcome such a tutorial from an experienced user!

如果您确实使用远程演员,我们(社区)当然会欢迎有经验的用户提供这样的教程!

回答by Quartz

The Akka framework has remote actors. The API is pretty similar to regular scala actors.

Akka 框架具有远程角色。API 与常规的 Scala 演员非常相似。

They provide some level of automatic clustering as well, but it's not complete.

它们也提供了一定程度的自动聚类,但并不完整。

回答by Arne

recently there was a guide added on the front page of www.scala-lang.org, here is the link http://www.scala-lang.org/docu/files/actors-api/actors_api_guide.html#

最近在 www.scala-lang.org 的首页增加了一个指南,这里是链接 http://www.scala-lang.org/docu/files/actors-api/actors_api_guide.html#

回答by Connor Doyle

Maybe this is a necropost but I was looking for all over and could not find much. Hopefully this will help someone.

也许这是一个墓地,但我到处找,找不到太多。希望这会帮助某人。

I am running Mac OS 10.6.8and Scala 2.9.0.1. I had problems getting the canonical remote actors example running. I ended up with the following code.

我正在运行Mac OS 10.6.8Scala 2.9.0.1。我在运行规范的远程角色示例时遇到了问题。我最终得到了以下代码。

Note:The clearmethod is just to prevent messages from piling up. It's not critical to the example. Likewise the calls to Thread.sleepare just to make it easier to see what is going on at runtime.

注:清楚的方法就是防止堆积的消息。这对示例来说并不重要。同样,对Thread.sleep的调用只是为了更容易查看运行时发生的情况。

Compile it, then in separate shell instances do:

编译它,然后在单独的 shell 实例中执行:

$> scala Ping

and

$> scala Pong

in any order. You can experiment by killing one of them at a time and tracing the code.

以任何顺序。您可以通过一次杀死其中一个并跟踪代码来进行实验。

import scala.actors._
import scala.actors.Actor._
import scala.actors.remote._
import scala.actors.remote.RemoteActor._

/** @author Connor Doyle */

// Remote messages must be serializable.
// The easist way to do this is to wrap
// them with a case class
case class Message(text: String)

abstract class PingPongActor extends Actor with App {
    val pingPort = 9000
    val pongPort = 9001
    val delay = 1000
    classLoader = getClass().getClassLoader() // hack!
    start

    // this method consumes all pending messages
    // the library should have implemented an atomic 
    // receiveAndClear operation
    def clear: Unit = receiveWithin(0) {
        case TIMEOUT => ()
        case _ => clear
    }
}

object Ping extends PingPongActor {

    // result of select already lazy, but explicit lazy conveys
    // semantics clearly
    lazy val pong = select(Node("localhost", pongPort), 'pong)

    def act = {
        alive(pingPort)
        register('ping, self)
        loop {
            pong ! Message("ping")
            receiveWithin(delay * 2) {
                case Message(text: String) => {
                    clear
                    println("received: "+text)
                    Thread.sleep(delay) // wait a while
                }
                case TIMEOUT => println("ping: timed out!")
            }
        }
    }
}

object Pong extends PingPongActor {

    lazy val ping = select(Node("localhost", pingPort), 'ping)

    def act = {
        alive(pongPort)
        register('pong, self)
        loop {
            receiveWithin(delay * 2) {
                case Message(text: String) => {
                    println("received: "+text)
                    Thread.sleep(delay) // wait a while
                    clear
                    ping ! Message("pong")
                }
                case TIMEOUT => println("pong: timed out")
            }
        }
    }
}

Cheers!

干杯!